Basic Python Programs Related to Electronics
1. Calculate Resistance (Ohm's Law)
voltage = 10 # in volts
current = 2 # in amperes
resistance = voltage / current
print("Resistance =", resistance, "Ohms")
2. Calculate Power
voltage = 5 # volts
current = 0.5 # amps
power = voltage * current
print("Power =", power, "Watts")
3. Find Capacitance Charging Time
R = 1000 # Ohms
C = 0.000001 # Farads (1uF)
time = 5 * R * C
print("Approximate Charging Time =", time, "seconds")
4. Convert mA to A
milliamp = 250 # mA
ampere = milliamp / 1000
print("Current in ampere =", ampere, "A")
5. Voltage Drop across Resistor
current = 0.03 # Amps
resistance = 220 # Ohms
voltage = current * resistance
print("Voltage Drop =", voltage, "Volts")
6. Series Resistance Calculator
r1 = 100 # Ohms
r2 = 220
r3 = 330
total = r1 + r2 + r3
print("Total Resistance in Series =", total, "Ohms")
7. Parallel Resistance Calculator
r1 = 100
r2 = 200
r3 = 300
total = 1 / (1/r1 + 1/r2 + 1/r3)
print("Total Resistance in Parallel =", total, "Ohms")
8. Resistor Color Code Reader
color_codes = {
"black": 0, "brown": 1, "red": 2, "orange": 3, "yellow": 4,
"green": 5, "blue": 6, "violet": 7, "gray": 8, "white": 9
}
band1 = input("Enter 1st color: ").lower()
band2 = input("Enter 2nd color: ").lower()
band3 = input("Enter multiplier color: ").lower()
if band1 in color_codes and band2 in color_codes and band3 in color_codes:
value = (color_codes[band1] * 10 + color_codes[band2]) * (10 ** color_codes[band3])
print("Resistor value =", value, "Ohms")
else:
print("Invalid color entered.")
9. Blink LED with Raspberry Pi
import RPi.GPIO as GPIO
import time
led_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
for i in range(5):
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(1)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(1)
GPIO.cleanup()
10. Buzzer ON/OFF with Raspberry Pi
import RPi.GPIO as GPIO
import time
buzzer_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(buzzer_pin, GPIO.OUT)
print("Buzzer ON")
GPIO.output(buzzer_pin, GPIO.HIGH)
time.sleep(2)
print("Buzzer OFF")
GPIO.output(buzzer_pin, GPIO.LOW)
GPIO.cleanup()