How to Build a Smart Traffic Signal System with ESP32: Step-by-Step Guide for Beginners

Creating a traffic signal project using the ESP32 involves controlling LEDs that simulate the traffic light signals (red, yellow, green) based on a timed sequence. Below is a step-by-step guide for the traffic signal project using the ESP32:

Components Needed:

  1. ESP32 Development Board
  2. LEDs (Red, Yellow, Green)
  3. Resistors (220 ohms or 330 ohms)
  4. Breadboard and Jumper wires
  5. Micro-USB Cable to connect the ESP32 to the computer
  6. Power Supply for the ESP32 (if not using USB)

Circuit Design:

  1. Red LED – Connect the anode (long leg) to GPIO 23 and cathode (short leg) to GND through a 220-ohm resistor.
  2. Yellow LED – Connect the anode to GPIO 22 and cathode to GND through a 220-ohm resistor.
  3. Green LED – Connect the anode to GPIO 21 and cathode to GND through a 220-ohm resistor.

Code:

You can use the Arduino IDE to program the ESP32. The code below simulates a simple traffic light system:

  1. Install the ESP32 board support in the Arduino IDE by going to FilePreferences and adding https://dl.espressif.com/dl/package_esp32_index.json in the “Additional Boards Manager URLs” section. Then go to ToolsBoardsBoards Manager and search for ESP32, then install it.
  2. Select your ESP32 board from ToolsBoard → Choose the specific ESP32 board.

Arduino Code:

// Define GPIO pins for LEDs
const int redLED = 23;
const int yellowLED = 22;
const int greenLED = 21;

void setup() {
  // Set LED pins as output
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
}

void loop() {
  // Red light on, others off
  digitalWrite(redLED, HIGH);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(5000); // Red light for 5 seconds
  
  // Yellow light on, others off
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, HIGH);
  digitalWrite(greenLED, LOW);
  delay(2000); // Yellow light for 2 seconds
  
  // Green light on, others off
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(greenLED, HIGH);
  delay(5000); // Green light for 5 seconds

  // Repeat the cycle
}

Explanation of the Code:

  1. Pin Setup: The LEDs are connected to the ESP32 GPIO pins 21, 22, and 23, and they are set as output pins in the setup() function.
  2. Traffic Light Cycle:
    • The red light stays on for 5 seconds (delay(5000)).
    • The yellow light stays on for 2 seconds (delay(2000)).
    • The green light stays on for 5 seconds (delay(5000)).
    • The cycle repeats indefinitely.

Uploading the Code:

  1. Select your ESP32 board and port from Tools.
  2. Click the Upload button (right arrow) in the Arduino IDE.
  3. Wait for the code to compile and upload to the ESP32.
  4. Once uploaded, the traffic lights will start operating according to the cycle.

Optional Enhancements:

  1. Manual Control: You can add a button to manually switch between red, yellow, and green lights using interrupts or simple logic.
  2. Real-Time Control via Wi-Fi: The ESP32 has Wi-Fi capabilities, so you could control the traffic light system via a web page or app.

This simple traffic signal project using the ESP32 simulates a basic traffic light control system. You can extend this with additional features like traffic sensors, timers, or even a web interface to manage the lights remotely.