Traffic Light System using Arduino Nano

🚦 Traffic Light System using Arduino Nano

🔰 Project Overview

This beginner-friendly project simulates a real traffic light system using three LEDs — Red, Yellow, and Green — connected to an Arduino Nano. The program controls the LEDs in sequence to mimic how actual traffic lights operate at road intersections.

This is a great way to learn about:

  • Digital output control
  • Timing with millis()
  • State-based logic programming (non-blocking code)

⚙️ Components Required

ComponentQuantityDescription
Arduino Nano1Main microcontroller board
Red LED1Represents STOP signal
Yellow LED1Represents READY / WAIT signal
Green LED1Represents GO signal
220Ω Resistors3Used in series with LEDs to limit current
Jumper WiresSeveralFor making connections
Breadboard1For easy prototyping

🧩 Circuit Connection (Text Description)

LED ColorArduino Nano PinConnection Details
Red LEDA3Anode (+) → A3 through 220Ω resistor, Cathode (–) → GND
Yellow LEDA4Anode (+) → A4 through 220Ω resistor, Cathode (–) → GND
Green LEDA5Anode (+) → A5 through 220Ω resistor, Cathode (–) → GND

🔌 Power Connections:

  • Arduino 5V pin → powers the circuit.
  • Arduino GND pin → connects to all LED cathodes.

🪛 Wiring Summary (Quick View)

A3 → 220Ω → RED LED → GND
A4 → 220Ω → YELLOW LED → GND
A5 → 220Ω → GREEN LED → GND

💻 Arduino Code

// 🚦 Traffic Light System using Arduino Nano
// Pins: A3 (Red), A4 (Yellow), A5 (Green)

#define RED_PIN    A3
#define YELLOW_PIN A4
#define GREEN_PIN  A5

// Timings in milliseconds
const unsigned long GREEN_TIME  = 5000;  // 5 seconds
const unsigned long YELLOW_TIME = 2000;  // 2 seconds
const unsigned long RED_TIME    = 5000;  // 5 seconds

// Define traffic light states
enum State { GREEN, YELLOW, RED };
State currentState = GREEN;

unsigned long stateStart = 0;

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);

  currentState = GREEN;
  stateStart = millis();
  updateOutputs();
}

void loop() {
  unsigned long now = millis();
  unsigned long elapsed = now - stateStart;

  switch (currentState) {
    case GREEN:
      if (elapsed >= GREEN_TIME) {
        currentState = YELLOW;
        stateStart = now;
        updateOutputs();
      }
      break;

    case YELLOW:
      if (elapsed >= YELLOW_TIME) {
        currentState = RED;
        stateStart = now;
        updateOutputs();
      }
      break;

    case RED:
      if (elapsed >= RED_TIME) {
        currentState = GREEN;
        stateStart = now;
        updateOutputs();
      }
      break;
  }

  // You can add other code here — no blocking!
}

void updateOutputs() {
  switch (currentState) {
    case GREEN:
      digitalWrite(GREEN_PIN, HIGH);
      digitalWrite(YELLOW_PIN, LOW);
      digitalWrite(RED_PIN, LOW);
      break;
    case YELLOW:
      digitalWrite(GREEN_PIN, LOW);
      digitalWrite(YELLOW_PIN, HIGH);
      digitalWrite(RED_PIN, LOW);
      break;
    case RED:
      digitalWrite(GREEN_PIN, LOW);
      digitalWrite(YELLOW_PIN, LOW);
      digitalWrite(RED_PIN, HIGH);
      break;
  }
}

🧠 Code Explanation

  1. Pin Setup:
    Pins A3, A4, and A5 are configured as outputs to control the Red, Yellow, and Green LEDs respectively.
  2. Traffic Light States:
    The program uses an enum (enumeration) with three states — GREEN, YELLOW, and RED.
    This structure makes it easy to switch between signals logically.
  3. Non-blocking Timing:
    Instead of using delay(), the program uses millis() to track how long each light stays ON.
    This allows the Arduino to remain responsive — you can later add sensors or buttons without freezing the loop.
  4. updateOutputs() Function:
    Based on the current state, it turns ON only one LED at a time.
    • When GREEN: only the green LED is ON.
    • When YELLOW: only yellow is ON.
    • When RED: only red is ON.
  5. Timing Cycle:
    • Green stays ON for 5 seconds
    • Yellow stays ON for 2 seconds
    • Red stays ON for 5 seconds
      The loop continues indefinitely in this order — Green → Yellow → Red → Green.

🔄 Output Preview

🟢 Green ON → Wait 5 sec
🟡 Yellow ON → Wait 2 sec
🔴 Red ON → Wait 5 sec
…and the cycle repeats continuously!


💬 Project Summary

This simple Traffic Light Project demonstrates how you can control multiple LEDs in sequence using Arduino. It introduces you to:

  • Timing functions (millis())
  • State management
  • Real-world logic simulation

This Post Has One Comment

Leave a Reply