Project 2: Arduino Traffic Lights

Building working traffic lights with Arduino

10/13/20263 min read

Introduction

Have you ever noticed how traffic lights control vehicles on roads? Red means stop, yellow means prepare to stop, and green means go.

In this project, you will build your own miniature traffic light system using an Arduino Uno and three LEDs. You will learn how to control multiple lights, set different time intervals, and understand how real-world traffic signals work.

This project is suitable for beginners and builds on the skills you learned in Project 1: Blinking LED.

What You Will Learn:

  • How to control multiple LEDs using Arduino.

  • How to use different Arduino pins for different components.

  • How to control the timing of lights.

  • How to use digitalWrite() and delay().

  • How traffic lights follow a repeating sequence.

1. Components Required

You will need the following components from your Arduino robotics kit:

  • 1 Arduino Uno

  • 1 breadboard

  • 1 red LED

  • 1 yellow LED

  • 1 green LED

  • 3 resistors of 220 ohms each

  • Jumper wires

  • 1 USB cable to connect the Arduino to your computer

Important: Each LED needs its own 220-ohm resistor to limit the current and protect the LED.

2. Understanding the Traffic Light System

A traffic light normally uses three colors:

Red Light: STOP

Vehicles must stop and wait.

Yellow Light: PREPARE TO STOP

Vehicles should prepare to stop as the signal changes.

Green Light: GO

Vehicles may proceed when it is safe to do so.

In our Arduino project, the lights will follow this sequence:

  1. Red LED turns on for 5 seconds.

  2. Green LED turns on for 5 seconds.

  3. Yellow LED turns on for 2 seconds.

  4. The sequence repeats automatically.

This is a simplified educational model of a traffic signal.

3. Connecting Components

Before uploading the code, connect the LEDs to your Arduino.

Step 1: Place the LEDs

Insert the red, yellow, and green LEDs into the breadboard.

Remember:

  • The long leg of an LED is usually the positive leg, called the anode.

  • The short leg is usually the negative leg, called the cathode.

Step 2: Connect the Red LED

  1. Connect Arduino digital pin 8 to one end of a 220-ohm resistor.

  2. Connect the other end of the resistor to the long leg of the red LED.

  3. Connect the short leg of the red LED to Arduino GND.

Step 3: Connect the Yellow LED

  1. Connect Arduino digital pin 9 to one end of another 220-ohm resistor.

  2. Connect the other end of the resistor to the long leg of the yellow LED.

  3. Connect the short leg of the yellow LED to Arduino GND.

Step 4: Connect the Green LED

  1. Connect Arduino digital pin 10 to one end of the third 220-ohm resistor.

  2. Connect the other end of the resistor to the long leg of the green LED.

  3. Connect the short leg of the green LED to Arduino GND.

IMPORTANT
All three LEDs should be connected to the same ground (GND) connection of Arduino

Double-check your connections before connecting the Arduino to your computer.

[CIRCUIT DIAGRAM]

4. Writing the Arduino Code

Now that the circuit is ready, let's program the Arduino to control the traffic lights.

Open the Arduino IDE

Copy and paste the following code into your sketch.

5. Understanding the Code

Let's understand the main parts of the program.

A. Defining the LED Pins

These lines give names to the Arduino pins connected to the LEDs.

  • redLED represents pin 8.

  • yellowLED represents pin 9.

  • greenLED represents pin 10.

Using names makes the code easier to understand and edit.

The word const means these pin numbers will remain fixed while the program runs.

B. Setting the Pins as Outputs

The setup() function runs once when the Arduino starts or resets.

The pinMode() function tells the Arduino how each pin will be used.

We use OUTPUT because the Arduino needs to send electrical signals to turn the LEDs on and off.

C. Turning the Red Light On

The digitalWrite() function controls whether a digital output pin is HIGH or LOW.

  • HIGH turns the connected LED on.

  • LOW turns the connected LED off.

The delay(5000) command pauses the program for 5,000 milliseconds, which equals 5 seconds.

This keeps the red LED on for 5 seconds.

D. Turning the Green Light On

First, the red LED turns off. The yellow LED stays off, and the green LED turns on.

The Arduino then waits for 5 seconds before moving to the next part of the program.

E. Turning the Yellow Light On

The yellow LED turns on while the red and green LEDs remain off.

The delay(2000) command keeps the yellow light on for 2 seconds.

F. Repeating the Sequence

The loop() function repeats continuously while the Arduino is powered.

When the program reaches the end of the loop, it starts again from the beginning. That is why your traffic light keeps changing automatically without needing you to press anything.

Congratulations! You have built your own Arduino traffic light system.

Through this project, you learned how to:

  • Control three LEDs using different Arduino pins.

  • Use resistors to protect LEDs.

  • Turn components on and off using digitalWrite().

  • Control timing using delay().

  • Repeat a sequence using the loop() function.

  • Apply programming logic to a real-world example.

These skills will help you build more advanced robotics projects in the future.