555 Calculator - Python Script


The 555 timer is a remarkable chip. It can be used for a variety of purposes, including pulse generation, timing, and as an oscillator. In 'monostable mode', a single pulse can be generated from the chip's output. The duration of this pulse can be set with a single resistor and a single capacitor. The equation for the pulse duration is 1.1*R*C, where R is the resistor's resistance in ohms and C is the capacitance in farads. 

In my home projects, when I am away from the well stocked labs at my university, I only have access to my own small stockpile of components. The values of my stock of passive components range from many thousands of ohms, to picofarads of capacitors. When using the 555 timer, I sometimes want to use a monostable pulse, but am not always sure of what resistors and capacitors I must use in order to create a pulse of a desired width. 

To combat this issue, I wrote a Python script to do the calculation for me. The program asks the user what their desired pulse width is. After that, the program goes through 2 text files in my computer: one is a list of resistor values that I own, and one is a list of capacitor values that I own. The program returns a list of all the resistor and capacitor combinations that can be used to achieve a pulse width within 10% of the desired one. The text files in my computer can be edited at any time, upon obtaining new components.

An interesting challenge in this code was how to handle the matrix prefixes commonly used when describing the values of these components. Functions were written to take in a number with a metric prefix after it, and convert that to a floating point number that represents the actual value of the component. When the program is ready to print an acceptable RC combination out, the program searches through the list again, converting all the values to decimal form, and matches it to the acceptable decimal number, so that the program prints a value with a metric prefix, making it easier for the user to read.

 

#555 Monostable Circuit Calculator
#Calculates what combination of resistors and capacitors in my posession
#can be used in a 555 monostable circuit for a desired pulse width.

def read_file(file_name):
    infile = open(file_name,'r')
    componentList = []
    for line in infile:
        line_s = line.strip()
        if line_s != "":
            componentList.append(line_s)
    infile.close()
    return componentList

def PrefixToDecimal(string):
    stringLength = len(string)
    stringPrefix = string[stringLength - 1]
    
    if stringPrefix == 'm':
        multiplier = .001
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'u':
        multiplier = .000001
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'n':
        multiplier = .000000001
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'p':
        multiplier = .000000000001
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'k':
        multiplier = 1000
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'M':
        multiplier = 1000000
        base = float(string[:(stringLength-1)])
        
    elif stringPrefix == 'G':
        multiplier = 1000000000
        base = float(string[:(stringLength-1)])
    else:
        multiplier = 1
        base = float(string)

    decimal = base*multiplier
    return decimal

 

def main():
    boolean = 1
    while(boolean):
        resistors_raw = read_file('Resistors.txt')
        capacitors_raw = read_file('Capacitors.txt')
        resistors = []
        capacitors = []
        for i in range(len(resistors_raw)):
            value = PrefixToDecimal(resistors_raw[i])
            resistors.append(value)
        for i in range(len(capacitors_raw)):
            value = PrefixToDecimal(capacitors_raw[i])
            capacitors.append(value)

        desiredValue = float(input("Type your desired pulse width, in seconds.\n"))
        validList = []
        returnCapacitor = ""
        returnResistor = ""

        for i in range(len(resistors)):
            for j in range(len(capacitors)):
                calculatedValue = 1.1*resistors[i]*capacitors[j]
                if (calculatedValue < (1.1*desiredValue) and calculatedValue > (0.9*desiredValue)):

                    for k in range(len(resistors_raw)):
                        if (resistors[i] == PrefixToDecimal(resistors_raw[k])):
                            returnResistor = resistors_raw[k]
                    for m in range(len(capacitors_raw)):
                        if (capacitors[j] == PrefixToDecimal(capacitors_raw[m])):
                            returnCapacitor = capacitors_raw[m]

                    printedPulse = str("%.2f" % calculatedValue)                                        
                    string = "Pulse width: " + printedPulse + " Resistor: " + returnResistor + " Capacitor: " + returnCapacitor
                    validList.append(string)                                      
                

        if (len(validList) > 0):
            for n in range(len(validList)):
                print(validList[n])
            print("")
        else:
            print("This pulse width value cannot be obtained.")
            print("")

main()