Traffic Light
Red, yellow, green LEDs cycling like a traffic light.
- Beginner
- LED & Lighting
- 8 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
Three LEDs, each with its own resistor, are wired to pins 11 (red), 12 (yellow) and 13 (green), and all three cathodes go to ground. Each LED lights when its pin is high, so the sketch controls the light by deciding which of the three pins is on at any moment.
loop() runs one full cycle of the lights. First it turns green on (pin 13) and the other two off, for three seconds. Then yellow for one second, then red for three. Each stage also prints its message (“GO - Green”, “CAUTION - Yellow”, “STOP - Red”) to the Serial tab. Only one LED is ever on at a time, because every stage sets all three pins explicitly.
Parts
- Arduino Uno
- 3 × Resistor
- 3 × LED
- GND
Wiring
- Arduino Uno D11 → Resistor 1 A
- Resistor 1 B → LED 1 Anode (+)
- LED 1 Cathode (-) → GND
- Arduino Uno D12 → Resistor 2 A
- Resistor 2 B → LED 2 Anode (+)
- LED 2 Cathode (-) → GND
- Arduino Uno D13 → Resistor 3 A
- Resistor 3 B → LED 3 Anode (+)
- LED 3 Cathode (-) → GND
Arduino code
// Traffic Light Controller
void setup() {
pinMode(11, OUTPUT); // Red
pinMode(12, OUTPUT); // Yellow
pinMode(13, OUTPUT); // Green
Serial.begin(9600);
}
void loop() {
// Green
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
Serial.println("GO - Green");
delay(3000);
// Yellow
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
Serial.println("CAUTION - Yellow");
delay(1000);
// Red
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
Serial.println("STOP - Red");
delay(3000);
}
Try changing it
- Give the green light longer: change the first delay(3000) to delay(5000).
- Real lights show red and yellow together just before green. Add a one-second stage that turns pins 11 and 12 on together before the green stage.
Next challenge
More projects in LED & Lighting
- LED BlinkerClassic Arduino blink: LED on pin 13 with resistor to GND.
- Breadboard BlinkerThe same blinking LED, built the way you would on a real breadboard: parts pushed into…
- RGB Mood LightRGB LED cycling through colors using PWM.
- Potentiometer LED DimmerTurn the knob to control LED brightness with PWM.