Projects / Traffic light with crossing button

Traffic light with crossing button

Beginner50 minGuided buildReviewed
Arduino UnoD10D9D8D2GNDRedYellowGreenCrossing button
State: Go (0 s)Serial at 9600 baud

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 5 done
  1. UNO11.1 mA

    Place the three LEDs

    Red, yellow and green in a column, long legs towards the board side.

  2. UNO11.1 mA

    Add a resistor to each

    One 220 Ω resistor from each long leg to its pin: red D10, yellow D9, green D8.

  3. UNO

    Share the ground

    Join all three short legs to the ground rail and the rail to GND.

  4. UNO

    Wire the button

    Button across the centre gap, one side to D2, the other to GND.

  5. UNO

    Upload and press

    Upload, open the serial monitor at 9600 baud, wait for green, then press the button.

In short

How do you build a traffic light with crossing button?

Three LEDs run a traffic light cycle on an Arduino Uno; a button requests the pedestrian crossing. You need Arduino Uno R3, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 5 steps and about 50 minutes.

Level
Beginner
Time
About 50 min
Parts
7

Parts list

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

Wiring

FromToWhy
Uno D10220 Ω, then red LED anodeRed light
Uno D9220 Ω, then yellow LED anodeYellow light
Uno D8220 Ω, then green LED anodeGreen light
All LED cathodesUno GNDReturn path
Uno D2Button, other side to GNDCrossing request, internal pull-up

Code

Arduino IDE, board "Arduino Uno". No extra libraries. millis() keeps the loop free, so the button is always read.

Runs on a virtual Arduino Uno 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 RED = 10, YELLOW = 9, GREEN = 8, BUTTON = 2;
enum State { GO, SLOW, STOP, WALK };
State state = GO;
unsigned long since = 0;
bool requested = false;

void show(bool r, bool y, bool g) {
  digitalWrite(RED, r); digitalWrite(YELLOW, y); digitalWrite(GREEN, g);
}

void setup() {
  pinMode(RED, OUTPUT); pinMode(YELLOW, OUTPUT); pinMode(GREEN, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(BUTTON) == LOW && !requested) { requested = true; Serial.println("Crossing requested"); }
  unsigned long t = millis() - since;
  switch (state) {
    case GO:   show(0, 0, 1); if (t > 8000 || (requested && t > 3000)) { state = SLOW; since = millis(); } break;
    case SLOW: show(0, 1, 0); if (t > 2000) { state = STOP; since = millis(); } break;
    case STOP: show(1, 0, 0); if (t > 1000) { state = WALK; since = millis(); Serial.println("Walk"); } break;
    case WALK: show(1, 0, 0); if (t > 5000) { state = GO; since = millis(); requested = false; } break;
  }
}

Test checklist

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

CheckExpected
No pressGreen 8 s, yellow 2 s, red 1 s plus 5 s walk, repeat
Press during greenGreen ends within about 3 s, serial prints "Crossing requested"
Current per LEDAbout 10 to 15 mA with 220 Ω at 5 V

If it does not work

One LED never lights

Turn it round: the long leg goes to the resistor side.

Button does nothing

Check it spans the centre gap and goes to GND, and that INPUT_PULLUP is set.

All LEDs glow dimly

A shared resistor on the ground side: give each LED its own resistor.

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

Ask Lark AI

Common questions

What parts do I need for the traffic light with crossing button?

Arduino Uno R3, LED 5 mm red, LED 5 mm yellow, LED 5 mm green, Resistor 220 Ω × 3, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the traffic light with crossing button take to build?

About 50 minutes for a beginner maker, in 5 steps.

How do I know the traffic light with crossing button works?

No press: Green 8 s, yellow 2 s, red 1 s plus 5 s walk, repeat. Press during green: Green ends within about 3 s, serial prints "Crossing requested". Current per LED: About 10 to 15 mA with 220 Ω at 5 V.

One LED never lights: what should I check?

Turn it round: the long leg goes to the resistor side.

Button does nothing: what should I check?

Check it spans the centre gap and goes to GND, and that INPUT_PULLUP is set.

All LEDs glow dimly: what should I check?

A shared resistor on the ground side: give each LED its own resistor.