Traffic Light Simulation
with Raspberry Pi Pico
Build a real-world traffic signal in Wokwi — no hardware needed. Control Red, Yellow & Green LEDs with GPIO programming.
▶ Open Free SimulationWhat You'll Build
This beginner-friendly project teaches you how to use Raspberry Pi Pico GPIO pins to control three LEDs in a timed sequence — just like a real traffic light. You'll code it in Arduino-style C++ and run it instantly in the Wokwi browser simulator.
The traffic light follows this realistic pattern:
Components Used
GPIO Pin Connections
| LED | Pico Anode Pin | Pico GND Pin | Wire Color |
|---|---|---|---|
| Red LED | GP1 |
GND.1 |
Blue / Black |
| Yellow LED | GP5 |
GND.2 |
Blue / Black |
| Green LED | GP9 |
GND.3 |
Blue / Black |
Circuit wiring diagram — Raspberry Pi Pico with Red, Yellow & Green LEDs
Traffic Light Sequence
delay() values in the code to match real-world traffic signal timings for your region.Step-by-Step Instructions
Open Wokwi Simulator
Go to wokwi.com — it's completely free, no account required. Click New Project and select Raspberry Pi Pico as your board.
Set Environment to Arduino
In the diagram.json, set the Pico attribute "env": "arduino-community" so you can use Arduino-style functions like pinMode() and digitalWrite().
Paste the diagram.json
Click the diagram.json tab in Wokwi and replace the contents with the JSON below. This adds your Pico and three LEDs (Red, Yellow, Green) with all wires pre-connected.
Paste the Arduino Code
Click the sketch.ino (or main code) tab. Paste the Arduino code provided below. This defines the GPIO pins and the traffic light timing loop.
Run the Simulation
Click the green ▶ Play button. Watch your traffic lights cycle through Green → Yellow → Red → Yellow in real time!
Experiment & Customise
Try changing the delay() values. Add a buzzer for audio alerts, or a button for pedestrian crossings. Wokwi lets you drag and drop new components instantly.
diagram.json
Copy this JSON and paste it into the diagram.json tab in Wokwi:
{
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 20,
"left": 60,
"attrs": { "env": "arduino-community" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 3,
"left": 0,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 48,
"left": 0,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 98,
"left": 0,
"attrs": { "color": "green" }
}
],
"connections": [
[ "pico:GND.1", "led1:C", "black", [] ],
[ "pico:GP1", "led1:A", "blue", [] ],
[ "pico:GND.2", "led2:C", "black", [] ],
[ "pico:GP5", "led2:A", "blue", [] ],
[ "pico:GND.3", "led3:C", "black", [] ],
[ "pico:GP9", "led3:A", "blue", [] ],
[ "$serialMonitor:RX", "pico:GP0", "", [] ]
]
}
Arduino Code (sketch.ino)
Copy this code and paste it into the code editor tab in Wokwi:
// Traffic Light Simulation — Raspberry Pi Pico // MakeMindz.com | makemindz.com/traffic-light // Define GPIO pin numbers for each LED #define RED 1 #define YELLOW 5 #define GREEN 9 void setup() { // Set all three LED pins as digital outputs pinMode(RED, OUTPUT); pinMode(YELLOW, OUTPUT); pinMode(GREEN, OUTPUT); } void loop() { // ── Phase 1: GREEN — Go ────────────────── digitalWrite(GREEN, HIGH); delay(3000); // Green ON for 3 seconds // ── Phase 2: YELLOW — Prepare to Stop ─── digitalWrite(GREEN, LOW); digitalWrite(YELLOW, HIGH); delay(500); // Yellow ON for 0.5 seconds // ── Phase 3: RED — Stop ───────────────── digitalWrite(YELLOW, LOW); digitalWrite(RED, HIGH); delay(2000); // Red ON for 2 seconds // ── Phase 4: YELLOW — Prepare to Go ───── digitalWrite(YELLOW, HIGH); delay(500); // Yellow ON for 0.5 seconds digitalWrite(YELLOW, LOW); digitalWrite(RED, LOW); // Loop repeats from Phase 1 }
Run the Free Simulation
🚦 Open in Wokwi — No Hardware Needed
Click below to launch the pre-built simulation with everything ready. Watch the traffic light cycle live in your browser.
▶ Open Free SimulationLearning Outcomes
pinMode() and digitalWrite()delay()
Comments
Post a Comment