Traffic Light Simulator
This project emulates a standard traffic light sequence using three colored LEDs (Red, Yellow, Green). The ESP32 controls the LEDs in a repeating cycle:
Green for 5 seconds → vehicles go
Yellow for 2 seconds → warning to stop
Red for 5 seconds → vehicles stop
The timing is managed using non-blocking or blocking delays (blocking used here for simplicity), creating an automatic, continuous loop.
All components are built into your trainer kit—no external wiring needed:
ESP32 DevKit (main controller)
LED1 (Green) – from CN7
LED2 (Yellow) – from CN7
LED3 (Red) – from CN7
✅ Confirmed from schematic: CN7 = 4 digital output LEDs connected to GPIO 16, 17, 18, 19.
// main.c - Traffic Light Simulator for ESP32 Trainer Kit
// Uses onboard LEDs: LED1=Green, LED2=Yellow, LED3=Red
#define GREEN_PIN 16
#define YELLOW_PIN 17
#define RED_PIN 18
void setup() {
// Configure LED pins as outputs
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
// Ensure all LEDs start OFF
digitalWrite(GREEN_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(RED_PIN, LOW);
}
void loop() {
// Green ON → Go
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(RED_PIN, LOW);
delay(5000); // 5 seconds
// Yellow ON → Prepare to stop
digitalWrite(GREEN_PIN, LOW);
digitalWrite(YELLOW_PIN, HIGH);
digitalWrite(RED_PIN, LOW);
delay(2000); // 2 seconds
// Red ON → Stop
digitalWrite(GREEN_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(RED_PIN, HIGH);
delay(5000); // 5 seconds
}
On power-up or reset, the ESP32 initializes GPIO 16–18 as outputs.
All LEDs are turned OFF initially.
The program enters an infinite loop:
Turns Green LED ON for 5 seconds → simulates "GO"
Switches to Yellow LED for 2 seconds → "CAUTION"
Switches to Red LED for 5 seconds → "STOP"
The cycle repeats automatically with no user input required.
Transitions are instantaneous (no fade or overlap).
Green LED lights steadily for 5 seconds, then turns off.
Yellow LED lights for 2 seconds, then turns off.
Red LED lights for 5 seconds, then turns off.
Sequence repeats indefinitely: Green → Yellow → Red → Green → …
No display, sound, or serial output — visual feedback only via onboard LEDs.
Correct Pin Mapping:
Based on your schematic (Skematik Trainer Kit IoT ESP32.pdf), CN7 connects:
LED1 → GPIO 16
LED2 → GPIO 17
LED3 → GPIO 18
→ Your #define values match exactly.
Clear State Logic:
Only one LED is ON at a time — no ambiguity or overlap.
Realistic Timing:
Green: 5s (standard for go)
Yellow: 2s (adequate warning)
Red: 5s (allows cross-traffic or pedestrian phase)
Safe Initialization:
All LEDs start OFF to avoid unexpected states on boot.
Simple & Readable:
Easy for students to understand — ideal for educational use.
CN7 section shows 4 LEDs labeled LED1–LED4.
Connected to ESP32 DevKit pins D16, D17, D18, D19 (as per CN2 header mapping).
Each has a 330Ω resistor → safe for direct GPIO drive.
You can enhance engagement by asking students to:
Modify timings (e.g., longer green for “main road”)
Add a pedestrian button (using CN6 SW1) to extend red
Simulate amber flash at night