How To Build A Traffic Light System With Arduino

Creating a traffic signal project with Arduino is a great way to learn about controlling LEDs and using timers to simulate a real-world system. Here’s a step-by-step guide to building a simple traffic light using an Arduino:

Components Needed:

  • Arduino Uno (or any compatible board)
  • Red LEDs
  • Yellow LEDs
  • Green LEDs
  • 220Ω Resistors
  • Breadboard
  • Jumper Wires

Wire Connection

  1. Red LED → Connect to pin 13 (through a 220Ω resistor)
  2. Yellow LED → Connect to pin 12 (through a 220Ω resistor)
  3. Green LED → Connect to pin 11 (through a 220Ω resistor)
  4. Resistors → Place 220Ω resistors in series with each LED to limit current.
  5. Connect the common cathode (short leg of LEDs) to the ground (GND) of Arduino.

Basic Operation:

  • The sequence should mimic a traffic light:
    • Red → Green → Yellow → Red
    • Each light should stay on for a defined time.

Arduino Code:

int redPin = 13;
int yellowPin = 12;
int greenPin = 11;

void setup() {
  // Set the LED pins as output
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  // Red light on
  digitalWrite(redPin, HIGH);
  digitalWrite(yellowPin, LOW); 
  digitalWrite(greenPin, LOW);  
  delay(5000); 
  
  // Green light on
  digitalWrite(redPin, LOW);  
  digitalWrite(yellowPin, LOW); 
  digitalWrite(greenPin, HIGH);
  delay(5000);
  
  // Yellow light on
  digitalWrite(redPin, LOW);   
  digitalWrite(yellowPin, HIGH); 
  digitalWrite(greenPin, LOW);  
  delay(2000); 
}

Explanation of Code:

  1. Pin Setup:
  • We define the pins connected to the red, yellow, and green LEDs.
  • In the setup() function, we set these pins as outputs.

2. Traffic Light Sequence in loop():

  • Red: The red light stays on for 5 seconds (delay(5000)).
  • Green: The green light stays on for 5 seconds.
  • Yellow: The yellow light stays on for 2 seconds to indicate the transition.

3. Delays: The delay function is used to keep each light on for a specified time in milliseconds.

How to Test:

  • Upload the code to your Arduino using the Arduino IDE.
  • Connect your LEDs to the appropriate pins on the Arduino board.
  • Once the code uploads, the LEDs should turn on in the sequence: Red → Green → Yellow → Red, and repeat.

Conclusion:

This project demonstrates a basic traffic light control system using an Arduino and LEDs. You can extend the functionality by adding more features such as sensors, additional lanes, or a pedestrian crossing button to make the system more dynamic and interactive.