DeaD_EyE

voltage_divider

Aug 10th, 2020 (edited)
1,043
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2. from typing import Tuple
  3.  
  4.  
  5. def voltage_divider(v_input: float, v_output: float, current: float) -> Tuple[float, float, float]:
  6.     """
  7.    Calculate a voltage divider.
  8.  
  9.    :param v_input: Input voltage in V
  10.    :param v_output: Output voltage in V
  11.    :param current: defined current in A
  12.  
  13.    :returns: r1, r2, v_output
  14.    """
  15.     R = v_input / current
  16.     # e24 = [round(math.pow(10 ** x, 1/24), 1) for x in range(24)]
  17.     e24 = [
  18.         1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0,
  19.         2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3,
  20.         4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1,
  21.     ]
  22.     r1 = R / v_input * (v_input - v_output)
  23.     r2 = R - r1
  24.     factor_1 = int(math.log10(r1))
  25.     factor_2 = int(math.log10(r2))
  26.     rr1, rr2 = 0, 0
  27.     for res in e24:
  28.         if res * 10 ** factor_1 <= r1:
  29.             rr1 = res
  30.         if res * 10 ** factor_2 <= r2:
  31.             rr2 = res
  32.     idx_1 = e24.index(rr1)
  33.     idx_2 = e24.index(rr2)
  34.     if idx_1 < len(e24):
  35.         idx_1 += 1
  36.     if idx_2 < len(e24):
  37.         idx_2 += 1
  38.     r1 = e24[idx_1] * 10 ** factor_1
  39.     r2 = e24[idx_2] * 10 ** factor_2
  40.     return r1, r2, v_input - v_input / (r1 + r2) * r1
RAW Paste Data