import RPi.GPIO as GPIO
import time
# Define pin numbers
RED = 1
YELLOW = 5
GREEN = 9
# Setup
GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering
GPIO.setup(RED, GPIO.OUT)
GPIO.setup(YELLOW, GPIO.OUT)
GPIO.setup(GREEN, GPIO.OUT)
# Define delays
GREEN_DELAY = 3 # 3 seconds
YELLOW_DELAY = 0.5 # 0.5 seconds
RED_DELAY = 2 # 2 seconds
try:
while True:
# Green light on for 3 seconds
GPIO.output(GREEN, GPIO.HIGH)
time.sleep(GREEN_DELAY)
# Green off, yellow on for 0.5 seconds
GPIO.output(GREEN, GPIO.LOW)
GPIO.output(YELLOW, GPIO.HIGH)
time.sleep(YELLOW_DELAY)
# Yellow off, red on for 2 seconds
GPIO.output(YELLOW, GPIO.LOW)
GPIO.output(RED, GPIO.HIGH)
time.sleep(RED_DELAY)
# Yellow on for 0.5 seconds
GPIO.output(YELLOW, GPIO.HIGH)
time.sleep(YELLOW_DELAY)
GPIO.output(YELLOW, GPIO.LOW)
# Turn off red light
GPIO.output(RED, GPIO.LOW)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup() # Clean up GPIO settings