Skip to content

Instantly share code, notes, and snippets.

@cibomahto
Created June 4, 2020 14:34
Show Gist options
  • Save cibomahto/135d36a52525cc858097ec98bcdcaaf6 to your computer and use it in GitHub Desktop.
Save cibomahto/135d36a52525cc858097ec98bcdcaaf6 to your computer and use it in GitHub Desktop.
Resistor divider brute force calculation
#For MT3608:
# Vout = Vref*(1+R1/R2)
import numpy as np
resistor_vals_E24 = np.array([1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1])
resistor_vals_E96 = np.array([1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30, 1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74, 1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32, 2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09, 3.16, 3.24, 3.32, 3.40, 3.48, 3.57, 3.65, 3.74, 3.83, 3.92, 4.02, 4.12, 4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49, 5.62, 5.76, 5.90, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32, 7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76])
def calc_best_divisor(equation, v_out, resistor_vals):
# Generate a big array of all values in decades 100-100000
vals = np.empty(1)
for decade in [100,1000,10000,100000]:
vals = np.append(vals, resistor_vals*decade)
solutions = []
for r1 in vals:
for r2 in vals:
v = equation(r1,r2)
err = abs((v-v_out)/v_out)*100
solutions.append([r1,r2,v,err])
# P=VI, V=IR=>I=V/R: P=V^2/R
solutions.sort(key = lambda x: x[3])
for solution in solutions[0:10]:
[r1,r2,v,err] = solution
power = v*v/(r1+r2)*1000 # P=V^2/r
print("r1:{:.0f} r2:{:.0f} Vout::{:.2f} Error:{:.4f}% Power:{:.2f}mW".
format(solution[0],solution[1],solution[2],solution[3],power))
def mt3608(r1, r2):
vref = 0.6
return vref*(1+r1/r2)
calc_best_divisor(equation = mt3608, v_out=12, resistor_vals=resistor_vals_E24)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment