import machine
import time
# define the GPIO pins for the traffic light LEDs
RED_LED_PIN = 1 # GP0
YELLOW_LED_PIN = 5 # GP1
GREEN_LED_PIN = 9 # GP2
#Initialize the GPIO pins for the LEDs
red_led = machine.Pin(RED_LED_PIN, machine.Pin.OUT)
yellow_led = machine.pin(YELLOW_LED_PIN, machine.Pin.OUT)
green_led = machine.Pin(GREEN_LED_PIN, machine.Pin.OUT)
# Function to turn on the specified LED and turn off the others
def turn_on_led(led_pin):
red_led.value(led_pin == RED_LED_PIN)
yellow_led.value(led_pin == YELLOW_LED_PIN)
green_led.value(led_pin == GREEN_LED_PIN)
# Traffic light sequence
while True:
# Red light
turn_on_led(RED_LED_PIN)
time.sleep(5) # Red light for 5 seconds
# Red and yellow light (transition)
turn_on_led(YELLOW_LED_PIN)
time.sleep(2) # Red and yellow lights for 5 seconds
# Red light
turn_on_led(GREEN_LED_PIN)
time.sleep(5) # Green light for 5 seconds
# Red light
turn_on_led(YELLOW_LED_PIN)
time.sleep(2) # Yellow light for 2 seconds