0
0
Raspberry Piprogramming~30 mins

Traffic light simulation in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Traffic light simulation
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print("Current light: " + light) to show the light color.