Smart Traffic System - Low-Cost Adaptive Signals
Adaptive traffic signals built on cheap ultrasonic sensors instead of cameras and heavy computing. Sensors on each road count vehicles and detect how far queues reach, a mathematical model turns the readings into flow rate and queue length, and a reinforcement-learning agent running on an ESP32 at each crossing sets the green-light timing. In a Unity simulation of the hardware setup, average waiting time for stopped vehicles fell from 89 to 63 seconds (29%) compared with fixed-time signals.
Overview
Most adaptive traffic systems watch the road with cameras and run computer vision on powerful hardware. That works, but it is expensive to install at every crossing in a city.
This project asked a simpler question: can cheap ultrasonic sensors measure traffic well enough to time the signals? We built the sensing and control on real hardware, then recreated the whole setup in a Unity simulation to measure the effect on waiting time.
- Where: academic research project, MIT Academy of Engineering, Pune
- Advisor: Dr. Sunita Barve
- When: July 2021 to December 2022
How the Sensors Work
Each road into the crossing gets one sensor unit:
- An ultrasonic sensor sits on the road divider, 30 to 40 m before the signal.
- It points across the lanes at a reflector plate on the far kerb, so the beam spans the road.
- Every vehicle that passes breaks the beam, and each break is counted.
- When cars wait at a red light, the queue grows backwards. Once it reaches the sensor, a car stands in the beam and the reading stays steady. That tells the controller the queue is now at least 30 to 40 m long.
A four-way crossing therefore has four sensors, one per road, all wired to the same ESP32 microcontroller that acts as the crossing's brain.
From Readings to Green Lights
The ESP32 at each crossing works in two steps:
- Mathematical model. A model we derived turns raw beam breaks and steady readings into the flow rate and queue length for every road.
- Reinforcement learning. These numbers, together with the current signal state, form the agent's state. Its action is which road gets the green light and for how long. It is rewarded for reducing the time vehicles spend waiting, so over time it learns timing that keeps queues short.
Fixed-time signals follow a preset cycle no matter what is happening on the road. This controller gives more green to the roads that need it right now.
A Controller at Every Crossing
Many smart-city designs connect every signal to one central online system. If that server or the network fails, the whole city's signals are affected.
Here, each crossing runs its own controller on a low-cost ESP32, using only its own sensors. The city network becomes a set of independent decision-makers: if one crossing goes offline, only that crossing falls back to normal timing, and the rest keep adapting.
Simulation in Unity
To test the system at city scale, we recreated the hardware setup in Unity (C#) with realistic traffic:
- Vehicles arrive at random intervals, so traffic is uneven, as it is in a real city
- Sensor zones on each road behave like the ultrasonic units: they count passing vehicles and detect queues that reach them
- Several crossing types: a four-way junction, and a highway crossing where side roads only get green when their queue builds up
- Live readouts of traffic rate and average traffic rate at each crossing
The simulation code is on GitHub.
Inside the Simulation
The clips and screenshots below are recorded from the Unity project. Each crossing tests a different idea. The overlay in the top corners is the simulation's own debug readout: RawData is a car's distance from the sensor it is standing in, and SensorTime is how long it has been there.
Highway crossing: side roads get green only when a queue builds

By default the side road gets nothing. The highway keeps both directions on green and the sign over the junction reads "No-stop HighWay".
When cars stop on the side road, the lead car pulls up to the signal and the car queued behind it stands on the sensor, the same way a real queue reaches back to the ultrasonic unit. The sensor starts timing. After 5 seconds it starts blinking, the highway turns red, and the side road gets a 10-second green with a countdown on the sign.


Four-way junction: green time split by weight
At the second crossing every road gets a turn, but not an equal one. Each approach, A to D, has a slider that sets its share of the cycle, and the signs above the junction show each road's green time in seconds. A multiplying factor speeds the whole cycle up or down.


With equal weights each road gets the same slice. Setting the sliders to A 100, B 20, C 50 and D 30 re-splits the cycle straight away: A gets 10 seconds of green, B gets 2, C gets 5 and D gets 3.
Highway queue meter
The third crossing measures a queue instead of reacting to single cars. Cars arrive at random intervals and stack up behind the signal. When the tail of the queue reaches the sensor, the signal turns green and releases the queue, and the sign records how long the queue took to build, the traffic rate and the running average.


Results
Compared with fixed-time signals, average waiting time for vehicles stopped at a signal fell from 89 to 63 seconds, a 29% reduction, in the simulation.
The result supports the main idea of the project: low-cost sensors, placed well and read by a learning controller, can capture enough about traffic to make signals noticeably better, without cameras or heavy computing at every crossing.
Technologies
- Hardware: an ESP32 microcontroller at each crossing as the controller
- Sensing: ultrasonic sensors with reflector plates, one unit per road
- Control: a derived mathematical model of flow rate and queue length, and a reinforcement-learning agent for signal timing
- Simulation: Unity (C#), with randomised vehicle arrivals and several crossing layouts
- Code: Smart-Traffic-System-Unity on GitHub (simulation)