🚦 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
| Component | Quantity | Description |
|---|---|---|
| Arduino Nano | 1 | Main microcontroller board |
| Red LED | 1 | Represents STOP signal |
| Yellow LED | 1 | Represents READY / WAIT signal |
| Green LED | 1 | Represents GO signal |
| 220Ω Resistors | 3 | Used in series with LEDs to limit current |
| Jumper Wires | Several | For making connections |
| Breadboard | 1 | For easy prototyping |
🧩 Circuit Connection (Text Description)
| LED Color | Arduino Nano Pin | Connection Details |
|---|---|---|
| Red LED | A3 | Anode (+) → A3 through 220Ω resistor, Cathode (–) → GND |
| Yellow LED | A4 | Anode (+) → A4 through 220Ω resistor, Cathode (–) → GND |
| Green LED | A5 | Anode (+) → 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
- Pin Setup:
Pins A3, A4, and A5 are configured as outputs to control the Red, Yellow, and Green LEDs respectively. - Traffic Light States:
The program uses anenum(enumeration) with three states —GREEN,YELLOW, andRED.
This structure makes it easy to switch between signals logically. - Non-blocking Timing:
Instead of usingdelay(), the program usesmillis()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. - 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.
- 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
Pingback: Arduino Demo Kit Projects - Motion Demo Kit (With Arduino) - Project List - EDGENext Learning