Python Forum
Basic DC Electronics- Resistors
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic DC Electronics- Resistors
#1
Seeing an increase recently in Raspberry Pi and Arduino questions, I thought I would dust off the cobwebs and write a simple script in Python about some basic DC resistor circuit calculations that might come in handy.

#! /usr/bin/env/ python3

# Circuit with 3 resistors connected is series with 9vdc source
# Current is constant thru all resistors in series.
#
#       _____________<R1, 3k>_____
#       |                        |
#       +                        |
# <9 vdc source>             <R2, 10k>
#       -                        |
#       |____________<R3, 5k>____|
#
# Total all resistors: RT = R1 + R2 + R3 = 18k ohms
# I = V / RT, I = 9 /18000, I = .0005 A
# Voltage Drops:
# VD_R1 = .0005 * 3000 = 1.5 V
# VD_R2 = .0005 * 10000 = 5 V
# VD_R3 = .0005 * 5000 = 2.5 V
# VT = 1.5 + 5 + 2.5 = 9 V
#
# P = V * I, P = 1.5 * .0005, P = .00075 W
# P = I^2 * R, P = .00000025 * 3000 = .00075 W
# P = V^2 / R, P = 2.25 / 3000 = .00075 W
# PT = (.0005 * 1.5) + (.0005 * 5) + (.0005 * 2.5) = .00075 + .0025 + .00125 = .0045 W
#
# And now lets put that in python:
#


def voltage_drop(number, source):
    res_total = 0
    volt_total = 0
    power_total = 0
    count = 0
    res_values = []

    while count < number:
        try:
            res_val = float(input("Enter the value of the resistors in ohms: "))
            res_values.append(res_val)
            res_total += res_val
            count += 1

        except ValueError as e:
            print("Not a number", e)
            continue

    print("\nTotal resistance = {} Ohms".format(res_total))
    current = source / res_total
    print("Current through circuit = {} Amps\n".format(current))

    for c in res_values:
        print("Voltage drop across {} Ohm resistor = {} Volts".format(c, current * c))
        print("...Power dissipated = {} Watts".format(current**2 * c))    # Use which ever power formula you prefer
        volt_total += current * c
        power_total += current**2 * c
    print("\nTotal of voltage drops = {} Volts".format(volt_total))
    print("Total power dissipated = {:.4f} Watts".format(power_total))    # Let's limit this to 4 decimal places

    return

source_voltage = float(input("Enter source voltage: "))
no_of_resistors = int(input("Enter the number of resistors in series: "))
voltage_drop(no_of_resistors, source_voltage)
Result:

Output:
C:\Python36\python.exe C:\Python\electronics\series_res.py Enter source voltage: 9 Enter the number of resistors in series: 3 Enter the value of the resistors in ohms: 3000 Enter the value of the resistors in ohms: 10000 Enter the value of the resistors in ohms: 5000 Total resistance = 18000.0 Ohms Current through circuit = 0.0005 Amps Voltage drop across 3000.0 Ohm resistor = 1.5 Volts ...Power dissipated = 0.00075 Watts Voltage drop across 10000.0 Ohm resistor = 5.0 Volts ...Power dissipated = 0.0025 Watts Voltage drop across 5000.0 Ohm resistor = 2.5 Volts ...Power dissipated = 0.00125 Watts Total 0f voltage drops = 9.0 Volts Total power dissipated = 0.0045 Watts Process finished with exit code 0
Of course these are "ideal' values.  It's would be best to use a DVM to get the actual values of the voltage source and resistor values before using the script.
It should be noted that the formula for resistors in series may also be used for inductors (in Henrys) in series and capacitors (in Farads) in parallel.

EDIT: Moved from scripts to tutorials, as it seemed a more appropriate spot.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Messages In This Thread
Basic DC Electronics- Resistors - by sparkz_alot - May-25-2017, 07:52 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020