DIY ELECTRONICSTR

← All projects

Traffic Light

Beginner20 min

A real traffic light with three LEDs: red, red + yellow, green and yellow light up in the order used in Turkey, and each stage is also written to the Serial Monitor. It teaches driving several outputs in turn, timing with digitalWrite and delay, and gathering a repeated job into its own function. You can change how long each light stays on with the constants in the code. No extra library needed.

Build it step by step in the app

What you need

Wiring steps

  1. 1

    Connect the common ground (GND)

    Connect the common ground (GND)

    Plug a black jumper wire into the GND pin next to pin 13 on the Arduino's digital header. Connect its other end to the breadboard's top blue (−) rail. All three LEDs' resistors will go into this rail, so they share one ground.

    • Arduino GND → the top blue (−) rail (black wire)
  2. 2

    Add the red LED and its resistor

    Add the red LED and its resistor

    Plug the red LED's long leg (+) into column 11 in the top half of the breadboard and its short leg (−) into column 12. Put one end of a 220 Ω resistor in column 12 and the other end in the top (−) rail. Then connect the Arduino's pin 11 to column 11 with a red jumper wire (RED_PIN 11). The resistor limits the current through the LED; without it both the LED and the Arduino's pin can be damaged.

    • red LED long leg (+) → column 11, top half
    • red LED short leg (−) → column 12, top half
    • red LED's resistor (leg 1) → column 12, top half
    • red LED's resistor (leg 2) → the top blue (−) rail
    • Arduino pin 11 → column 11, top half (red wire)
  3. 3

    Add the yellow LED and its resistor

    Add the yellow LED and its resistor

    Plug the yellow LED's long leg (+) into column 14 in the top half and its short leg (−) into column 15. Put the second 220 Ω resistor between column 15 and the top (−) rail. Connect the Arduino's pin 10 to column 14 with a yellow jumper wire (YELLOW_PIN 10).

    • yellow LED long leg (+) → column 14, top half
    • yellow LED short leg (−) → column 15, top half
    • yellow LED's resistor (leg 1) → column 15, top half
    • yellow LED's resistor (leg 2) → the top blue (−) rail
    • Arduino pin 10 → column 14, top half (yellow wire)
  4. 4

    Add the green LED and its resistor

    Add the green LED and its resistor

    Plug the green LED's long leg (+) into column 17 in the top half and its short leg (−) into column 18. Put the third 220 Ω resistor between column 18 and the top (−) rail. Connect the Arduino's pin 9 to column 17 with a green jumper wire (GREEN_PIN 9). That completes the circuit.

    • green LED long leg (+) → column 17, top half
    • green LED short leg (−) → column 18, top half
    • green LED's resistor (leg 1) → column 18, top half
    • green LED's resistor (leg 2) → the top blue (−) rail
    • Arduino pin 9 → column 17, top half (green wire)

Arduino sketch

// The traffic light's pins
const int RED_PIN = 11;
const int YELLOW_PIN = 10;
const int GREEN_PIN = 9;

// How long each light stays on (ms)
const unsigned long RED_TIME = 5000;
const unsigned long YELLOW_TIME = 2000;
const unsigned long GREEN_TIME = 5000;

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  Serial.begin(9600);
}

// Sets all three lights: HIGH is on
void lights(int red, int yellow, int green) {
  digitalWrite(RED_PIN, red);
  digitalWrite(YELLOW_PIN, yellow);
  digitalWrite(GREEN_PIN, green);
}

void loop() {
  // Stop: red only
  lights(HIGH, LOW, LOW);
  Serial.println("Red: stop");
  delay(RED_TIME);

  // Get ready: red and yellow together
  lights(HIGH, HIGH, LOW);
  Serial.println("Red + yellow: get ready");
  delay(YELLOW_TIME);

  // Go: green only
  lights(LOW, LOW, HIGH);
  Serial.println("Green: go");
  delay(GREEN_TIME);

  // Caution: yellow only
  lights(LOW, HIGH, LOW);
  Serial.println("Yellow: caution, stopping");
  delay(YELLOW_TIME);
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
Red: stop
Red + yellow: get ready
Green: go
Yellow: caution, stopping
Red: stop
Red + yellow: get ready

What you will see

The red LED lights for 5 seconds, then red and yellow light together for 2 seconds, then green alone for 5 seconds, and finally yellow alone for 2 seconds before the cycle starts again. If an LED never lights, first check its legs (the long one goes to the Arduino's pin) and that its resistor sits in the (−) rail.

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Build it step by step in the app