Bird
Raised Fist0
Raspberry Piprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using GPIO pins in a Raspberry Pi traffic light simulation?
easy
A. To control the LEDs turning on and off
B. To increase the Raspberry Pi processing speed
C. To connect the Raspberry Pi to the internet
D. To store the traffic light timing data

Solution

  1. Step 1: Understand GPIO pin function

    GPIO pins on Raspberry Pi are used to send signals to external devices like LEDs.
  2. Step 2: Connect GPIO pins to LEDs

    In traffic light simulation, GPIO pins turn LEDs on or off to show red, yellow, or green lights.
  3. Final Answer:

    To control the LEDs turning on and off -> Option A
  4. Quick Check:

    GPIO pins control LEDs = A [OK]
Hint: GPIO pins control hardware like LEDs in simulations [OK]
Common Mistakes:
  • Thinking GPIO speeds up the CPU
  • Confusing GPIO with internet connection
  • Assuming GPIO stores data
2. Which of the following Python code snippets correctly sets up GPIO pin 17 as an output for the traffic light LED?
easy
A. GPIO.output(17, GPIO.IN)
B. GPIO.setup(17, GPIO.OUT)
C. GPIO.setup(17, GPIO.IN)
D. GPIO.output(17, GPIO.OUT)

Solution

  1. Step 1: Identify correct GPIO setup function

    GPIO.setup(pin_number, mode) is used to set pin mode; mode can be GPIO.OUT or GPIO.IN.
  2. Step 2: Choose correct mode for LED output

    LEDs need output mode, so GPIO.OUT is correct for pin 17.
  3. Final Answer:

    GPIO.setup(17, GPIO.OUT) -> Option B
  4. Quick Check:

    Setup pin as output = B [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) to set output pins [OK]
Common Mistakes:
  • Using GPIO.output instead of GPIO.setup to set mode
  • Setting pin as GPIO.IN instead of GPIO.OUT
  • Confusing setup and output functions
3. What will be the output behavior of this Python code snippet controlling a traffic light LED on GPIO 18?
GPIO.output(18, GPIO.HIGH)
time.sleep(3)
GPIO.output(18, GPIO.LOW)
medium
A. The LED on pin 18 stays off permanently
B. The LED on pin 18 blinks rapidly for 3 seconds
C. The program will cause a syntax error
D. The LED on pin 18 turns on for 3 seconds, then turns off

Solution

  1. Step 1: Analyze GPIO.output commands

    GPIO.HIGH turns the LED on; GPIO.LOW turns it off.
  2. Step 2: Understand time.sleep effect

    time.sleep(3) pauses the program for 3 seconds while LED stays on.
  3. Final Answer:

    The LED on pin 18 turns on for 3 seconds, then turns off -> Option D
  4. Quick Check:

    HIGH then sleep then LOW = C [OK]
Hint: HIGH turns LED on, LOW turns it off; sleep pauses timing [OK]
Common Mistakes:
  • Thinking LED blinks rapidly without loop
  • Assuming syntax error due to time.sleep
  • Confusing HIGH with LOW states
4. Identify the error in this traffic light simulation code snippet:
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, HIGH)
time.sleep(2)
GPIO.output(22, LOW)
medium
A. HIGH and LOW should be prefixed with GPIO.
B. GPIO.setup should use GPIO.IN instead of GPIO.OUT
C. time.sleep cannot be used with GPIO.output
D. Pin 22 cannot be used for output

Solution

  1. Step 1: Check usage of HIGH and LOW constants

    HIGH and LOW must be written as GPIO.HIGH and GPIO.LOW to be recognized.
  2. Step 2: Verify other code parts

    GPIO.setup with GPIO.OUT is correct; time.sleep works fine; pin 22 is valid.
  3. Final Answer:

    HIGH and LOW should be prefixed with GPIO. -> Option A
  4. Quick Check:

    HIGH/LOW require GPIO. prefix = D [OK]
Hint: Always prefix HIGH/LOW with GPIO. to avoid NameError [OK]
Common Mistakes:
  • Omitting GPIO. prefix for HIGH/LOW
  • Changing pin mode incorrectly
  • Thinking time.sleep causes error
5. You want to simulate a traffic light cycle with red, yellow, and green LEDs connected to GPIO pins 5, 6, and 13 respectively. Which Python code snippet correctly cycles through the lights with 2 seconds red, 1 second yellow, and 3 seconds green, repeating forever?
hard
A. GPIO.output(5, GPIO.HIGH) GPIO.output(6, GPIO.HIGH) GPIO.output(13, GPIO.HIGH) time.sleep(6) GPIO.output(5, GPIO.LOW) GPIO.output(6, GPIO.LOW) GPIO.output(13, GPIO.LOW)
B. for light in [5,6,13]: GPIO.output(light, GPIO.HIGH) time.sleep(2) GPIO.output(light, GPIO.LOW)
C. while True: GPIO.output(5, GPIO.HIGH) time.sleep(2) GPIO.output(5, GPIO.LOW) GPIO.output(6, GPIO.HIGH) time.sleep(1) GPIO.output(6, GPIO.LOW) GPIO.output(13, GPIO.HIGH) time.sleep(3) GPIO.output(13, GPIO.LOW)
D. while True: GPIO.output(13, GPIO.HIGH) time.sleep(3) GPIO.output(13, GPIO.LOW) GPIO.output(6, GPIO.HIGH) time.sleep(1) GPIO.output(6, GPIO.LOW) GPIO.output(5, GPIO.HIGH) time.sleep(2) GPIO.output(5, GPIO.LOW)

Solution

  1. Step 1: Check order and timing of lights

    Traffic lights cycle red (2s), yellow (1s), green (3s) in that order repeatedly.
  2. Step 2: Verify code matches timing and order

    while True: GPIO.output(5, GPIO.HIGH) time.sleep(2) GPIO.output(5, GPIO.LOW) GPIO.output(6, GPIO.HIGH) time.sleep(1) GPIO.output(6, GPIO.LOW) GPIO.output(13, GPIO.HIGH) time.sleep(3) GPIO.output(13, GPIO.LOW) uses while True loop, turns on red (pin 5) for 2s, then yellow (pin 6) for 1s, then green (pin 13) for 3s, turning off each after delay.
  3. Final Answer:

    while True loop with red (2s), yellow (1s), green (3s) -> Option C
  4. Quick Check:

    Correct order and timing in loop = A [OK]
Hint: Use while True and sequence GPIO.output with time.sleep [OK]
Common Mistakes:
  • Wrong order of lights
  • Using for loop without timing per light
  • Turning all LEDs on at once
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard