Voltage Dividers
This is a repost, with some python code for a very basic, (as in it worked when I last ran it) voltage divider calculator.
A voltage divider is a simple circuit which turns a large voltage into a smaller one. Using just two series resistors and an input voltage, we can create an output voltage that is a fraction of the input.
Voltage dividers are one of the most fundamental circuits in electronics. If learning Ohm's law was like being introduced to the ABC's, learning about voltage dividers would be like learning how to spell cat. [1]
The formula for a voltage divider is
and
At the next STEM group meeting, I plan to have some of my boards available. These can be connected together with either dupont cables or with crocodile clip cables. Then voltages can be measured with a multimeter.
Links
Code
I wrote this a while back, so including the code 'as is' below.
#!/usr/bin/env python3
#voltage divider calculator
# http://www.ohmslawcalculator.com/voltage-divider-calculator
print("Voltage Divider Calculator")
print("Version 1.0")
print("By Paul Sutton")
print("zleap@disroot.org")
print("http://www.zleap.net")
print
print("Formula = Vout = Vin * R2 / R1 + R2")
print
print("Enter values")
supvol = int(input('Enter Supply Voltage (v): '))
#str(supvol)
r1 = int(input( 'Enter R1 value (Ohms) : ' ))
r2 = int(input( 'Enter R2 value (Ohms) : ' ))
print ( 'Supply Voltage(V):', supvol )
print ( 'R1(Ohms : ', r1 )
print ( 'R2(Ohms : ', r2 )
#print(r2 / (r1 + r2))
mainsum = (r2 / (r1 + r2))
outvolt = (supvol * mainsum)
print ( 'Output Voltage = :', outvolt )
This is an improved version by Mark on IRC
#!/usr/bin/env python3
msg = """Voltage Divider Calculator (v1.1)
Formula: Voltage out is "Voltage in * Resistor 2 / Resistor 1 + Resistor 2"
You entered:
Voltage in {voltage}
Resistor 1 {resistor1}
Resistor 2 {resistor2}
Which equals:
{output}
Output voltage is: "{output}", rounded (nearest 10) is "{rounded}"!
"""
error = """Usage: python3 {script} <voltage in> <resistor 1> <resister 2>.
Example: python3 {script} 5000 2000 4000
Seeing an Error?
ValueError: You enter an invalid value (or left it empty).
"""
def main(args):
script = args.pop(0)
try:
voltage, resistor1, resistor2 = list(map(int, args))
output = voltage * (resistor2 / (resistor1 + resistor2))
except ValueError:
print(error.format(script=script))
raise
print(msg.format(voltage=voltage, resistor1=resistor1,
resistor2=resistor2, output=output,
rounded=round(output)))
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
Mastodon | Peertube | Join Mastodon |
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.