Raspberry Pi Program for Traffic Light Control
RPi.GPIO library to control GPIO pins for red, yellow, and green LEDs with timed delays; for example, set pins high and low in sequence with GPIO.output(pin, GPIO.HIGH) and time.sleep(seconds) to simulate a traffic light.Examples
How to Think About It
GPIO.output to control LEDs and time.sleep to wait between changes.Algorithm
Code
import RPi.GPIO as GPIO import time # Define GPIO pins RED = 17 YELLOW = 27 GREEN = 22 # Setup GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YELLOW, GPIO.OUT) GPIO.setup(GREEN, GPIO.OUT) try: while True: GPIO.output(RED, GPIO.HIGH) GPIO.output(YELLOW, GPIO.LOW) GPIO.output(GREEN, GPIO.LOW) print("Red ON") time.sleep(5) GPIO.output(RED, GPIO.LOW) GPIO.output(YELLOW, GPIO.HIGH) GPIO.output(GREEN, GPIO.LOW) print("Yellow ON") time.sleep(2) GPIO.output(RED, GPIO.LOW) GPIO.output(YELLOW, GPIO.LOW) GPIO.output(GREEN, GPIO.HIGH) print("Green ON") time.sleep(5) except KeyboardInterrupt: print("Program stopped") finally: GPIO.cleanup()
Dry Run
Let's trace one full cycle of the traffic light program through the code.
Turn on Red LED
GPIO.output(RED, GPIO.HIGH) sets pin 17 high; Red LED lights up. Yellow and Green pins set low, LEDs off.
Wait for 5 seconds
time.sleep(5) pauses the program, keeping Red LED on.
Turn on Yellow LED
GPIO.output(YELLOW, GPIO.HIGH) sets pin 27 high; Yellow LED lights up. Red and Green LEDs off.
Wait for 2 seconds
time.sleep(2) pauses the program, keeping Yellow LED on.
Turn on Green LED
GPIO.output(GREEN, GPIO.HIGH) sets pin 22 high; Green LED lights up. Red and Yellow LEDs off.
Wait for 5 seconds
time.sleep(5) pauses the program, keeping Green LED on.
| Step | Red Pin | Yellow Pin | Green Pin | LED On |
|---|---|---|---|---|
| 1 | HIGH | LOW | LOW | Red |
| 2 | HIGH | LOW | LOW | Red (waiting) |
| 3 | LOW | HIGH | LOW | Yellow |
| 4 | LOW | HIGH | LOW | Yellow (waiting) |
| 5 | LOW | LOW | HIGH | Green |
| 6 | LOW | LOW | HIGH | Green (waiting) |
Why This Works
Step 1: GPIO Setup
We use GPIO.setmode(GPIO.BCM) to refer to pins by their Broadcom numbers and set each LED pin as output with GPIO.setup(pin, GPIO.OUT).
Step 2: Control LEDs
Turning an LED on means setting its pin to GPIO.HIGH and turning others off with GPIO.LOW to simulate traffic light states.
Step 3: Timing with sleep
We use time.sleep(seconds) to keep each LED on for the correct duration, mimicking real traffic light timing.
Alternative Approaches
import RPi.GPIO as GPIO import time RED = 17 YELLOW = 27 GREEN = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YELLOW, GPIO.OUT) GPIO.setup(GREEN, GPIO.OUT) red_pwm = GPIO.PWM(RED, 100) yellow_pwm = GPIO.PWM(YELLOW, 100) green_pwm = GPIO.PWM(GREEN, 100) red_pwm.start(0) yellow_pwm.start(0) green_pwm.start(0) try: while True: red_pwm.ChangeDutyCycle(100) yellow_pwm.ChangeDutyCycle(0) green_pwm.ChangeDutyCycle(0) print("Red ON with PWM") time.sleep(5) red_pwm.ChangeDutyCycle(0) yellow_pwm.ChangeDutyCycle(100) green_pwm.ChangeDutyCycle(0) print("Yellow ON with PWM") time.sleep(2) red_pwm.ChangeDutyCycle(0) yellow_pwm.ChangeDutyCycle(0) green_pwm.ChangeDutyCycle(100) print("Green ON with PWM") time.sleep(5) except KeyboardInterrupt: print("Program stopped") finally: red_pwm.stop() yellow_pwm.stop() green_pwm.stop() GPIO.cleanup()
import RPi.GPIO as GPIO import time RED = 17 YELLOW = 27 GREEN = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YELLOW, GPIO.OUT) GPIO.setup(GREEN, GPIO.OUT) states = [ {'red': True, 'yellow': False, 'green': False, 'duration': 5}, {'red': False, 'yellow': True, 'green': False, 'duration': 2}, {'red': False, 'yellow': False, 'green': True, 'duration': 5} ] try: while True: for state in states: GPIO.output(RED, GPIO.HIGH if state['red'] else GPIO.LOW) GPIO.output(YELLOW, GPIO.HIGH if state['yellow'] else GPIO.LOW) GPIO.output(GREEN, GPIO.HIGH if state['green'] else GPIO.LOW) print(f"State: Red={state['red']}, Yellow={state['yellow']}, Green={state['green']}") time.sleep(state['duration']) except KeyboardInterrupt: print("Program stopped") finally: GPIO.cleanup()
Complexity: O(1) time, O(1) space
Time Complexity
The program runs in an infinite loop with fixed delays; no loops depend on input size, so time complexity is constant O(1).
Space Complexity
Only a few variables and GPIO pin states are stored, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in real time with delays; the basic GPIO on/off method uses least CPU, while PWM uses more CPU for brightness control.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic GPIO On/Off | O(1) | O(1) | Simple, low CPU usage |
| PWM Brightness Control | O(1) | O(1) | Smooth LED brightness, more complex |
| State Machine Dictionary | O(1) | O(1) | Easily extendable and modifiable states |
GPIO.cleanup() to reset pins when your program ends to avoid warnings.