sentinel-arduino-firmware
PublicLoading…
SENTINEL — Adaptive Traffic-Light FSM v1.0
A deterministic, fully offline traffic-light controller running on the Arduino UNO R3 (ATmega328P). SENTINEL manages a 3-LED traffic junction, a multiplexed 7-segment countdown display, a pedestrian crossing system, and an automated night-mode vehicle detection pipeline — all in a single non-blocking loop with no external libraries.
Features
FSM Core
- Three-state machine: RED (30s) → YELLOW (5s) → GREEN (25s) → YELLOW (5s) → repeat
- End-of-phase warning blink on RED (600ms period) and GREEN (300ms period) during the final 3 seconds
- 7-segment countdown display driven through a 74HC595 shift register, multiplexed at 2ms intervals across two active digits — no
delay()in the display path
Pedestrian Crossing
- TTP223 capacitive touch sensor triggers a 10-second safe-passage window
- If triggered with 15 seconds or less remaining in RED: activates immediately
- If triggered with more than 15 seconds remaining: queues for the next RED phase
- One crossing per RED cycle — subsequent touches in the same cycle are ignored
- Buzzer pulses at 880 Hz (double-beep, 600ms interval) during crossing, escalating to 1050 Hz (single-beep, 300ms interval) in the final 3 seconds
Night Mode
- LDR sampled every 500ms on A0 via a 1kΩ voltage divider
- Night mode arms when LDR ADC reading drops below 50
- After 15 seconds into a RED phase, HC-SR04 begins scanning every 60ms
- If a vehicle is detected within 20 cm continuously for 2 seconds: RED warning blink (200ms period, 2s duration), then immediate GREEN at a shortened 10-second duration
- Night RED is also shortened to 25 seconds on the following cycle
Serial Diagnostics (9600 baud)
| Command | Effect |
|---|---|
L / l | Print current LDR ADC value, threshold, and DAY/NIGHT status |
D / d | Print last sonar distance reading in cm |
S / s | Full FSM snapshot: state, elapsed time, remaining time, night mode status, pedestrian flags |
Hardware Requirements
| Component | Quantity | Notes |
|---|---|---|
| Arduino UNO R3 | 1 | ATmega328P, 5V logic |
| Red, Yellow, Green LED (5mm) | 1 each | Standard diffused, Vf 2.0–2.2V |
| 220Ω resistor | 3 | Current limiting, one per traffic LED |
| Active buzzer (5V) | 1 | Built-in oscillator, draws ~30mA — must be transistor-driven |
| 2N2222 NPN transistor | 1 | Buzzer driver — keeps MCU pin current at ~5mA |
| 1kΩ resistor | 2 | 2N2222 base resistor + LDR voltage divider pull-down |
| 10kΩ resistor | 1 | 2N2222 base pull-down, keeps buzzer off at boot |
| TTP223 touch sensor module | 1 | Capacitive, 2.5–5.5V, clean HIGH/LOW output |
| 74HC595 shift register | 1 | 8-bit serial-to-parallel, drives segment lines |
| 4-digit 7-segment display | 1 | Common Anode or Common Cathode — verify before wiring, set flag in firmware |
| HC-SR04 ultrasonic sensor | 1 | 5V supply, 5V ECHO output |
| LDR (photoresistor) | 1 | Ambient light sensing |
| 100nF ceramic capacitor | 1 | Decoupling cap on 74HC595 VCC pin |
Wiring
Full pin-level connection map is in WIRING.md.
| Arduino Pin | Connected to |
|---|---|
| D2 | Red LED (via 220Ω) |
| D3 | Yellow LED (via 220Ω) |
| D4 | Green LED (via 220Ω) |
| D5 | 2N2222 base (via 1kΩ) — drives buzzer |
| D6 | TTP223 SIG (touch sensor) |
| D7 | 74HC595 SRCLK (shift clock) |
| D8 | 74HC595 RCLK (latch clock) |
| D9 | 74HC595 SER (serial data) |
| D10 | 7-segment DIG4 (ones digit) |
| D11 | 7-segment DIG3 (tens digit) |
| D12–D13 | 7-segment DIG1–DIG2 (driven HIGH, unused) |
| A0 | LDR signal (voltage divider mid-point) |
| A1 (D15) | HC-SR04 TRIG |
| A2 (D16) | HC-SR04 ECHO |
Note on A1/A2: the ATmega328P analog pins double as digital I/O (D15/D16). Using them here frees all of D0–D13 for the LED, buzzer, shift register, and digit lines.
Note on CC/CA: before wiring the 7-segment display, confirm whether it is Common Cathode or Common Anode using a coin cell test. Set the
#define COMMON_CATHODEflag at the top ofsentinel.inoaccordingly. Wiring the wrong type will produce no output.
PCB Design
A custom PCB was designed in KiCad, integrating all components onto a single board: Arduino UNO R3 footprint, 74HC595 shift register, 4-digit 7-segment display, 2N2222 buzzer driver circuit, traffic LEDs with current-limiting resistors, custom-designed TTP223 touch sensor footprint, LDR voltage divider, and HC-SR04 connector pads. KiCad project files are in hardware/.
Software Setup
1. Prerequisites
- Arduino IDE 2.x
- No external libraries required — the sketch uses only Arduino built-ins (
tone(),pulseIn(),millis())
2. Configure Display Type
At the top of sentinel.ino, set the flag matching your display:
#define COMMON_CATHODE true // true for CC, false for CA
3. Flash the Board
- Open
firmware/sentinel/sentinel.inoin Arduino IDE - Select
Tools → Board → Arduino Uno - Select the correct port under
Tools → Port - Click Upload
On boot, SENTINEL runs a hardware self-test: all three LEDs fire simultaneously, both active display digits show 8, and the buzzer chirps once. The system then enters STATE_RED and the countdown begins.
How It Works
SENTINEL uses a single non-blocking loop() — no delay() calls in the main execution path. Every timed action is gated by millis() interval checks, keeping the MCU free to service sensors, inputs, and the display multiplexer on every tick.
Cycle diagram:
RED (30s) ──► YELLOW (5s) ──► GREEN (25s) ──► YELLOW (5s) ──► RED ...
│ │
│ [touch, <=15s remaining] │ [touch while GREEN]
▼ ▼
PED WINDOW (10s) queued for next RED
│
▼
back to RED remainder
RED (night, >=15s elapsed + car at <=20cm for 2s)
│
▼
NIGHT WARNING (200ms blink, 2s)
│
▼
GREEN NIGHT (10s) ──► YELLOW (5s) ──► RED NIGHT (25s) ...
Night mode substate machine:
| Substate | Condition to advance |
|---|---|
NIGHT_IDLE | 15 seconds elapsed in current RED phase |
NIGHT_POLLING | Vehicle within 20 cm for 2 continuous seconds |
NIGHT_WARNING | 2-second warning blink completes |
NIGHT_DONE | Locked out for the remainder of the current RED cycle |
Timing Reference
| Parameter | Value |
|---|---|
| RED duration (day) | 30s |
| RED duration (night, post-override) | 25s |
| GREEN duration (day) | 25s |
| GREEN duration (night override) | 10s |
| YELLOW duration | 5s |
| Pedestrian window | 10s |
| End-of-phase warning blink | last 3s |
| Night mode sonar arm delay | 15s into RED |
| Vehicle confirmation hold | 2s continuous |
| Sonar poll interval | 60ms |
| LDR sample interval | 500ms |
| Display mux interval | 2ms |
License
MIT License — see LICENSE for full terms.
No comments yet. Be the first to ask about this board.