Arduino Traffic Light: Wiring, Code and Simulator
To build this Arduino Traffic Light project you need an Arduino Uno and three 220 Ω resistors, a red LED, a yellow LED and a green LED. Three LEDs on pins 13, 12 and 11 cycling red → green → yellow: a state machine in code. Make the 9 connections in the wiring table, upload the Arduino C++ code, and watch the sequence, then draw the same machine in the FSM Synthesizer and compare.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 3 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | yellow LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | green LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 13 | 220 Ω resistor (rR) leg 1 |
| 2 | Arduino Uno pin 12 | 220 Ω resistor (rY) leg 1 |
| 3 | Arduino Uno pin 11 | 220 Ω resistor (rG) leg 1 |
| 4 | 220 Ω resistor (rR) leg 2 | red LED anode (A, long leg) |
| 5 | 220 Ω resistor (rY) leg 2 | yellow LED anode (A, long leg) |
| 6 | 220 Ω resistor (rG) leg 2 | green LED anode (A, long leg) |
| 7 | Arduino Uno GND | red LED cathode (C, short leg) |
| 8 | Arduino Uno GND | yellow LED cathode (C, short leg) |
| 9 | Arduino Uno GND | green LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add three 220 Ω resistors, a red LED, a yellow LED and a green LED from the parts palette.
- Wire 1. Connect Arduino Uno pin 13 to 220 Ω resistor (rR) leg 1.
- Wire 2. Connect Arduino Uno pin 12 to 220 Ω resistor (rY) leg 1.
- Wire 3. Connect Arduino Uno pin 11 to 220 Ω resistor (rG) leg 1.
- Wire 4. Connect 220 Ω resistor (rR) leg 2 to red LED anode (A, long leg).
- Wire 5. Connect 220 Ω resistor (rY) leg 2 to yellow LED anode (A, long leg).
- Wire 6. Connect 220 Ω resistor (rG) leg 2 to green LED anode (A, long leg).
- Wire 7. Connect Arduino Uno GND to red LED cathode (C, short leg).
- Wire 8. Connect Arduino Uno GND to yellow LED cathode (C, short leg).
- Wire 9. Connect Arduino Uno GND to green LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Watch the sequence, then draw the same machine in the FSM Synthesizer and compare.
Arduino C++ code
// Traffic light: a state machine in code. RED -> GREEN -> YELLOW -> RED.
// Draw the same machine in the FSM Synthesizer and compare.
const int RED = 13, YELLOW = 12, GREEN = 11;
void show(int r, int y, int g) {
digitalWrite(RED, r);
digitalWrite(YELLOW, y);
digitalWrite(GREEN, g);
}
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("RED"); show(HIGH, LOW, LOW); delay(3000);
Serial.println("GREEN"); show(LOW, LOW, HIGH); delay(2500);
Serial.println("YELLOW"); show(LOW, HIGH, LOW); delay(800);
}
What to try
Watch the sequence, then draw the same machine in the FSM Synthesizer and compare.
Frequently asked questions
How do I wire the Traffic Light project on an Arduino Uno?
Make 9 connections: Arduino Uno pin 13 to 220 Ω resistor (rR) leg 1; Arduino Uno pin 12 to 220 Ω resistor (rY) leg 1; Arduino Uno pin 11 to 220 Ω resistor (rG) leg 1; 220 Ω resistor (rR) leg 2 to red LED anode (A, long leg); 220 Ω resistor (rY) leg 2 to yellow LED anode (A, long leg); 220 Ω resistor (rG) leg 2 to green LED anode (A, long leg); Arduino Uno GND to red LED cathode (C, short leg); Arduino Uno GND to yellow LED cathode (C, short leg); Arduino Uno GND to green LED cathode (C, short leg).
What parts do I need for this Arduino project?
An Arduino Uno and three 220 Ω resistors, a red LED, a yellow LED and a green LED.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
Three LEDs on pins 13, 12 and 11 cycling red → green → yellow: a state machine in code.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator