Traffic light with crossing button
Serial at 9600 baudTap 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 donePlace the three LEDs
Red, yellow and green in a column, long legs towards the board side.
Add a resistor to each
One 220 Ω resistor from each long leg to its pin: red D10, yellow D9, green D8.
Share the ground
Join all three short legs to the ground rail and the rail to GND.
Wire the button
Button across the centre gap, one side to D2, the other to GND.
Upload and press
Upload, open the serial monitor at 9600 baud, wait for green, then press the button.
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
- Development BoardsBuyArduino Uno R3 × 1
- SemiconductorsBuyLED 5 mm red × 1
- SemiconductorsBuyLED 5 mm yellow × 1
- SemiconductorsBuyLED 5 mm green × 1
- PassivesBuyResistor 220 Ω × 3
- SwitchesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Uno D10 | 220 Ω, then red LED anode | Red light |
| Uno D9 | 220 Ω, then yellow LED anode | Yellow light |
| Uno D8 | 220 Ω, then green LED anode | Green light |
| All LED cathodes | Uno GND | Return path |
| Uno D2 | Button, other side to GND | Crossing 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.
| Check | Expected |
|---|---|
| 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 |
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 →
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.
