Resistor Calculator
Recently I had need of a simple way to create a circuit of equivalent resistance to a calculated value using only parts I had available from the ECE stockroom. Lucky for me this was made easy since there is a posted parts list spreadsheet including a page of all resistor values in stock.
The simplest way I could think of for doing this was just to use two resistors in parallel and find a pair of resistor values in the parts list that is equivalent to the value i'm looking for. Two resistors in parallel have an equivalent resistance of:
A little bit of coding and I wound up at this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import math res_val = 19872 approx_list = [] parallel = lambda r1, r2: float(r1 * r2) / float(r1 + r2) for sig in range(0, 4): if len(approx_list) > 0: pass else: for r1 in stockroom[len(stockroom) / 2:]: for r2 in stockroom: if math.trunc(parallel(r1, r2) / (10**sig)) == math.trunc(float(res_val) / (10**sig)): if (r2, r1, parallel(r1, r2)) not in approx_list: approx_list.append((r1, r2, parallel(r1, r2))) for r1, r2, req in approx_list: print '\t'.join([str(r1), str(r2), str(req)]) |
I left out the stockroom parts list because it's a bit long, there's about 130 resistor values included. Now mind you this program assumes a few things:
- You've already checked the parts list for the resistor value you're looking for.
- You've already checked the parts list for a resistor twice the value of the part you're looking for since in parallel they would equal resistance of the part you're looking for.
The basic logic of the program is like so:
- For 0 to 4 (skips digits when / 10 ** index of this for loop).
- If there aren't any resistor pairs in the approximation list then we haven't found a pair yet.
- For the first half of the stockroom list (we only need to compare once or we'll end up with swapped duplicates).
- Compare every item in the stockroom list to the current resistor value.
- If the equivalent resistance of the current pair of resistors is equal to the value you're looking for.
- Add it to the list.
Once we've compared all of them we go back and do the same process again but only if we found no resistor pairs matching the desired value. The next time through the list we'll ignore the last digit on both the equivalent resistance of the current resistor pair and the desired resistance value so we can at least get some sort of approximation even if it isn't optimal.
Update: I've been thinking a lot in my spare time about this and i'm sure there's a simple recursive way of developing a circuit with only resistors you have on hand provided you had a lot of the same ones. Probably just for nested parallel branches and series of them. If I have enough time to write the code for this I'll be sure to post it here.
