Illustration, from above: an LED traffic light at a play-mat crossroads, red lit, with toy cars waiting.Lights and displays
Illustration (made with AI): roughly what the finished project looks like.

A Traffic Light with Three LEDs

Build a small model of a real traffic light: green, flashing green, yellow, red and finally red with yellow, just like on the street.

Difficulty
Beginner
Time
30 min
Age
8+
Parts
4, 4 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Place the LEDs

    On the breadboard, place the red, yellow and green LEDs from top to bottom. The LED's long leg is “+”, the short one is “−”.

  2. Wire them to the pins

    Connect each LED's long leg through a 330 Ω resistor: red to pin 4, yellow to pin 3, green to pin 2. Take the short legs to the breadboard's ground rail and connect the rail to the board's GND.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Red LED + → 330 ΩArduino D4
    • Red LED − → Arduino GND
    • Yellow LED + → 330 ΩArduino D3
    • Yellow LED − → Arduino GND
    • Green LED + → 330 ΩArduino D2
    • Green LED − → Arduino GND
  3. Upload the code

    The lights() function sets all three LEDs at once, and loop() goes through the phases one after another.

    Arduino C++
    const int RED = 4, YELLOW = 3, GREEN = 2;
    
    void setup() {
      pinMode(RED, OUTPUT);
      pinMode(YELLOW, OUTPUT);
      pinMode(GREEN, OUTPUT);
    }
    
    void lights(bool red, bool yellow, bool green) {
      digitalWrite(RED, red ? HIGH : LOW);
      digitalWrite(YELLOW, yellow ? HIGH : LOW);
      digitalWrite(GREEN, green ? HIGH : LOW);
    }
    
    void loop() {
      lights(false, false, true);   delay(5000);   // green
      for (int i = 0; i < 3; i++) {                // green flashes: about to change
        lights(false, false, false);  delay(500);
        lights(false, false, true);   delay(500);
      }
      lights(false, true, false);   delay(2000);   // yellow
      lights(true, false, false);   delay(5000);   // red
      lights(true, true, false);    delay(1500);   // red + yellow: get ready
    }
  4. Change the timing

    delay() takes milliseconds: 5000 is five seconds. Try keeping the red on longer than the green.

    Illustration: red, yellow and green LEDs as a traffic light on a breadboard, the green one lit, a toy car in front.

    Illustration, made with AI

  • Lights and displays
    Beginner45 min10+

    Pedestrian Crossing with a Button

    Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.

    6/6 parts on the site
  • Games
    Beginner45 min10+

    A Two-Player Reaction Game

    An LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.

    6/6 parts on the site
  • Lights and displays
    Beginner15 min8+

    Your First Program: Blink an LED

    Everyone starts here: the LED built into the board turns on and off once a second. You'll learn how to install the Arduino IDE, write code and upload it to the board.

    1/1 parts on the site