BridgeCircuit.py Python Code

A Wheatstone Bridge Circuit.

Enter the Values of Ra, Rb, Rc, Rd, Re, and Vcc
to calculate the Bridge Circuits' total Current, and Branch Voltages and Currents.


"""
Python Code written: 27 August 2013.
[Added 14 Sept. 2020 - The Code below was written in Python 2.X and works up to Python 2.7.10. See below if you use Python 3.X]

Calculates the Total Current of a Bridge Circuit; and - the Voltages across and the Current through each Component.
"""

# --------------------------------------------------------------------------------------------------------------------- #
# Library Imports

# Allows Python 2.x to use Python 3.x 'print()' Function correctly. This Line must be before all other 'import' Entries.
from __future__ import print_function

# 'import' UNIX 'os' Library, and use 'os.system' to call 'clear' command.
import os;
os.system('clear');

# 'import' 'sys' Library, to access System level functions.
import sys;

# --------------------------------------------------------------------------------------------------------------------- #
# User Defined Functions:

def Pi_to_T(Ri,Rj,Rk,a):
 denom = float (Ri + Rj + Rk) # Calculate the denom Value.

 if (a == 1): # Use a Key to determine to display 'Rb: ' and 'Rc: ' or 'Rd: ' and 'Re: '.
  b = "Rb"
  c = "Rc"
 else:
  b = "Rd"
  c = "Re"

 # Print the entered Resistance Values; and, the calculated denom Value.
 print ("Ra:", str(round(Ri, 3)) + " Ohm(s); " + b + ": " + str(round(Rj, 3)) + " Ohm(s); "+ c + ": " + str(round(Rk, 3)) + " Ohm(s)"),
 print ("denom: ( Ra + " + b + " + " + c + " ) = ( " + str(round(Ri, 3)) + " + " + str(round(Rj, 3)) + " + " + str(round(Rk, 3)) + " ) = ", str(round(denom,3)) + " Ohm(s)"),
 print ()

 # Convert the entered pi Resistance Values to corresponding T Values.
 R1 = float( (Rj * Rk ) / denom )
 R2 = float( (Ri * Rk ) / denom )
 R3 = float( (Ri * Rj ) / denom )

 # Print the corresponding T Values.
 print ("Rx: (( " + b + " x " + c + " ) / denom ) = " + "(( " + str(Rj) + " * " + str(Rk) + " ) / " + str(denom) + " ) = ", str(round(R1, 3)) + " Ohm(s)"),
 print ("Ry: (( Ra x " + c + " ) / denom ) = " + "(( " + str(Ri) + " * " + str(Rk) + " ) / " + str(denom) + " ) = ", str(round(R2, 3)) + " Ohm(s)"),
 print ("Rz: (( Ra x " + b + " ) / denom ) = " + "(( " + str(Ri) + " * " + str(Rj) + " ) / " + str(denom) + " ) = ", str(round(R3, 3)) + " Ohm(s)"),
 print ()
 return (R1,R2,R3)

# --------------------------------------------------------------------------------------------------------------------- #
# Code starts here.

# Determine Version of Python:
pv = sys.version_info[:][0] # 'sys.version_info[:]' returns, for example, (2, 6, 1, 'final', 0) or (3, 3, 1, 'final', 0).

print ("Enter Bridge Components and Voltage Values below: ")
print ()
Ra = float( raw_input ("Enter Ra (in Ohms): "));
Rb = float( raw_input ("Enter Rb (in Ohms): "));
Rc = float( raw_input ("Enter Rc (in Ohms): "));
Rd = float( raw_input ("Enter Rd (in Ohms): "));
Re = float( raw_input ("Enter Re (in Ohms): "));
Vcc = float( raw_input ("Enter Vcc (in Volts): "));

print ()
print ("# ------------------------------------------ # ")
print ("Lower pi to T Conversion:")

result = Pi_to_T(Ra,Rb,Rc,1) # Convert the pi -> Ra, Rb, and Rc Values to a T -> R1, R2, and R3 Values.

(R1, R2, R3) = result # Extract the R1, R2, and R3 Values from the Pi_to_T() Function result.

Rl = R2 + Rd # Add the Resistance Values of the left Side Branch.
Rr = R3 + Re # Add the Resistance Values of the right Side Branch.

Rp = (Rl * Rr) / (Rl + Rr) # Calculate the parallel Resistance Value.

Rt = Rp + R1 # Add the total Resistance.
It = float( Vcc / Rt ) # Calculate the total Current.

IRl = ( It * Rr ) / ( Rl + Rr ) # Calculate the Current of the left Side Branch.
IRr = ( It * Rl ) / ( Rl + Rr ) # Calculate the Current of the right Side Branch.

IRd = IRl # The Current of the left Side Branch is the same Current through Rd.
IRe = IRr # The Current of the right Side Branch is the same Current through Re.

VRd = IRd * Rd # Calculate the Voltage across Rd.
VRe = IRe * Re # Calculate the Voltage across Re.

print ("Rl: ", str(round(Rl, 3)) + " Ohm(s); IRl: ", str(round(IRl, 3)) + " Amp(s)"), # Left Side Resistance and Current.
print ("Rr: ", str(round(Rr, 3)) + " Ohm(s); IRr: ", str(round(IRr, 3)) + " Amp(s)"), # Right Side Resistance and Current.
print ("Rp: ", str(round(Rp, 3)) + " Ohm(s); It: ", str(round(It, 3)) + " Amp(s)"), # Parallel Resistance Value, and Total Current.

print ()
print ("# ------------------------------------------ # ")
print ("Upper pi to T Conversion:")

result = Pi_to_T(Ra,Rd,Re,2) # Convert the pi -> Ra, Rd, and Re Values to a T -> R1, R2, and R3 Values.

(R1, R2, R3) = result # Extract the R1, R2, and R3 Values from the Pi_to_T() Function result.

Rl = R3 + Rc # Add the Resistance Values of the left Side Branch.
Rr = R2 + Rb # Add the Resistance Values of the right Side Branch.

Rp = (Rl * Rr) / (Rl + Rr) # Calculate the parallel Resistance Value.

Rt = Rp + R1 # Add the total Resistance.
It = float( Vcc / Rt ) # Calculate the total Current.

IRl = ( It * Rr ) / ( Rl + Rr ) # Calculate the Current of the left Side Branch.
IRr = ( It * Rl ) / ( Rl + Rr ) # Calculate the Current of the right Side Branch.

IRc = IRl # The Current of the left Side Branch is the same Current through Rc.
IRb = IRr # The Current of the right Side Branch is the same Current through Rb.

VRc = IRc * Rc # Calculate the Voltage across Rc.
VRb = IRb * Rb # Calculate the Voltage across Rb.

print ("Rl: ", str(round(Rl, 3)) + " Ohm(s); IRl: ", str(round(IRl, 3)) + " Amp(s)"), # Left Side Resistance and Current.
print ("Rr: ", str(round(Rr, 3)) + " Ohm(s); IRr: ", str(round(IRr, 3)) + " Amp(s)"), # Right Side Resistance and Current.
print ("Rp: ", str(round(Rp, 3)) + " Ohm(s); It: ", str(round(It, 3)) + " Amp(s)"), # Parallel Resistance Value, and Total Current.

VRa = VRc - VRb # Calculate the Voltage across Ra.
IRa = VRa / Ra # Calculate the Current through Ra.

# --------------------------------------------------------------------------------------------------------------------- #
# Display the calculated Values.

print()
print ("# ------------------------------------------ # ")
print ("Results:"),
print ("Vcc: ", str(round(Vcc, 3)) + " Volt(s)"),
print ("It: ", str(round(It, 3)) + " Amp(s)"),
print()
print ("Ra: ", str(round(Ra, 3)) + " Ohm(s); VRa: ", str(round(VRa, 3)) + " Volt(s); IRa: ", str(round(IRa, 3)) + " Amp(s)"),
print ("Rb: ", str(round(Rb, 3)) + " Ohm(s); VRb: ", str(round(VRb, 3)) + " Volt(s); IRb: ", str(round(IRb, 3)) + " Amp(s)"),
print ("Rc: ", str(round(Rc, 3)) + " Ohm(s); VRc: ", str(round(VRc, 3)) + " Volt(s); IRc: ", str(round(IRc, 3)) + " Amp(s)"),
print ("Rd: ", str(round(Rd, 3)) + " Ohm(s); VRd: ", str(round(VRd, 3)) + " Volt(s); IRd: ", str(round(IRd, 3)) + " Amp(s)"),
print ("Re: ", str(round(Re, 3)) + " Ohm(s); VRe: ", str(round(VRe, 3)) + " Volt(s); IRe: ", str(round(IRe, 3)) + " Amp(s)"),
print()

# Code ends here.
# --------------------------------------------------------------------------------------------------------------------- #
Download, and use, the actual Code provided here.

[Added 14 Sept. 2020]
If you use Python 3.X you need to replace all six (6) 'raw_input' Comamnds each, with 'input', and save the File.
Or, just download and use, the modified Code provided here.

Entered Values:

Ra = 2, Rb = 4, Rc = 6, Rd = 3, Re = 6, and Vcc = 30
Calculated Values:

Itotal = 6.667 A, VRa = 2.52 V and IRa = 1.25 A, VRb = 15 V and IRb = 3.75 A,
VRc = 17.5 V and IRc = 2.917 A, VRd = 12.5 V and IRd = 4.167 A, VRe = 15 V and and, IRe = 2.5 A.

Return to Python Code
©2008 - 2099, Alle Rechte vorbehalten, SJWL