Build your own Arduino Traffic light system
Ever wanted to control traffic from your desk? Building a miniature traffic light system is not only a fun Arduino project but also a perfect introduction to programming state machines and working with LEDs and timers.
In this tutorial, we'll create a realistic traffic light that cycles through red, yellow, and green lights with proper timing intervals. Let's get started!
What You'll Need
- Arduino Uno or Nano
- Breadboard
- 3 LEDs (red, yellow, green)
- 3 resistors (220Ω)
- Jumper wires
- USB cable for Arduino
- Arduino IDE installed on your computer
The Circuit Diagram
Before we start coding, let's set up our hardware. Here's how to connect everything:
- Connect the Arduino's GND to the breadboard's ground rail
- Connect each LED's cathode (shorter leg) to ground
- Connect each LED's anode (longer leg) to a 220Ω resistor
- Connect the other end of each resistor to the following Arduino pins:
- Red LED → Pin 10
- Yellow LED → Pin 9
- Green LED → Pin 8
The Basic Traffic Light Code
Now let's write our traffic light program:
// Define pins for each traffic light
const int RED_PIN = 10;
const int YELLOW_PIN = 9;
const int GREEN_PIN = 8;
// Define timing constants (in milliseconds)
const int RED_TIME = 5000; // Red light stays on for 5 seconds
const int YELLOW_TIME = 2000; // Yellow light stays on for 2 seconds
const int GREEN_TIME = 5000; // Green light stays on for 5 seconds
void setup() {
// Initialize all LED pins as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
// Initially turn all LEDs off
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
// Start serial communication for debugging
Serial.begin(9600);
Serial.println("Traffic Light System Initialized");
}
void loop() {
// Red light phase
Serial.println("Red light ON");
digitalWrite(RED_PIN, HIGH);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
delay(RED_TIME);
// Green light phase
Serial.println("Green light ON");
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
delay(GREEN_TIME);
// Yellow light phase
Serial.println("Yellow light ON");
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
delay(YELLOW_TIME);
}
Uploading the Code
- Connect your Arduino to your computer via USB
- Open the Arduino IDE
- Copy the code above and paste it into a new sketch
- Select your Arduino board type from Tools → Board
- Select the correct COM port from Tools → Port
- Click the upload button (right arrow icon)
Once uploaded, your traffic light should start cycling through the red, green, and yellow phases automatically!
Understanding the Code
Let's break down what the code does:
Constants and Setup
First, we define our pin numbers and timing constants. Then in the setup()
function, we:
- Configure all three pins as outputs
- Turn all LEDs off initially
- Start serial communication for debugging
The Main Loop
Our loop()
function runs continuously and follows this sequence:
- Turn on the red light for 5 seconds
- Switch to green light for 5 seconds
- Switch to yellow light for 2 seconds
- Repeat from step 1
Each phase turns on just one light while ensuring the others are off. The delay()
function pauses execution for the specified milliseconds before moving to the next step.
Advanced Traffic Management Approaches
The basic timer-based approach works well for consistent traffic conditions, but real-world traffic varies. Here are some beginner-friendly concepts for more intelligent traffic management:
1. Sensor-Based Adaptive Timing
The Concept:
- Add simple IR (infrared) or ultrasonic sensors to detect vehicles at each intersection approach
- When sensors detect more vehicles on one road than another, extend the green light time for the busier road
- Use threshold values to determine "light" vs "heavy" traffic conditions
How to Start:
- Place IR sensors behind each traffic light to detect the presence of vehicles
- Connect these sensors to analog pins on your Arduino
- Create simple rules: If sensor A detects many vehicles but sensor B detects few, give more green time to the direction covered by sensor A
2. Time-of-Day Patterns
The Concept:
- Different times of day have predictable traffic patterns (morning rush to work, evening rush home)
- Program your traffic light to automatically adjust timings based on the time of day
- Implement a real-time clock module to track the current time
How to Start:
- Add an RTC (Real-Time Clock) module to your Arduino
- Define time periods (e.g., 7-9 AM, 4-6 PM as rush hours)
- Program longer green lights for the main road during morning rush hour
- Adjust to favor the opposite direction during evening rush hour