from machine import Pin
import time
# Define pins for each signal
SIGNALS = {
'NS': {'RED': Pin(17, Pin.OUT), 'YELLOW': Pin(27, Pin.OUT), 'GREEN': Pin(22, Pin.OUT)},
'EW': {'RED': Pin(5, Pin.OUT), 'YELLOW': Pin(6, Pin.OUT), 'GREEN': Pin(13, Pin.OUT)},
'SN': {'RED': Pin(19, Pin.OUT), 'YELLOW': Pin(26, Pin.OUT), 'GREEN': Pin(21, Pin.OUT)},
'WE': {'RED': Pin(20, Pin.OUT), 'YELLOW': Pin(16, Pin.OUT), 'GREEN': Pin(12, Pin.OUT)}
}
# Function to control traffic light sequence
def traffic_light_control():
while True:
# North-South Green, East-West Red
SIGNALS['NS']['RED'].off()
SIGNALS['NS']['YELLOW'].off()
SIGNALS['NS']['GREEN'].on()
SIGNALS['EW']['RED'].on()
SIGNALS['EW']['YELLOW'].off()
SIGNALS['EW']['GREEN'].off()
SIGNALS['SN']['RED'].off()
SIGNALS['SN']['YELLOW'].off()
SIGNALS['SN']['GREEN'].on()
SIGNALS['WE']['RED'].on()
SIGNALS['WE']['YELLOW'].off()
SIGNALS['WE']['GREEN'].off()
time.sleep(10) # Green light duration for NS-SN
# Yellow light for North-South
SIGNALS['NS']['RED'].off()
SIGNALS['NS']['YELLOW'].on()
SIGNALS['NS']['GREEN'].off()
SIGNALS['SN']['RED'].off()
SIGNALS['SN']['YELLOW'].on()
SIGNALS['SN']['GREEN'].off()
time.sleep(3) # Yellow light duration for NS-SN
# North-South Red, East-West Green
SIGNALS['NS']['RED'].on()
SIGNALS['NS']['YELLOW'].off()
SIGNALS['NS']['GREEN'].off()
SIGNALS['EW']['RED'].off()
SIGNALS['EW']['YELLOW'].off()
SIGNALS['EW']['GREEN'].on()
SIGNALS['SN']['RED'].on()
SIGNALS['SN']['YELLOW'].off()
SIGNALS['SN']['GREEN'].off()
SIGNALS['WE']['RED'].off()
SIGNALS['WE']['YELLOW'].off()
SIGNALS['WE']['GREEN'].on()
time.sleep(10) # Green light duration for EW-WE
# Yellow light for East-West
SIGNALS['EW']['RED'].off()
SIGNALS['EW']['YELLOW'].on()
SIGNALS['EW']['GREEN'].off()
SIGNALS['WE']['RED'].off()
SIGNALS['WE']['YELLOW'].on()
SIGNALS['WE']['GREEN'].off()
time.sleep(3) # Yellow light duration for EW-WE
# Run the traffic light control
try:
traffic_light_control()
except KeyboardInterrupt:
for signal in SIGNALS.values():
for led in signal.values():
led.off()