Traffic lights with a crossing button on pin 8, on an Arduino Uno
The traffic lights of pins 3, 4 and 5 with a pedestrian button on pin 8, held low by a 10k pull-down: the lights stay green for the cars until someone presses, then change to red to let them cross. The whole build, an Arduino Uno and 8 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Reading a button with a pull-down resistor
- Sequencing outputs with delay
- Wrapping repeated output code in a function
- Why a button needs a defined resting level
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 10
How it works
Three LEDs make a set of traffic lights: green on pin 3, yellow on pin 4 and red on pin 5. A button on pin 8 is the crossing request. A 10k resistor pulls pin 8 down to ground, so it reads LOW until someone presses.
Pressing joins pin 8 to 5 V and it reads HIGH. The lights go yellow for 2 seconds, then red for 5 seconds so people can cross, then back to green.
While nobody presses, the sketch does nothing but watch the button.
Parts list
10 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin GND |
| LED, red pin C | Arduino Uno R3 pin GND |
| LED, yellow pin C | Arduino Uno R3 pin GND |
| LED, green pin C | Arduino Uno R3 pin GND |
| Pushbutton pin 2a | Arduino Uno R3 pin 8 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 3 |
| Pushbutton pin 1a | Arduino Uno R3 pin 5V |
| Resistor, 10k Ω pin 2 | Pushbutton pin 2b |
| LED, red pin A | Resistor, 220 Ω (1) pin 2 |
| LED, yellow pin A | Resistor, 220 Ω (2) pin 2 |
| LED, green pin A | Resistor, 220 Ω (3) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A pedestrian crossing: traffic lights on 3, 4 and 5, a button on 8.
//
// pin 3 -> 220 ohm -> green LED -> GND
// pin 4 -> 220 ohm -> yellow LED -> GND
// pin 5 -> 220 ohm -> red LED -> GND
// one side of the button -> 5V; the other -> pin 8, and 10k from pin 8 to GND
//
// The 10k pull-down holds pin 8 LOW while nobody presses, so the lights sit
// on green for the cars. A press joins pin 8 to 5 V, it reads HIGH, and the
// lights go yellow, then red for long enough to cross, then back to green.
const int GREEN = 3;
const int YELLOW = 4;
const int RED = 5;
const int BUTTON = 8;
void lights(int green, int yellow, int red) {
digitalWrite(GREEN, green);
digitalWrite(YELLOW, yellow);
digitalWrite(RED, red);
}
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(BUTTON, INPUT);
lights(HIGH, LOW, LOW);
Serial.begin(9600);
Serial.println("Mokxi Uno: green for the cars. Press the button on pin 8 to cross.");
}
void loop() {
if (digitalRead(BUTTON) == HIGH) {
Serial.println("button pressed: yellow, then red");
lights(LOW, HIGH, LOW);
delay(2000);
lights(LOW, LOW, HIGH);
Serial.println("red: walk");
delay(5000);
lights(HIGH, LOW, LOW);
Serial.println("green for the cars again");
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
lights sketch.ino
Sets all three LEDs in one call. You pass it three values, one each for green, yellow and red, and it writes them to the pins. This keeps the sequence in loop short and easy to read.
void lights(int green, int yellow, int red) {
digitalWrite(GREEN, green);
digitalWrite(YELLOW, yellow);
digitalWrite(RED, red);
}setup sketch.ino
Sets the three LED pins as outputs and the button as a plain input. It starts on green with lights(HIGH, LOW, LOW), then opens serial at 9600 baud.
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(BUTTON, INPUT);
lights(HIGH, LOW, LOW);
Serial.begin(9600);
Serial.println("Mokxi Uno: green for the cars. Press the button on pin 8 to cross.");
}loop sketch.ino
Waits for BUTTON to read HIGH. When it does, it shows yellow for 2 seconds, then red for 5 seconds, then returns to green.
void loop() {
if (digitalRead(BUTTON) == HIGH) {
Serial.println("button pressed: yellow, then red");
lights(LOW, HIGH, LOW);
delay(2000);
lights(LOW, LOW, HIGH);
Serial.println("red: walk");
delay(5000);
lights(HIGH, LOW, LOW);
Serial.println("green for the cars again");
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lights change on their own This project
The button pin is floating. Check that the 10k resistor joins pin 8 to GND. - Pressing does nothing This project
One side of the button goes to 5 V, and the other side goes to pin 8. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.