0
0
Raspberry Piprogramming~10 mins

Traffic light simulation in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Traffic light simulation
Start Simulation
Turn RED ON
Wait RED duration
Turn RED OFF, Turn GREEN ON
Wait GREEN duration
Turn GREEN OFF, Turn YELLOW ON
Wait YELLOW duration
Turn YELLOW OFF
Repeat cycle
The traffic light cycles through red, green, and yellow lights with waits in between, repeating continuously.
Execution Sample
Raspberry Pi
import time
from gpiozero import LED

red = LED(17)
green = LED(27)
yellow = LED(22)

while True:
    red.on()
    time.sleep(5)
    red.off()
    green.on()
    time.sleep(5)
    green.off()
    yellow.on()
    time.sleep(2)
    yellow.off()
This code turns on each traffic light color for a set time, then switches to the next color in a loop.
Execution Table
StepActionLED State (Red, Green, Yellow)Wait (seconds)Next Step
1Turn RED ONRed=ON, Green=OFF, Yellow=OFF52
2Turn RED OFF, GREEN ONRed=OFF, Green=ON, Yellow=OFF53
3Turn GREEN OFF, YELLOW ONRed=OFF, Green=OFF, Yellow=ON24
4Turn YELLOW OFFRed=OFF, Green=OFF, Yellow=OFF01 (repeat)
💡 Simulation runs infinitely; cycle repeats after step 4.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
redOFFONOFFOFFOFF
greenOFFOFFONOFFOFF
yellowOFFOFFOFFONOFF
Key Moments - 3 Insights
Why does the red light turn off before the green light turns on?
Because the code explicitly calls red.off() before green.on() in step 2, ensuring only one light is on at a time as shown in execution_table row 2.
What happens if we remove the time.sleep() calls?
Without time.sleep(), the lights would change instantly without delay, so the lights would appear to flicker or all turn off quickly, losing the traffic light effect (see Wait column in execution_table).
Why does the simulation never stop?
Because the while True loop repeats the steps endlessly, as noted in the exit_note and the Next Step column looping back to step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED state after step 3?
ARed=OFF, Green=OFF, Yellow=ON
BRed=ON, Green=OFF, Yellow=OFF
CRed=OFF, Green=ON, Yellow=OFF
DAll OFF
💡 Hint
Check the LED State column in execution_table row 3.
At which step does the green light turn on?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Action column in execution_table where green.on() is called.
If the time.sleep(5) after turning red on is changed to time.sleep(3), what changes in variable_tracker?
AGreen variable stays ON longer
BNo change in variable values, only timing changes
CRed variable changes to OFF earlier
DYellow variable turns ON earlier
💡 Hint
Variable_tracker shows LED states, not timing; timing changes do not affect ON/OFF states directly.
Concept Snapshot
Traffic light simulation cycles through red, green, yellow LEDs.
Use GPIO pins to control LEDs on Raspberry Pi.
Turn one LED ON, wait, then turn OFF and switch to next.
Use time.sleep() to pause between changes.
Repeat cycle endlessly with while True loop.
Full Transcript
This traffic light simulation code runs on a Raspberry Pi using GPIO pins to control three LEDs representing red, green, and yellow lights. The program turns the red LED on, waits 5 seconds, then turns it off and turns the green LED on for 5 seconds. Next, it turns green off and yellow on for 2 seconds, then turns yellow off. This cycle repeats forever. The execution table shows each step with LED states and wait times. The variable tracker shows how each LED's state changes after each step. Key moments clarify why lights turn off before the next turns on, the importance of delays, and the infinite loop. The quiz tests understanding of LED states at steps, when green turns on, and effects of changing wait times.