0
0
Raspberry Piprogramming~20 mins

Traffic light simulation in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Traffic Light Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple traffic light sequence
What will be the output of this Raspberry Pi traffic light simulation code snippet?
Raspberry Pi
import time

lights = ['Red', 'Green', 'Yellow']
for light in lights:
    print(f"Light is {light}")
    time.sleep(1)
A
Light is Red
Light is Green
Light is Yellow
B
Light is Red
Light is Yellow
Light is Green
C
Light is Green
Light is Yellow
Light is Red
D
Light is Yellow
Light is Green
Light is Red
Attempts:
2 left
💡 Hint
Look at the order of the lights in the list and how the loop prints them.
🧠 Conceptual
intermediate
1:30remaining
Understanding GPIO pin setup for traffic lights
Which statement correctly describes the purpose of setting GPIO pins as outputs in a Raspberry Pi traffic light program?
ATo read the current state of the traffic light buttons
BTo send signals to turn the LEDs on or off
CTo power the Raspberry Pi board
DTo connect the Raspberry Pi to the internet
Attempts:
2 left
💡 Hint
Think about what output pins do in controlling LEDs.
🔧 Debug
advanced
2:30remaining
Identify the error in this traffic light timing code
What error will this Raspberry Pi traffic light code produce when run?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

try:
    while True:
        GPIO.output(17, True)
        time.sleep(2)
        GPIO.output(17, False)
        time.sleep('2')
finally:
    GPIO.cleanup()
ANo error, the code runs correctly
BSyntaxError due to missing colon after while True
CRuntimeError because GPIO pin 17 is not set as output
DTypeError because time.sleep() received a string instead of a number
Attempts:
2 left
💡 Hint
Check the argument types passed to time.sleep.
📝 Syntax
advanced
2:00remaining
Correct syntax for turning on multiple LEDs
Which option correctly turns on GPIO pins 17, 27, and 22 to simulate a traffic light?
AGPIO.output([17, 27, 22], True)
BGPIO.output({17, 27, 22}, True)
CGPIO.output(17, True); GPIO.output(27, True); GPIO.output(22, True)
DGPIO.output(17 and 27 and 22, True)
Attempts:
2 left
💡 Hint
Check how GPIO.output accepts pin numbers.
🚀 Application
expert
3:00remaining
Calculate total cycle time of traffic light simulation
Given this traffic light timing code, what is the total time in seconds for one full cycle?
Raspberry Pi
import time

sequence = [
    ('Red', 5),
    ('Green', 3),
    ('Yellow', 2)
]

def run_cycle():
    for color, duration in sequence:
        print(f"Light: {color}")
        time.sleep(duration)

run_cycle()
A15 seconds
B5 seconds
C10 seconds
D20 seconds
Attempts:
2 left
💡 Hint
Add the durations for all colors in the sequence.