#!/usr/bin/python3 """Design resistor dividers for voltage feedback. Slow, brute-force approach.x For example: $ time ./resisdivide.py 12 5 You can get a ratio of 2.4 between a resistor divider of 12000/(33000+12000) and a resistor divider of 1500/(12000+1500). You can get a ratio of 2.4 between a resistor divider of 12000/(33000+12000) and a resistor divider of 15000/(120000+15000). ... This suggests that using a 12kΩ resistor, a 33kΩ resistor, a 1.5kΩ resistor, and a second 12kΩ resistor, you can get circuit nodes that will be at the same voltage if you feed one divider with 12V and the other one with 5V. The second suggestion involving a 120kΩ resistor is probably less good because it's going to be noisier, but it would also use less power, so, meh. Probably there should be added a mode that designs just a single voltage divider, so you can divide 12V directly down to 5V, but I wrote this for a circuit where I was using the voltages as inputs to an op-amp which might not support rail-to-rail inputs. If I were comparing to a voltage reference IC or something, that would be a sensible thing to do. """ import sys, math, heapq E12 = [10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82] #decades = [10, 100, 1000, 10000, 100000] # too slow decades = [100, 1000, 10000] resistors = [x*y for x in E12 for y in decades] dividers = [(b/(a+b), a, b) for a in resistors for b in resistors] ratios = [(c/d, a1, b1, a2, b2) for c, a1, b1 in dividers for d, a2, b2 in dividers] def approximate(ratio): def key(stuff): "Sort key for approximation to desired ratio." candidate_ratio, a1, b1, a2, b2 = stuff # The secondary key here is how far the resistors are from 10kΩ. return (abs(math.log10(candidate_ratio/ratio)), sum(abs(math.log10(xi/10000)) for xi in [a1, b1, a2, b2])) return heapq.nsmallest(10, ratios, key=key) def main(argv): if len(argv) == 3: for r, a1, b1, a2, b2 in approximate(float(argv[1])/float(argv[2])): print(("You can get a ratio of %.2g between a resistor divider" + " of %d/(%d+%d) and a resistor divider" + " of %d/(%d+%d).") % (r, b1, a1, b1, b2, a2, b2)) else: raise "Usage" if __name__ == '__main__': main(sys.argv)