📖 Scenario: You are creating a simple traffic light simulation using a Raspberry Pi. The traffic light has three colors: red, yellow, and green. Each color will be represented by a variable that can be either True (light on) or False (light off).
🎯 Goal: Build a program that simulates the traffic light changing colors in the correct order: red, green, yellow, then back to red. You will create variables for each light, set a timer for how long each light stays on, and print the current light color to the screen.
📋 What You'll Learn
Create three variables named red, yellow, and green to represent the traffic lights.
Create a variable named light_duration to set how many seconds each light stays on.
Use a for loop to cycle through the traffic light colors in the order: red, green, yellow.
Print the name of the light that is currently on.
💡 Why This Matters
🌍 Real World
Traffic lights control vehicle and pedestrian movement safely at intersections. Simulating them helps understand timing and sequencing.
💼 Career
Understanding how to control hardware like LEDs with code is useful for embedded systems and IoT jobs.
Progress0 / 4 steps
1
Set up the traffic light variables
Create three variables called red, yellow, and green. Set red to True and the others to False to start with the red light on.
Raspberry Pi
Hint
Think of each light as a switch that can be on (True) or off (False).
2
Add the light duration variable
Create a variable called light_duration and set it to 5 to represent the number of seconds each light stays on.
Raspberry Pi
Hint
This variable will help control how long each light stays on.
3
Cycle through the traffic lights
Use a for loop with the variable light to cycle through the list ["red", "green", "yellow"]. Inside the loop, set the variables red, yellow, and green so that only the current light is True and the others are False.
Raspberry Pi
Hint
Use light == "color" to set each variable to True or False.
4
Print the current light
Inside the for loop, add a print statement to display the text "Current light: " followed by the value of the variable light.
Raspberry Pi
Hint
Use print("Current light: " + light) to show the light color.
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
Step 1: Understand GPIO pin function
GPIO pins on Raspberry Pi are used to send signals to external devices like LEDs.
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.
Final Answer:
To control the LEDs turning on and off -> Option A
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
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.
Step 2: Choose correct mode for LED output
LEDs need output mode, so GPIO.OUT is correct for pin 17.
Final Answer:
GPIO.setup(17, GPIO.OUT) -> Option B
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?
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
Step 1: Check usage of HIGH and LOW constants
HIGH and LOW must be written as GPIO.HIGH and GPIO.LOW to be recognized.
Step 2: Verify other code parts
GPIO.setup with GPIO.OUT is correct; time.sleep works fine; pin 22 is valid.
Final Answer:
HIGH and LOW should be prefixed with GPIO. -> Option A
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?
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
Step 1: Check order and timing of lights
Traffic lights cycle red (2s), yellow (1s), green (3s) in that order repeatedly.
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.
Final Answer:
while True loop with red (2s), yellow (1s), green (3s) -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.