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:
- ESP32 Development Board
- LEDs (Red, Yellow, Green)
- Resistors (220 ohms or 330 ohms)
- Breadboard and Jumper wires
- Micro-USB Cable to connect the ESP32 to the computer
- Power Supply for the ESP32 (if not using USB)
Circuit Design:
- Red LED – Connect the anode (long leg) to GPIO 23 and cathode (short leg) to GND through a 220-ohm resistor.
- Yellow LED – Connect the anode to GPIO 22 and cathode to GND through a 220-ohm resistor.
- 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:
- Install the ESP32 board support in the Arduino IDE by going to File → Preferences and adding
https://dl.espressif.com/dl/package_esp32_index.json
in the “Additional Boards Manager URLs” section. Then go to Tools → Boards → Boards Manager and search forESP32
, then install it. - Select your ESP32 board from Tools → Board → 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:
- 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. - 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.
- The red light stays on for 5 seconds (
Uploading the Code:
- Select your ESP32 board and port from Tools.
- Click the Upload button (right arrow) in the Arduino IDE.
- Wait for the code to compile and upload to the ESP32.
- Once uploaded, the traffic lights will start operating according to the cycle.
Optional Enhancements:
- Manual Control: You can add a button to manually switch between red, yellow, and green lights using interrupts or simple logic.
- 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.