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
#2
In Part I we learned how to use Ohms Law to find various measurements related to resistors connected in series.

In this segment, Part II, we will again use Ohms Law and how it relates to resistors connected in parallel. In the series circuit, we saw that there is a voltage drop across each resistor and the current was unchanged. In a parallel circuit, the voltage across each resistor is the same, while the current changes.

#! /usr/bin/env/ python3

# Circuit with 3 resistors connected in parallel with 9vdc source
# Voltage is constant thru all resistors in parallel.
#
#        ___________________________
#       |                __________|__________
#       |               |          |          |
#       +               |          |          |
# <9 vdc source>    <R1, 3k>   <R2, 10k>  <R3, 5k>
#       -               |          |          |
#       |               |__________|__________|
#       |__________________________|
#
# In a parallel circuit, RT = 1 / (1 / R1 + 1 / R2 + 1 / R3 ....n)
# From Ohms Law, I = V / R
# IT = 9 / 3000 + 9 / 10000 + 9 / 5000 = .003 + .0009 + .0018 = .0057 Amps
# To test, convert parallel resistors to 1 resistor using above formula
# RT = 1 / ((1 / 3000) + (1 / 10000) + (1 / 5000)) = 1578.94736 Ohms
# I = 9 / 1578.94736 = .005700 Amps


def parallel_res(number, source):
   """ Find total value of parallel resistors or coils or series capacitors """
   current_total = 0
   power_total = 0
   count = 0
   sub_parallel = 0
   res_values = []

   while count < number:
       try:
           val = float(input("Enter the resistor values (in Ohms): "))
           res_values.append(val)
           count += 1
       except ValueError as e:
           print("Not a number", e)
           continue

   for r in res_values:
       sub_parallel += 1 / r

   res_total = 1 / sub_parallel

   print("\nTotal of parallel resistors = {:.6f} Ohms".format(res_total))
   current = source / res_total
   print("Total current through circuit = {:.6f} Amps\n".format(current))

   for c in res_values:
       print("Current through {} Ohm resistor = {} Amps".format(c, source / c))
       print("...Power dissipated = {:.6f} Watts".format(current**2 * c))    # Use which ever power formula you prefer
       current_total += current * c
       power_total += current**2 * c
   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 parallel: "))
parallel_res(no_of_resistors, source_voltage)
Output:
Enter source voltage: 9 Enter the number of resistors in parallel: 3 Enter the resistor values (in Ohms): 3000 Enter the resistor values (in Ohms): 10000 Enter the resistor values (in Ohms): 5000 Total of parallel resistors = 1578.947368 Ohms Total current through circuit = 0.005700 Amps Current through 3000.0 Ohm resistor = 0.003 Amps ...Power dissipated = 0.097470 Watts Current through 10000.0 Ohm resistor = 0.0009 Amps ...Power dissipated = 0.324900 Watts Current through 5000.0 Ohm resistor = 0.0018 Amps ...Power dissipated = 0.162450 Watts Total power dissipated = 0.5848 Watts Process finished with exit code 0
You will also run across more "complex" circuits involving both parallel and series resistors.  In that case, solve for the parallel resistors (in essence creating one resistor (RT) then use that value in the series calculations.
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
#3
In Part I and Part II  we learned how to use Ohms Law to find various measurements related to resistors connected in series and in parallel configurations.

In this segment, Part III, we will again use Ohms Law to compute the values for a basic building block in electronics, the voltage divider.  The voltage divider does exacly what the name implies, it takes the source voltage and divides it for a lesser output voltage and depends on the voltage drop across the resistors connected. The circuit itself can consist of as little as 2 resistors connected in series to multiple resistors. Resistor 1 can also be replaced with a group of resistors in parallel in order to supply a certain current (a current divider). One or more of the resistors may also be replaced by potentiometers (adjustable resistors) in order to 'tweak' the resistance.

An example usage would be to consider the UART serial connections between the Raspberry Pi and the Arduino. The Raspberry Pi uses CMOS level logic (0 vdc for a low or 0, and 3.3 vdc for a high or 1) while the Arduino uses TTL level logic (0 vdc for a low or 0 and 5 vdc for a high or 1). The voltage divider isn't applicable when communicating from the Raspberry Pi to the Arduino since the output is always less then the source but due to the 'fudge' factor of the respective levels, it may or may not work, or work intermittently. Communicating in the other direction, from the Arduino to the Raspberry Pi will almost certainly overload and ruin the Raspberry Pi. Part of the solution is the voltage divder, dropping the source voltage of 5 vdc to 3.3 vdc. I say part of the solution, because we still have to consider the current and power dissipation in both directions. We can of course use our scripts in Parts I and II to ensure we stay within the parameters.  In todays world, we could also simply use a (Bidirectional) Logic Level Converter...but that would be cheating.

Here is our script for the voltage divider using two resistor in series:

#! /usr/bin/env/ python3

import os

# Simple Voltage Divider
#      V(out) = V(in) * (R2 / (R1 + R2))
#      or R1 = R2 * ((V(in) / V(out)) - 1)
#      or R2 = R1 * (1 / ((V(in) / V(out)) - 1)
#
#      V(in) _______
#                 <R1>
#                  |------------ V(out)
#      GRND  _____<R2>
#
# Ohms Law (A reminder)
#     V = I * R; I = V / R; R = V / I
#     P = V * I; P = I**2 * R; P = V**2 / R


def find_volt_out(v_in, res_1, res_2):
   v_out = v_in * (res_2 / (res_1 + res_2))
   print("Output of voltage divider = {:.4f} VDC".format(v_out))

   return


def find_res_1(v_in, v_out, res_2):
   res_1 = res_2 * ((v_in / v_out) - 1)
   print("Value of resistor 1 = {:.0f} Ohms".format(res_1))

   return


def find_res_2(v_in, v_out, res_1):
   res_2 = res_1 * (1 / ((v_in / v_out) - 1))
   print("Value of resistor 2 = {:.0f} Ohms".format(res_2))

   return


def main():
   # output ratio 3.3/5
   # volt_in = 5.0     TTL High or 1
   # volt_out = 3.33   CMOS High or 1
   # resist_1 = 25
   # resist_2 = 50
   volt_in = float(input("What is the supply voltage in Volts: "))
   volt_out = float(input("What is the desired output in Volts or '0' if unknown: "))
   resist_1 = float(input("What is the value of resistor 1 in Ohms or '0' if unknown: "))
   resist_2 = float(input("What is the value of resistor 2 in Ohms or '0' if unknown: "))

   if volt_out == 0:
       find_volt_out(volt_in, resist_1, resist_2)
   elif resist_1 == 0:
       find_res_1(volt_in, volt_out, resist_2)
   elif resist_2 ==0:
       find_res_2(volt_in, volt_out, resist_1)


if __name__ == "__main__":
   os.system('cls' if os.name == 'nt' else 'clear')

   main()
Output:
What is the supply voltage in Volts: 5 What is the desired output in Volts or '0' if unknown: 0 What is the value of resistor 1 in Ohms or '0' if unknown: 25 What is the value of resistor 2 in Ohms or '0' if unknown: 50 Output of voltage divider = 3.3333 VDC
Output:
What is the supply voltage in Volts: 5 What is the desired output in Volts or '0' if unknown: 3.33 What is the value of resistor 1 in Ohms or '0' if unknown: 0 What is the value of resistor 2 in Ohms or '0' if unknown: 50 Value of resistor 1 = 25 Ohms
Output:
What is the supply voltage in Volts: 5 What is the desired output in Volts or '0' if unknown: 3.33 What is the value of resistor 1 in Ohms or '0' if unknown: 25 What is the value of resistor 2 in Ohms or '0' if unknown: 0 Value of resistor 2 = 50 Ohms
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


Forum Jump:

User Panel Messages

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