Sharjah, UAE GST
Home/ Projects/ Lesson 2: Traffic Light
Lesson 2 of 7 Basics Beginner ◎ 25 min

Lesson 2: Traffic Light

Three LEDs run the red → red + yellow → green → yellow cycle of a real traffic light. You will control several outputs and write your own function.

Arduino UNO Sensor & Output Starter Kit - LEDs, Buzzer, Servo, Ultrasonic & LCD
One of 7 projects in the Arduino UNO Sensor & Output Starter Kit - LEDs, Buzzer, Servo, Ultras… — the parts below are from that kit.

Before you start

Time
25 min
Do these first
On your own
You can do this lesson on your own.

Exact parts

PartHow many
UNO R3 + USB cable1
400-point breadboard1
Red, yellow and green LED1 each
220 Ω resistors (red-red-brown)3
Jumper wiresabout 8

Not in the box

Nothing extra - just the kit and your computer.

What you build and learn

  • Controlling several outputs from one sketch
  • Timing with delay(): 1000 ms = 1 second
  • Writing your own function (showLights)
  • Reading messages in the Serial Monitor

Each LED has its own pin: red on 2, yellow on 3, green on 4. HIGH turns an LED on and LOW turns it off.

showLights() switches all three LEDs at once, prints the name of the phase and waits. loop() calls it four times, one full cycle, and then starts again.

The order is the one many countries use: red (stop), red + yellow (get ready), green (go), yellow (prepare to stop).

New words

Function
A named block of code you can run as often as you like, such as showLights(...).
Millisecond (ms)
One thousandth of a second. delay(1000) waits 1 second.
Serial Monitor
A window in the Arduino IDE (Tools → Serial Monitor) that shows the text the UNO sends.

What this project uses

Every item is in the kit — nothing extra to buy.

Build this with

Arduino UNO Sensor & Output Starter Kit - LEDs, Buzzer, Servo, Ultrasonic & LCD

All 10 parts in one box, with this project and 6 more to build from them.

AED 85.00 See the kit
Need only these parts? Order them loose · from AED 87.65
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 87.65

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. red/yellow/green LEDs: included in the kit, ask us for it loose. Delivery is AED 20, free on orders AED 100+.

Connections

Unplug USB before you change any wire. Pin names are the labels printed on the board.

Lesson 2: Traffic Light - connection diagram with labelled pins
Connection diagram, drawn from this lesson's pin plan - not a photo of a tested build. Tap to open it full size. The table below says the same thing in words.
Part / pinConnect toNote
Red LED long leg (+)220 Ω → UNO pin 2
Yellow LED long leg (+)220 Ω → UNO pin 3Already wired in Lesson 1
Green LED long leg (+)220 Ω → UNO pin 4
All three short legs (−)− rail (GND)
UNO 5V / GND+ rail / − railAlready wired in Lesson 1
PotentiometerCan stay as in Lesson 1Not used in this lesson

Step by step

One thing at a time. Tick nothing off until it matches the diagram.

  1. Unplug the USB cable.Watch out: Always unplug the USB cable before you add, move or remove a wire.
  2. Leave the yellow LED and its resistor on pin 3 from Lesson 1.
  3. Add the red LED and a 220 Ω resistor the same way, and wire the resistor's free end to UNO pin 2.Watch out: Long leg towards the resistor.
  4. Add the green LED and a 220 Ω resistor, wired to UNO pin 4.Watch out: Long leg towards the resistor.
  5. Connect each LED's short leg to the − rail.
  6. Check the three LEDs against the diagram, plug in USB, copy the Lesson 2 code into the IDE and upload it.
  7. Open Tools → Serial Monitor at 9600 baud to see the name of each phase.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

lesson2_traffic_light.ino arduino
/*
  DIY KIT - Arduino UNO Sensor & Output Starter Kit
  Lesson 2 of 7: Traffic Light

  WIRING (unplug the USB cable before you wire)
    Keep the yellow LED from Lesson 1 on pin 3 and add two more:
    Red LED long leg (+)    -> 220 ohm resistor -> UNO pin 2
    Yellow LED long leg (+) -> 220 ohm resistor -> UNO pin 3
    Green LED long leg (+)  -> 220 ohm resistor -> UNO pin 4
    Every short leg (-)     -> GND rail of the breadboard
    The potentiometer from Lesson 1 can stay where it is.

  WHAT SHOULD HAPPEN
    Red (3 s) -> red + yellow (1 s) -> green (3 s) -> yellow (1.2 s) -> repeat.
*/

const int RED_PIN    = 2;
const int YELLOW_PIN = 3;
const int GREEN_PIN  = 4;

// How long each light stays on, in milliseconds (1000 ms = 1 second).
const unsigned long RED_MS        = 3000;  // stop
const unsigned long RED_YELLOW_MS = 1000;  // get ready
const unsigned long GREEN_MS      = 3000;  // go
const unsigned long YELLOW_MS     = 1200;  // prepare to stop

// Switch the three lights, tell the Serial Monitor, then wait.
// true = on, false = off.
void showLights(bool red, bool yellow, bool green, const char *name, unsigned long holdMs) {
  digitalWrite(RED_PIN, red ? HIGH : LOW);
  digitalWrite(YELLOW_PIN, yellow ? HIGH : LOW);
  digitalWrite(GREEN_PIN, green ? HIGH : LOW);
  Serial.println(name);
  delay(holdMs);
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Lesson 2: Traffic Light");
}

void loop() {
  showLights(true,  false, false, "RED",          RED_MS);
  showLights(true,  true,  false, "RED + YELLOW", RED_YELLOW_MS);
  showLights(false, false, true,  "GREEN",        GREEN_MS);
  showLights(false, true,  false, "YELLOW",       YELLOW_MS);
}
ℹCompiler-checked for Arduino UNO (0 warnings) and run in the kit simulator. Pins match the lesson diagram. Not yet bench-tested on the shipped parts.

Upload it

  1. Click Copy (or Download .ino and open the file).
  2. In the Arduino IDE choose File → New Sketch, select everything in it and paste.
  3. Check the bottom of the window says Arduino Uno and your port (Start here, step 5).
  4. Click Upload (the → arrow) and wait for Done uploading.

How the code works

In the codeWhat it does
const int RED_PIN = 2; ...Names for the three LED pins.
const unsigned long RED_MS = 3000;How long each phase lasts, in milliseconds.
showLights(red, yellow, green, name, holdMs)Our own function: true means on, false means off. It also prints the phase name and waits holdMs.
loop()Four calls to showLights(): one full traffic-light cycle.

Change it and see

Change: Change GREEN_MS from 3000 to 6000.What happens: The green light now stays on for 6 seconds.
Change: Add a fifth line in loop(): showLights(false, false, false, "ALL OFF", 500);What happens: All the lights go dark for half a second at the end of each cycle.

What you should see or hear

  • Red for 3 s, red + yellow for 1 s, green for 3 s, yellow for 1.2 s, then it repeats.
  • Only the lights named in the Serial Monitor are on.

If it doesn't work

ProblemWhat to check
One LED never lightsUnplug USB and turn that LED round (long leg towards the resistor). Check its wire goes to the right pin.
The wrong colour lights at the wrong timeTwo pin wires are swapped. Red must be on 2, yellow on 3, green on 4.
All the LEDs stay offCheck the − rail is connected to a UNO GND pin.
Nothing in the Serial MonitorSet the speed in the Serial Monitor to 9600 baud.

Small experiment

Use a stopwatch (or a phone) to time the green phase. Is it really 3 seconds? Change GREEN_MS and time it again.

Check what you learned

How many milliseconds are there in 2.5 seconds?

2500.

What does showLights(false, true, false, ...) switch on?

Only the yellow LED.

Why does the cycle start again after yellow?

Because loop() runs over and over for ever.

▶In the simulator: The simulator shows the same lights with the same timing. Real LEDs only light if they are the right way round - the simulator does not check that.

Next

  • Lesson 3 adds the buzzer on pin 6. Keep the LEDs where they are.

Stuck?

Most problems are one wire in the wrong hole. Check these first:

  1. Unplug, then compare every wire with the lesson's diagram and table. One pin off is the most common problem.
  2. LEDs: is the long leg on the resistor side?
  3. Is the − rail connected to a UNO GND pin, and the + rail to 5V?
  4. Upload problems: is Tools → Board set to Arduino Uno and Tools → Port to your UNO? (Start here, step 5.)
  5. Read the lesson's "If it doesn't work" table and find the line that matches what you see.
Still stuck? Ask us on WhatsApp. Send this message with a photo of your wiring - it tells us exactly where you are.
Hi DIY KIT, I'm stuck on the Arduino UNO Sensor & Output Starter Kit.
Lesson: __
Step number: __
What should happen: __
What happens instead: __
Error message (copied exactly): __
I'll attach a photo of my wiring.
Message us on WhatsApp

More from this kit

All projects →

Bot
ECHO
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.