from machine import Pin
from utime import sleep
# Setup for LED pins
led = Pin(5, Pin.OUT) # LED 1
led1 = Pin(9, Pin.OUT) # LED 2
# Define the resistor value (same for both LEDs)
R = 330 # Resistor value in ohms
# Define supply voltage and LED forward voltage
Vcc = 3.3 # Supply voltage for Pi Pico
V_led = 2.0 # Approximate forward voltage for the LEDs (modify as needed)
# Function to calculate voltage across resistor and current
def calculate_values():
# Voltage across LED (assumed forward voltage of LED is 2V)
V_LED = V_led
# Voltage across resistor (remaining voltage after LED's forward voltage)
V_resistor = Vcc - V_LED
# Current through the resistor (same current flows through both LED and resistor in series)
current = V_resistor / R # Apply Ohm's Law: I = V / R
current_mA = current * 1000 # Convert current to milliamps (mA)
return V_LED, V_resistor, current_mA
while True:
led.toggle() # Toggle LED 1
led1.toggle() # Toggle LED 2
# Calculate and display voltage across LED and resistor, and current
V_LED, V_resistor, current_mA = calculate_values()
# Output for LED 1
print(f"LED 1 - Voltage across LED: {V_LED:.2f}V")
print(f"LED 1 - Voltage across Resistor: {V_resistor:.2f}V")
print(f"LED 1 - Estimated Current: {current_mA:.2f}mA")
# Output for LED 2 (same values as LED 1)
print(f"LED 2 - Voltage across LED: {V_LED:.2f}V")
print(f"LED 2 - Voltage across Resistor: {V_resistor:.2f}V")
print(f"LED 2 - Estimated Current: {current_mA:.2f}mA")
sleep(0.5) # Wait for 0.5 seconds before toggling again