Sensor-triggered traffic light using a finite state machine and non-blocking timing.
An HC-SR04 ultrasonic sensor watches for approaching objects. When triggered, a full traffic light sequence runs: red hold → green → yellow → cooldown. Built using a finite state machine with non-blocking timing via millis().
- Arduino Uno
- 1x HC-SR04 Ultrasonic Sensor
- 1x Red LED
- 1x Yellow LED
- 1x Green LED
- 3x 220Ω resistors
- Breadboard + jumper wires
| Component | Arduino Pin |
|---|---|
| Red LED (+) | Pin 13 |
| Yellow LED (+) | Pin 11 |
| Green LED (+) | Pin 8 |
| All LED (-) | GND via 220Ω resistors |
| HC-SR04 Trig | Pin 12 |
| HC-SR04 Echo | Pin 10 |
| HC-SR04 VCC | 5V |
| HC-SR04 GND | GND |
| State | Behavior | Duration |
|---|---|---|
| IDLE_RED | Red on, waiting for sensor | Until triggered |
| RED_DELAY | Red holds before changing | 3 seconds |
| GREEN | Green on | 5 seconds |
| YELLOW | Yellow on | 2 seconds |
| COOLDOWN_RED | Red on before resetting to idle | 5 seconds |
- Non-blocking timing with
millis() - Finite state machine design with
enum - Ultrasonic sensor driving multi-output logic
- Modular helper functions