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

 


This tutorial shows how to create a traffic light simulator using Raspberry Pi Pico in the Wokwi online simulator. Wokwi is a free, browser-based electronics simulator - perfect for testing projects without physical hardware.

Getting Started with Wokwi

Step 1: Access Wokwi Simulator

  1. Go to https://wokwi.com
  2. Click on "Start from Scratch" or "New Project"
  3. Select "Raspberry Pi Pico" as your board

Step 2: Add Components to Your Circuit

Adding the Raspberry Pi Pico:

  • The Pico should already be on the canvas
  • If not, click the "+" button and search for "Raspberry Pi Pico"

Adding LEDs:

  1. Click the blue "+" button in the parts panel
  2. Search for "LED" and click to add
  3. Add three LEDs total (you can change colors after placing them)
  4. Click each LED to select it, then use the properties panel to set colors:
    • First LED: Red
    • Second LED: Yellow
    • Third LED: Green

Adding Resistors:

  1. Click the "+" button
  2. Search for "Resistor"
  3. Add three 220Ω or 330Ω resistors (one for each LED)

Step 3: Wire Your Circuit 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", "", [] ]
  ]
}

Connecting the Red LED:

  1. Click on the red LED's anode (positive/longer leg shown in simulator)
  2. Drag a wire to GPIO 15 on the Pico
  3. Connect the LED's cathode to one end of a resistor
  4. Connect the resistor's other end to any GND pin on the Pico

Connecting the Yellow LED:

  1. Connect yellow LED anode to GPIO 14
  2. Connect cathode through a resistor to GND

Connecting the Green LED:

  1. Connect green LED anode to GPIO 13
  2. Connect cathode through a resistor to GND
CODE:
#define RED 1
#define YELLOW 5
#define GREEN 9

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);
}

void loop() {
  digitalWrite(GREEN, HIGH);
  delay(3000);

  digitalWrite(GREEN, LOW);
  digitalWrite(YELLOW, HIGH);
  delay(500);

  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, HIGH);
  delay(2000);

  digitalWrite(YELLOW, HIGH);
  delay(500);
  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, LOW);
}


Wokwi Tip: In Wokwi, you can click and drag to create wires. The simulator automatically routes them neatly.

Comments