PinoutSearch

LED Traffic Light (Arduino)

Cycle red, yellow, and green LEDs like a traffic light. Great practice for digital outputs and timing.

Beginner20 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

traffic_light.ino
// Cycle three LEDs like a traffic light.
const int RED = 2, YELLOW = 3, GREEN = 4;

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);
}

void loop() {
  digitalWrite(RED, HIGH);    delay(3000); digitalWrite(RED, LOW);     // stop
  digitalWrite(GREEN, HIGH);  delay(3000); digitalWrite(GREEN, LOW);   // go
  digitalWrite(YELLOW, HIGH); delay(1000); digitalWrite(YELLOW, LOW);  // slow
}

Steps

  1. Wire each LED from its pin (D2/D3/D4) through a 220Ω resistor; all cathodes to GND.
  2. Red = D2, Yellow = D3, Green = D4.
  3. Upload the sketch and watch the cycle.
  4. Tweak the delay values to change the timing.