Projects / Pedestrian crossing light

Pedestrian crossing light

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO12.9 mA

    Wire the car lights

    Red D3, yellow D4, green D5 with 220 ohm.

  2. NANO

    Wire the pedestrian lights

    Red D6, green D7 with 220 ohm.

  3. NANO

    Wire the button

    D2 to GND.

  4. NANO

    Wire the buzzer

    D8 and GND.

  5. NANO

    Upload the sketch

    Car green, pedestrian red.

  6. NANO

    Press the button

    A beep, then the sequence starts after the minimum green.

  7. NANO12.9 mA

    Watch the full cycle

    Yellow, red, gap, green man, flashing, gap.

  8. NANO

    Change the timings

    Edit the constants and compare.

In short

How do you build a pedestrian crossing light?

A model traffic light cycles through its safe sequence when a pedestrian presses the button, with a beeper that ticks while it is safe to cross. A teaching model only. You need Arduino Nano, LED 5 mm red × 2, LED 5 mm yellow, LED 5 mm green × 2, Resistor 220 Ω × 5, Tactile push button 6 mm, Passive buzzer, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
9

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Car red LED anode220 ohm to Nano D3Cars stop
Car yellow LED anode220 ohm to Nano D4Cars get ready to stop
Car green LED anode220 ohm to Nano D5Cars go
Pedestrian red LED anode220 ohm to Nano D6Do not cross
Pedestrian green LED anode220 ohm to Nano D7Safe to cross
All five LED cathodesNano GNDCommon ground
Pedestrian buttonNano D2 and GNDRequest to cross
Passive buzzer positiveNano D8Button and crossing beeps
Passive buzzer negativeNano GNDCommon ground

Code

A real pedestrian crossing is a state machine: a fixed order of states, each lasting a set time, with safety gaps between them. The model keeps the cars on green until the button is pressed, and the request is remembered. Once the cars have had a minimum green time, they go to yellow, then to red, and then there is a short gap with every light red before the pedestrian light turns green. That gap is the safety margin, and a real signal has one so that late cars have time to clear. While the green man is lit, the buzzer ticks once a second to help people who cannot see the light, and in the last three seconds the green flashes and the ticks speed up to warn that the time is nearly over. After another all-red gap the cars go again. Using millis instead of delay lets the button be read at any moment, so a press is never missed. Try changing the timings and watch how the safety gaps matter. This is a teaching model only. Real traffic signals follow national standards and must never be copied for real roads.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int CAR_R = 3, CAR_Y = 4, CAR_G = 5, PED_R = 6, PED_G = 7, BTN = 2, BUZZ = 8;

enum State { CARS_GREEN, CARS_YELLOW, ALL_RED_1, PED_GREEN, PED_FLASH, ALL_RED_2 };
State st = CARS_GREEN;
unsigned long tState = 0, lastBeep = 0, lastPress = 0;
bool request = false, wasPressed = false;

const unsigned long MIN_GREEN = 6000, YELLOW_MS = 3000, ALL_RED_1_MS = 1000;
const unsigned long PED_GREEN_MS = 6000, PED_FLASH_MS = 3000, ALL_RED_2_MS = 1500;

void lights(bool cr, bool cy, bool cg, bool pr, bool pg) {
  digitalWrite(CAR_R, cr); digitalWrite(CAR_Y, cy); digitalWrite(CAR_G, cg);
  digitalWrite(PED_R, pr); digitalWrite(PED_G, pg);
}

void enter(State s) {
  st = s;
  tState = millis();
  switch (s) {
    case CARS_GREEN:  lights(0, 0, 1, 1, 0); break;
    case CARS_YELLOW: lights(0, 1, 0, 1, 0); break;
    case ALL_RED_1:   lights(1, 0, 0, 1, 0); break;
    case PED_GREEN:   lights(1, 0, 0, 0, 1); request = false; lastBeep = 0; break;
    case PED_FLASH:   lights(1, 0, 0, 0, 1); break;
    case ALL_RED_2:   lights(1, 0, 0, 1, 0); break;
  }
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  int outs[] = {CAR_R, CAR_Y, CAR_G, PED_R, PED_G};
  for (int i = 0; i < 5; i++) pinMode(outs[i], OUTPUT);
  enter(CARS_GREEN);
}

void loop() {
  bool pressed = (digitalRead(BTN) == LOW);
  if (pressed && !wasPressed && millis() - lastPress > 40) {
    lastPress = millis();
    request = true;
    tone(BUZZ, 2000, 80);                                   // the request was received
  }
  wasPressed = pressed;

  unsigned long t = millis() - tState;
  switch (st) {
    case CARS_GREEN:
      if (request && t >= MIN_GREEN) enter(CARS_YELLOW);
      break;
    case CARS_YELLOW:
      if (t >= YELLOW_MS) enter(ALL_RED_1);
      break;
    case ALL_RED_1:
      if (t >= ALL_RED_1_MS) enter(PED_GREEN);
      break;
    case PED_GREEN:
      if (millis() - lastBeep >= 1000) { tone(BUZZ, 1000, 100); lastBeep = millis(); }   // slow ticks
      if (t >= PED_GREEN_MS) enter(PED_FLASH);
      break;
    case PED_FLASH:
      digitalWrite(PED_G, (t / 250) % 2 == 0);                                          // flashing green
      if (millis() - lastBeep >= 250) { tone(BUZZ, 1500, 60); lastBeep = millis(); }    // fast ticks
      if (t >= PED_FLASH_MS) enter(ALL_RED_2);
      break;
    case ALL_RED_2:
      if (t >= ALL_RED_2_MS) enter(CARS_GREEN);
      break;
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onCar green and pedestrian red
Press the buttonShort beep, request kept
After 6 seconds of greenCar yellow for 3 seconds, then red
One second all redPedestrian green with a tick each second
Last 3 seconds of crossingFlashing green and fast ticks

If it does not work

A light never comes on

Check its LED direction and the 220 ohm resistor.

Button seems ignored

Check D2 to GND and wait for the minimum green time.

Sequence runs without a press

A loose wire on D2, check the connection.

No beeps

Use a passive buzzer on D8.

Wrong light order

Check the pin numbers against the table.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pedestrian crossing light?

Arduino Nano, LED 5 mm red × 2, LED 5 mm yellow, LED 5 mm green × 2, Resistor 220 Ω × 5, Tactile push button 6 mm, Passive buzzer, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.

How long does the pedestrian crossing light take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the pedestrian crossing light works?

Power on: Car green and pedestrian red. Press the button: Short beep, request kept. After 6 seconds of green: Car yellow for 3 seconds, then red.

A light never comes on: what should I check?

Check its LED direction and the 220 ohm resistor.

Button seems ignored: what should I check?

Check D2 to GND and wait for the minimum green time.

Sequence runs without a press: what should I check?

A loose wire on D2, check the connection.

No beeps: what should I check?

Use a passive buzzer on D8.