An IoT-based Smart Traffic Violation Detection System using ESP32 microcontroller with Cloudflare cloud backend, Telegram photo alerts, and a live global dashboard. Built on a budget of under ₹6,000. Real-world deployable.
With curiosity, code, and determination — a 13-year-old proved that age is no barrier to building real-world IoT solutions.
1.5 lakh lives lost every year. Most of it preventable. Existing solutions are either too expensive or don't exist at all.
India accounts for the highest number of road accident fatalities in the world. Overspeeding, signal jumping, and wrong-way driving are the top three causes — all detectable by sensors.
The government spends approximately ₹40 lakh per speed camera installation. At this rate, covering even 1% of India's 63 lakh km road network is economically impossible.
Existing systems detect only one violation type per device. Speed camera, red light camera, and emission sensor are all separate — requiring separate infrastructure investment.
Traffic police cannot be present at every junction 24×7. Human monitoring is inconsistent, prone to bias, and completely absent in rural and remote areas where most accidents occur.
One pocket-sized circuit board costing under ₹6,000 that monitors a road 24 hours a day, automatically detects 8 categories of violations, photographs offenders, maintains a digital challan log, streams live data to a global cloud dashboard, and runs completely off-grid on solar power — all simultaneously, without any human intervention.
Two ESP32 microcontrollers working in tandem — one as the intelligent brain, one as the eyes connected to the cloud.
The main brain of the system. Handles all sensing, decision-making, traffic logic, and cloud data push. Runs the full embedded C++ firmware at 240MHz.
Dedicated camera and cloud uplink board. Communicates with Board 01 via Serial (UART). Handles photography with intelligent flash control.
IR Beam 1 is broken. ESP32 records timestamp t1 and immediately commands ESP32-CAM to capture a photo and hold it in RAM.
When IR Beam 2 is broken, Speed = (0.175m ÷ elapsed time) × 3.6 = km/h. If speed exceeds limit, overspeed challan issued.
If Beam 2 breaks before Beam 1, vehicle is travelling in reverse (wrong way). A wrong-way violation and photo alert are triggered instantly.
If a second vehicle passes within 1 second of the previous one, a tailgating challan is automatically generated for unsafe following distance.
The system always knows the exact signal state. If a vehicle passes while trafficState == RED, a Red Light Jump challan is generated.
Violation found → ESP32-CAM sends the RAM-held photo to Telegram with violation label. No violation detected → photo silently discarded, no spam.
Every 2 seconds, all live data is pushed to Cloudflare D1 database via HTTPS. Dashboard at margrakshak.pages.dev updates automatically.
Noise (sound sensor), air quality (MQ gas sensor), and ambient light (LDR for streetlight) are sampled every 20ms regardless of vehicle detection.
A global edge computing backend that makes the dashboard accessible from anywhere in the world — not just on a local WiFi.
The system uses a Cloudflare Worker deployed at the network edge. The ESP32 pushes JSON sensor data every 2 seconds. The Worker stores it in a Cloudflare D1 SQLite database. The live dashboard fetches this data from anywhere in the world.
Photo uploads from ESP32-CAM go through the same Worker as multipart/form-data. The Worker forwards photos to Telegram, extracts the direct URL, and stores it in D1 — making violation photos visible on the dashboard from anywhere.
seconds_offline calculationFrom the moment a vehicle enters the sensor zone to the challan appearing on the cloud dashboard — everything is automated.
Two IR beam sensors placed exactly 17.5 cm apart measure precise crossing time. Speed = distance ÷ time × 3.6 km/h. Automatic challan + buzzer alert + Telegram photo on violation.
Direction is identified by which IR sensor triggers first. If a vehicle travels in reverse (illegal) direction, a wrong-way violation is logged with a Telegram photo alert instantly.
The system knows the exact signal state at all times. Any vehicle crossing the IR sensors during RED phase automatically generates a Red Light Jump challan with photo evidence.
If a second vehicle passes the sensor point within 1 second of the previous vehicle, it is flagged for dangerously small following distance — a challan is generated automatically.
Green (15s) → Yellow (3s) → Red (10s) → Yellow (3s) → Green. A live countdown timer is shown on the dashboard, with smart sync to compensate for network transmission delay.
A sound sensor continuously monitors ambient noise. When loud horns or excessive noise above 85 dB equivalent are detected in a 500ms window, an alert triggers on the dashboard.
MQ-series gas sensor measures pollutant concentration with 3-minute warm-up calibration. Dashboard shows AQI as GOOD / MODERATE / POOR with colour-coded badge and live bar graph.
An LDR constantly checks ambient light levels. When darkness is detected (night-time), the streetlight LED switches on automatically. Turns off at dawn — zero manual intervention.
When LDR detects night conditions, ESP32-CAM activates its built-in flash LED for exactly 50 milliseconds during photo capture — ensuring clear, usable evidence images in darkness.
Every challan-worthy violation triggers the ESP32-CAM to send a photo message to a Telegram bot with a labelled caption (e.g., "VIOLATION: OVERSPEED"). Reaches officer's phone in seconds.
Camera captures photo at vehicle detection, holds in RAM. Decision: violation found → send via Telegram. No violation → silently discard. No false alerts, no photo spam to Telegram.
Full mobile-responsive dashboard at margrakshak.pages.dev/live — accessible from any device anywhere in the world. Updates every 2.5 seconds via Cloudflare D1 polling.
Every violation receives a unique challan number (starting at #1001) with violation description. Last 5 challans always visible on cloud dashboard. Full log accessible via full-screen modal.
Non-blocking 3-beep buzzer pattern sounds immediately on every challan-worthy violation. Uses state-machine buzzer logic so vehicle detection is never interrupted during beeping.
A 128×64 OLED screen (I2C) shows live speed, signal state, vehicle count, active alert message, latest challan number, and Cloudflare connection status — updated every loop cycle.
Pressing a dedicated button triggers a built-in demo that cycles through all 8 violation types automatically — ideal for science exhibitions, STEM fairs, and classroom demonstrations.
6 solar panels (6V/900mAh total) + 18650 Li-ion battery (2600mAh) + power bank module provide 5V regulated output. Granular power control via 3 switches. Completely off-grid capable.
Every component under ₹6,000 total. Designed to be replicable by any school, panchayat, or municipality at scale.
| Component | Qty | Function |
|---|---|---|
| ESP32 Dev Module | 1 | Main brain — reads all sensors, controls traffic light, pushes data to Cloudflare D1 every 2 seconds |
| ESP32-CAM (AI Thinker) | 1 | Camera module — captures violation photos, sends to Telegram Bot API via WiFiClientSecure |
| IR Obstacle Sensor | 2 | Speed detection (17.5cm apart) and direction detection for wrong-way identification |
| MQ Gas Sensor (MQ-2/135) | 1 | Vehicle emission monitoring, AQI classification (Good / Moderate / Poor) |
| Sound Sensor Module | 1 | Noise / horn pollution detection — triggers alert above 85 dB equivalent threshold |
| LDR (Light Sensor) | 1 | Ambient light detection — auto-controls smart streetlight and triggers night mode for camera |
| OLED Display 128×64 (I2C) | 1 | On-device live display of speed, signal state, violations, alerts, and cloud status |
| Red LED | 1 | Traffic light — RED signal |
| Yellow LED | 1 | Traffic light — YELLOW signal (transition state) |
| Green LED | 1 | Traffic light — GREEN signal |
| White LED (Street) | 1 | Smart streetlight — auto-activates at night via LDR reading |
| Active Buzzer | 1 | Audio alert — 3-beep non-blocking pattern on every challan-worthy violation |
| Push Button | 1 | Triggers 8-step automated Demo Mode for science exhibitions and STEM fairs |
| Breadboard + Jumper Wires | Set | Prototyping connections between all components |
| Miniature Road Model (Foam Board) | 1 | Physical road prototype base with road markings, plants, and miniature vehicles for demo |
| Solar Panel (6V / 150mAh)★ NEW | 6 | Power generation — 6 panels in parallel = 6V / 900mAh total. Charges 18650 battery via power bank module |
| 18650 Li-Ion Battery (3.7V, 2600mAh)★ NEW | 1 | Energy storage — stores solar energy, powers entire node for extended hours without sunlight |
| Power Bank Module (No Display)★ NEW | 1 | Power management — accepts 6V solar input, charges 18650 cell, boosts output to clean 5V DC |
| Toggle Switch★ NEW | 3 | SW1=Master (all power), SW2=Gas Sensor branch, SW3=ESP32-CAM branch (for power management) |
A fully self-sustaining three-tier power system — generate, store, distribute. No electrical infrastructure required anywhere.
| SW1 — Master Switch | First after power bank output. Cuts ALL power to the entire system. |
| SW2 — Gas Sensor | Independent branch. Disable MQ sensor to save power in clean-air deployments. |
| SW3 — ESP32-CAM | Independent branch. Disable camera when Telegram alerts are not needed. |
What makes MargRakshak genuinely different from everything else out there.
Government speed cameras cost ~₹40 lakh each. MargRakshak delivers more features at 0.015% of that cost. Scalable to thousands of units.
Overspeed, wrong way, red light jump, tailgating, noise, emission, streetlight, photo evidence — all in a single unit. No other system matches this integration.
No more "local WiFi only" limitation. Live data accessible from any phone, anywhere in the world via margrakshak.pages.dev — powered by Cloudflare's global edge network.
Every challan is backed by an automatic timestamped photo sent directly to Telegram. Reduces disputes, eliminates corruption, creates an auditable digital trail.
Prepare → Send → Discard logic means zero false Telegram alerts. The camera only fires when a violation is confirmed — not on every vehicle passing.
6 solar panels + 18650 battery + power bank module. Deploy anywhere — highway, rural junction, remote area — with absolutely no electrical infrastructure required.
MargRakshak is a proof-of-concept today. Here is the roadmap for a full-scale national deployment.
Upgrade to a larger panel and 10,000mAh battery pack for 72-hour autonomous operation even on cloudy days.
Upgrade to higher-resolution camera + TensorFlow Lite model for automatic reading of vehicle registration plates — enabling direct digital challans.
Link multiple nodes using LoRa radio modules or 4G SIM, creating a city-wide sensor mesh reporting to a central police dashboard.
Add ultrasonic or vibration sensor to detect road surface damage and automatically alert municipal corporations for repair.
A second IR beam across the footpath detects jaywalking and warns pedestrians before they step onto the road.
A sound frequency analyser detects ambulance sirens and automatically switches the traffic light to green — clearing the path for emergency vehicles.
Connect to the live cloud dashboard and see real-time data from the physical prototype — speed readings, traffic signal timer, violation counts, and Telegram photo alerts all streaming live.
▶ OPEN LIVE DASHBOARDMargRakshak proves that affordable, open-source hardware can solve a real national problem. Every year, thousands of lives are lost on Indian roads due to violations that a small sensor node can detect in milliseconds. With the addition of a solar power system, Li-ion battery storage, modular switch control, and a Cloudflare cloud backend, MargRakshak is now fully off-grid capable and globally accessible — ready for real-world deployment on highways, rural roads, and remote junctions with zero infrastructure required.
With government support and manufacturing at scale, thousands of MargRakshak nodes could be deployed across India at a fraction of the current cost — saving lives and reducing traffic chaos simultaneously.