Traffic Light Simulation in Wokwi: Complete Step-by-Step Guide

Traffic Light Simulation using Raspberry Pi Pico in Wokwi | MakeMindz
Free Simulation Included

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 Simulation

What 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:

🟢 Green → 🟡 Yellow → 🔴 Red → 🟡 Yellow → 🟢 Repeat

Components Used

🧠
Raspberry Pi Pico
Microcontroller
🔴
Red LED
GPIO 1 (GP1)
🟡
Yellow LED
GPIO 5 (GP5)
🟢
Green LED
GPIO 9 (GP9)
Resistors
220Ω or 330Ω × 3
💻
Wokwi Simulator
Free, browser-based

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
RASPBERRY PI PICO GP1 GP5 GP9 GND RED GP1 YELLOW GP5 GREEN GP9 330Ω 330Ω 330Ω GND LEGEND Red LED → GP1 Yellow LED → GP5 Green LED → GP9 GND (black) 330Ω Resistor

Circuit wiring diagram — Raspberry Pi Pico with Red, Yellow & Green LEDs

Traffic Light Sequence

ON
🟢 Green Light ON
3000 ms — Go signal
🟡 Yellow Light ON
500 ms — Prepare to stop
ON
🔴 Red Light ON
2000 ms — Stop signal
🟡 Yellow Light ON
500 ms — Prepare to go
💡
Total cycle time: ~6 seconds. You can adjust the delay() values in the code to match real-world traffic signal timings for your region.

Step-by-Step Instructions

1

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.

2

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().

3

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.

4

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.

5

Run the Simulation

Click the green ▶ Play button. Watch your traffic lights cycle through Green → Yellow → Red → Yellow in real time!

6

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:

📋 diagram.json
{
  "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:

⚡ sketch.ino — Arduino C++
// 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
}
🔧
Wokwi Tip: In Wokwi, click and drag between pin holes to create wires. The simulator routes them automatically. Use the diagram.json above to skip this step and have everything pre-wired!

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 Simulation
  Free  ·  No signup  ·  Works on mobile

Learning Outcomes

How GPIO pins work on Raspberry Pi Pico
LED interfacing with current-limiting resistors
Writing Arduino-style C++ code for Pico
Using pinMode() and digitalWrite()
Sequential logic and timing with delay()
Embedded state management — real-world automation logic
Circuit simulation without physical hardware
Digital output control for IoT and robotics

© 2026 MakeMindz.com — Raspberry Pi Pico Tutorials & STEM Projects

Built with ❤️ for students, makers & robotics enthusiasts

Comments

try for free