Traffic lights are the classic first Arduino project. This version goes further: it's a pedestrian crossing. Cars get green until someone presses the button, the WAIT light comes on, and the lights change for the pedestrians and then back again. You'll learn how to write a state machine, the pattern behind almost every real controller, and how to time things with millis() so the button is never ignored.
What you need
- An Arduino Uno and its USB cable
- A breadboard and about a dozen jumper wires
- Two red LEDs, one amber (or yellow) LED and two green LEDs
- Five 470 Ω resistors (yellow, violet, brown, or yellow, violet, black, black, brown on 5-band metal film)
- One tactile push button
Shopping list (6 items)
Some shop links are affiliate links (Amazon UK, Tooled Up): if you buy through one we may earn a small commission, at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. It never changes which products we recommend, and no shop has paid to be included.
Everything for this build, in one place. One resistor kit and one LED pack cover this project and the others on the site.
- 1×
- 1×
- 1×
- 1×
- 1×Resistor kit, 1/4 W (0.25 W) assorted valuesOne kit covers every resistor in our projects, and is cheaper than buying values one at a time.
- 1×Tactile push button, 6 mm, four legsThe small square breadboard kind. Packs of 10 or more are the cheapest way to buy them.
- FreeArduino IDEThe free Arduino editor. Nothing to buy.
Each button opens that shop's page for the part, checked when we wrote the list; if one shop is out of stock, try the next. Any equivalent part with the same value works.
Starting from nothing? A starter kit gives you the board, a breadboard and many of these parts in one box.
Why these values
The Uno's pins give 5 V. A red LED drops about 2 V, so a 470 Ω resistor gives (5 − 2) ÷ 470 ≈ 6.4 mA. That's bright enough for a model, and it keeps every pin under the 8 mA per pin that Arduino gives for the UNO R4. (The older R3 allows 20 mA per pin, so 470 Ω is safe on both.) Amber and standard green LEDs drop a little more and come out at about 6 mA. Check other combinations with the LED resistor calculator.
The button needs no resistor. The sketch turns on the Uno's built-in pull-up with INPUT_PULLUP, so the pin reads HIGH normally and LOW while the button is pressed. The WAIT light is the Uno's own LED next to pin 13, which needs no wiring.
The circuit
Every LED is wired the same way: from the Arduino pin through a 470 Ω resistor to the LED's long leg (anode), with its short leg (cathode) to GND.
| Part | Arduino pin |
|---|---|
| Car red LED (through 470 Ω) | 4 |
| Car amber LED (through 470 Ω) | 3 |
| Car green LED (through 470 Ω) | 2 |
| Red man LED (through 470 Ω) | 6 |
| Green man LED (through 470 Ω) | 5 |
| Push button, one leg | 7 |
| Push button, diagonally opposite leg | GND |
| All LED short legs | GND |
| WAIT light | built-in LED, pin 13 (no wiring) |
- Unplug the Uno. Run a jumper from a GND pin to the breadboard's blue (−) rail.
- Place the five LEDs with each short leg in the ground rail and each long leg in its own row.
- Put a 470 Ω resistor from each LED's row to a free row, and jumper that row to the LED's Arduino pin from the table.
- Put the button across the centre gap. Wire one leg to pin 7 and the diagonally opposite leg to the ground rail. Diagonal legs always go through the switch.
Our Arduino Uno pinout shows where every pin is, and the breadboard guide explains which holes are joined.
Setting up the Arduino IDE
Install the free Arduino IDE from arduino.cc, plug in the Uno, choose Arduino Uno and its port from the board menu at the top, paste the sketch into a new window and click the arrow (Upload). If the upload fails, check you picked the right port, and that the cable carries data rather than only charging.
The sketch
// Pedestrian crossing traffic lights for an Arduino Uno.
// Cars get green until someone presses the button, then the lights
// change for the pedestrians and back again. No delay(), so the
// button is always being watched.
const byte CAR_RED = 4;
const byte CAR_AMBER = 3;
const byte CAR_GREEN = 2;
const byte WALK_RED = 6; // red man
const byte WALK_GREEN = 5; // green man
const byte BUTTON = 7; // to GND, reads LOW when pressed
const byte WAIT_LIGHT = LED_BUILTIN; // the "WAIT" light: the Uno's own LED, pin 13
// Times in milliseconds
const unsigned long MIN_GREEN = 10000; // cars always get at least this much green
const unsigned long AMBER = 3000;
const unsigned long ALL_RED = 2000; // safety gap with every light red
const unsigned long WALK = 8000; // green man time
const unsigned long RED_AMBER = 2000;
enum Stage { CARS_GO, CARS_AMBER, RED_BEFORE_WALK, WALKING, RED_AFTER_WALK, CARS_RED_AMBER };
Stage stage;
unsigned long stageStart;
bool waiting = false; // true once the button has been pressed
void showLights(bool carRed, bool carAmber, bool carGreen, bool walkGreen) {
digitalWrite(CAR_RED, carRed);
digitalWrite(CAR_AMBER, carAmber);
digitalWrite(CAR_GREEN, carGreen);
digitalWrite(WALK_GREEN, walkGreen);
digitalWrite(WALK_RED, !walkGreen); // the red man is on whenever the green man is off
}
void startStage(Stage s) {
stage = s;
stageStart = millis();
switch (s) {
case CARS_GO: showLights(false, false, true, false); break;
case CARS_AMBER: showLights(false, true, false, false); break;
case RED_BEFORE_WALK: showLights(true, false, false, false); break;
case WALKING: showLights(true, false, false, true); break;
case RED_AFTER_WALK: showLights(true, false, false, false); break;
case CARS_RED_AMBER: showLights(true, true, false, false); break;
}
}
void setup() {
pinMode(CAR_RED, OUTPUT);
pinMode(CAR_AMBER, OUTPUT);
pinMode(CAR_GREEN, OUTPUT);
pinMode(WALK_RED, OUTPUT);
pinMode(WALK_GREEN, OUTPUT);
pinMode(WAIT_LIGHT, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
startStage(CARS_GO);
}
void loop() {
unsigned long inStage = millis() - stageStart; // safe when millis() wraps round
// Remember a press, unless the green man is already showing
if (digitalRead(BUTTON) == LOW && stage != WALKING) {
waiting = true;
}
digitalWrite(WAIT_LIGHT, waiting);
switch (stage) {
case CARS_GO:
if (waiting && inStage >= MIN_GREEN) startStage(CARS_AMBER);
break;
case CARS_AMBER:
if (inStage >= AMBER) startStage(RED_BEFORE_WALK);
break;
case RED_BEFORE_WALK:
if (inStage >= ALL_RED) {
waiting = false;
startStage(WALKING);
}
break;
case WALKING:
if (inStage >= WALK) startStage(RED_AFTER_WALK);
break;
case RED_AFTER_WALK:
if (inStage >= ALL_RED) startStage(CARS_RED_AMBER);
break;
case CARS_RED_AMBER:
if (inStage >= RED_AMBER) startStage(CARS_GO);
break;
}
}
At first the car light is green and the red man is lit. Press the button and the Uno's own LED, the WAIT light, comes on. Once the cars have had at least 10 seconds of green, the lights go amber, then red, then the green man shows for 8 seconds, then red with amber, and back to green.
How it works
- A state machine. The crossing is always in exactly one stage, listed in the
enum. Each stage has its own set of lights and a rule for moving on.startStage()switches to a new stage, sets its lights and notes the time. Adding a stage is as simple as adding a name and acase. - No
delay(). Withdelay(10000)the Uno would be deaf to the button for 10 seconds. Hereloop()runs thousands of times a second, checks the button every time, and only changes stage whenmillis() − stageStartsays enough time has passed. Our guide to millis() vs delay() explains the idea, and why the subtraction keeps working whenmillis()wraps round after about 49.7 days. - Remembering the press. A press sets
waitingto true and it stays true until the green man appears, so even a quick tap is never lost. Because the press only sets a flag, contact bounce does no harm, so this sketch doesn't need debouncing. - Cars always get a fair turn. Even if someone holds the button down, the lights stay green for cars for at least
MIN_GREENafter they last turned green. - Safety gaps. Every light is red for 2 seconds before and after the green man, so the two signals can never show "go" at the same time. We tested this by running the sketch on a simulated Uno with dozens of button presses, checking every millisecond that the green man and a car green or amber are never on together.
The times follow the UK pattern of 3 seconds of amber and 2 seconds of red with amber, but the green times are short for testing. Change the constants at the top to suit.
Troubleshooting
- An LED never lights: it's probably the wrong way round. The long leg goes towards the Arduino pin. Swap it and try again.
- The button does nothing: check it goes from pin 7 to GND using diagonally opposite legs. Two legs on the same side are often joined inside, so the button would seem permanently pressed or never pressed.
- The lights change straight away without a press: pin 7 is touching GND all the time. Check the button's legs aren't in the same row.
- Upload fails: choose the right board and port, and close the Serial Monitor or any other program using the port.
Take it further
- Add a buzzer that beeps during the green man, like a real crossing. The parking sensor project shows how to use
tone(). - Make a crossroads: two sets of car lights that take turns. It's the same state machine with more stages.
- Print each stage to the Serial Monitor, then log it on your computer with our guide to reading Arduino serial data in Python.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.