Smart Density Based Traffic Light Control System with ESP32 & Blynk
Density Based 4 Way Traffic Light Controller Project using ESP32 & Ultrasonic Sensor HC-SR04 with Light Monitoring on IoT Blynk App with Code
- Suitable forB.Tech/Diploma ESP32/IoT students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Overview
In this project, you’ll learn how to create a
Density Based 4 way Traffic Light Controller project
using an
ESP32
microcontroller and
ultrasonic sensors
, with integration into the
Blynk IoT
platform for real-time monitoring and control. Earlier we made
Simple Traffic Light Controller
using Raspberry Pi Pico.
The primary goal of this project is to create a
smart traffic management system
that optimizes signal timing based on the
density of traffic
. Rather than following a fixed timing schedule, this system dynamically adjusts the
signal changes
, ensuring that if no vehicles are present at a signal, it will be skipped in favor of the next one, thereby reducing unnecessary waiting times and
improving traffic flow
At the heart of the system lies the
ESP32
, a powerful and versatile microcontroller that reads data from
HC-SR04 ultrasonic sensors
placed at each signal. These sensors measure the distance to the nearest object, providing a reliable estimation of
vehicle presence
and
proximity
at each traffic signal. Based on the Ultrasonic Sensor data, the light signalling functions. Additionally, this system utilizes the
Blynk IoT platform
to offer a user-friendly interface that displays the system status.
Bill of Materials
The following are the components required for making Smart Density Based Traffic Light Controller Project with ESP32. You can purchase all the components online from given links.
S.N.
Components
Quantity
Purchase Links
1
ESP32 WiFi Module
1
Amazon
AliExpress
SunFounder
2
Ultrasonic Sensor HC-SR04
4
Amazon
AliExpress
SunFounder
3
Red LED
4
Amazon
AliExpress
SunFounder
4
Yellow LED
4
Amazon
AliExpress
SunFounder
5
Green LED
4
Amazon
AliExpress
SunFounder
6
Resistor 220-ohm
12
Amazon
AliExpress
SunFounder
7
Connecting Wires
30
Amazon
AliExpress
SunFounder
8
Micro-USB Cable
1
Amazon
AliExpress
9
Breadboard
1
Amazon
AliExpress
SunFounder
Algorithm & Working of the Project
The following
flowchart
shows the
algorithm
used for making a Smart Density Based Traffic Light Controller Project.
The working of project is divided into three steps:
Normal Operation with Traffic at All Signals
: When traffic is detected at each of the four signals, the system operates in a standard cycle. It sequentially controls each traffic signal, allowing for a regulated flow of vehicles. The system transitions from one signal to the next, ensuring that each direction is given a green light in turn.
Adaptive Skipping of Signals Without Traffic
: The system actively monitors the presence of vehicles at each signal using ultrasonic sensors. If it detects an absence of traffic at a particular signal, the system will adaptively skip this signal to reduce unnecessary waiting times. For instance, if it’s currently allowing traffic at signal 1 and detects no vehicles waiting at signals 2 and 3, it will proceed directly to signal 4 after completing the cycle at signal 1.
Idle State During Absence of Traffic
: If the ultrasonic sensors do not detect traffic at any of the four signals, the system enters a standby mode at the current signal. It will remain in this state until vehicles are detected at any signal. Once traffic is detected, the system resumes its operation, starting the cycle with the signal where traffic was identified.
Circuit Digram & Connection
Let us take a look at the circuit diagram of “
Density Based Traffic Light Controller project using ESP32
“. The circuit is designed using the frtizing software.
In the diagram, each connection is
color-coded
, representing different connections, ensuring clarity and aiding in the setup and troubleshooting of the circuit.
The
ESP32
is the central processing unit that manages all components, including
traffic lights
and
ultrasonic sensors
. It is connected to a power source, Wi-Fi network, and other peripherals for complete control.
Each signal has three
LEDs
representing the
Red, Yellow,
and
Green lights
which are connected as follows.
Signal Number
Red LED
Yellow LED
Green LED
GND
Signal 1
GPIO 26
GPIO 25
GPIO 33
Connect to GND
Signal 2
GPIO 19
GPIO 18
GPIO 5
Connect to GND
Signal 3
GPIO 32
GPIO 3
GPIO 21
Connect to GND
Signal 4
GPIO 17
GPIO 16
GPIO 15
Connect to GND
There are
4 Ultrasonic sensors
utilized to measure the
traffic density
at each signal. Refer to
ESP32 & HC-SR04
interfacing guide to learn about Ultrasonic Sensor. The Ultrasonic Sensor
HC-SR04
is connected to ESP32 as follows.
Sensor Number
Trigger Pin
Echo Pin
VCC
GND
Sensor 1
GPIO 13
GPIO 12
Connect to 5V
Connect to GND
Sensor 2
GPIO 27
GPIO 14
Connect to 5V
Connect to GND
Sensor 3
GPIO 22
GPIO 23
Connect to 5V
Connect to GND
Sensor 4
GPIO 2
GPIO 4
Connect to 5V
Connect to GND
All the components are powered by a
5V supply
. Make sure that the ground of the power supply is connected to the ground of the ESP32.
You can use a pair of
breadboard
for assembly as ESP32 doesn’t fit into a single breadboard. Use smaller breadboard to fit two
ultrasonic sensors
in opposite directions. Also make sure that all components are correctly connected as per the diagram to ensure the proper functionality of the density based traffic light controller project.
Setting Up Blynk Application
Visit
blynk.cloud
and create a Blynk account on the Blynk website. Or you can simply sign in using the registered Email ID.
Click on
+New Template
Give any name to the Hardware such as “
Traffic Light Controller
“. Choose the Hardware type as
ESP32
and the connection type as
WiFi
Now we need to set up Datastreams. Therefore Click on
+New Datastreams
and Select
Virtual Pin
Create datastreams for
Red Light 1, Yellow Light 1
, and
Green Light
1
and assign the virtual pins as
V1, V2, and V3
respectively.
In the same way create data streams for
Red Light 2, Yellow Light 2
, and
Green Light
2 and assign the virtual pins as
V4, V5, and V6
respectively.
Do the same for
3rd Traffic Lights
and
4th Traffic Lights
. So the total number of data streams will be 12.
Now go to the Dashboard section and set up the dashboard.
For all the 4 Red LEDs, 4 Yellow LEDs, and 4 Green LEDs link all the widgets with the datastreams and click on save.
With the
free version
, you can only use
10 widgets
. If you want to monitor more than
12 data streams
you need to upgrade the app. In my case, I am using the free version. Therefore I am monitoring using only 3 traffic lights which will be 9 LEDs.
Go to the Home and click on Add Device. Name the Device “
Traffic Light Controller
” and Click on Create.
After the device is successfully created, you will get the
device ID
and
Authentication Token
. The Authentication Token will be used in the code.
Source Code/Program
The complete code for the Project “Density Based Traffic Light Controller with ESP32 & Blynk” is written in Arduino IDE.
You need to add
Blynk Library
to your Arduino IDE first.
In the code below, you need to change the WiFi SSID, Password and Blynk Authentication token from these lines. Replace it with your credentials.
1
2
3
char
auth
[
]
=
\"************************\"
;
char
ssid
[
]
=
\"************************\"
;
char
pass
[
]
=
\"************************\"
;
Here is the final code for the project. Copy it and paste it on your Arduino IDE editor. Make necessary changes in the code as mentioned aboved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
In this project, you’ll learn how to create a
Density Based 4 way Traffic Light Controller project
using an
ESP32
microcontroller and
ultrasonic sensors
, with integration into the
Blynk IoT
platform for real-time monitoring and control. Earlier we made
Simple Traffic Light Controller
using Raspberry Pi Pico.
The primary goal of this project is to create a
smart traffic management system
that optimizes signal timing based on the
density of traffic
. Rather than following a fixed timing schedule, this system dynamically adjusts the
signal changes
, ensuring that if no vehicles are present at a signal, it will be skipped in favor of the next one, thereby reducing unnecessary waiting times and
improving traffic flow
At the heart of the system lies the
ESP32
, a powerful and versatile microcontroller that reads data from
HC-SR04 ultrasonic sensors
placed at each signal. These sensors measure the distance to the nearest object, providing a reliable estimation of
vehicle presence
and
proximity
at each traffic signal. Based on the Ultrasonic Sensor data, the light signalling functions. Additionally, this system utilizes the
Blynk IoT platform
to offer a user-friendly interface that displays the system status.
Bill of Materials
The following are the components required for making Smart Density Based Traffic Light Controller Project with ESP32. You can purchase all the components online from given links.
S.N.
Components
Quantity
Purchase Links
1
ESP32 WiFi Module
1
Amazon
AliExpress
SunFounder
2
Ultrasonic Sensor HC-SR04
4
Amazon
AliExpress
SunFounder
3
Red LED
4
Amazon
AliExpress
SunFounder
4
Yellow LED
4
Amazon
AliExpress
SunFounder
5
Green LED
4
Amazon
AliExpress
SunFounder
6
Resistor 220-ohm
12
Amazon
AliExpress
SunFounder
7
Connecting Wires
30
Amazon
AliExpress
SunFounder
8
Micro-USB Cable
1
Amazon
AliExpress
9
Breadboard
1
Amazon
AliExpress
SunFounder
Algorithm & Working of the Project
The following
flowchart
shows the
algorithm
used for making a Smart Density Based Traffic Light Controller Project.
The working of project is divided into three steps:
Normal Operation with Traffic at All Signals
: When traffic is detected at each of the four signals, the system operates in a standard cycle. It sequentially controls each traffic signal, allowing for a regulated flow of vehicles. The system transitions from one signal to the next, ensuring that each direction is given a green light in turn.
Adaptive Skipping of Signals Without Traffic
: The system actively monitors the presence of vehicles at each signal using ultrasonic sensors. If it detects an absence of traffic at a particular signal, the system will adaptively skip this signal to reduce unnecessary waiting times. For instance, if it’s currently allowing traffic at signal 1 and detects no vehicles waiting at signals 2 and 3, it will proceed directly to signal 4 after completing the cycle at signal 1.
Idle State During Absence of Traffic
: If the ultrasonic sensors do not detect traffic at any of the four signals, the system enters a standby mode at the current signal. It will remain in this state until vehicles are detected at any signal. Once traffic is detected, the system resumes its operation, starting the cycle with the signal where traffic was identified.
Circuit Digram & Connection
Let us take a look at the circuit diagram of “
Density Based Traffic Light Controller project using ESP32
“. The circuit is designed using the frtizing software.
In the diagram, each connection is
color-coded
, representing different connections, ensuring clarity and aiding in the setup and troubleshooting of the circuit.
The
ESP32
is the central processing unit that manages all components, including
traffic lights
and
ultrasonic sensors
. It is connected to a power source, Wi-Fi network, and other peripherals for complete control.
Each signal has three
LEDs
representing the
Red, Yellow,
and
Green lights
which are connected as follows.
Signal Number
Red LED
Yellow LED
Green LED
GND
Signal 1
GPIO 26
GPIO 25
GPIO 33
Connect to GND
Signal 2
GPIO 19
GPIO 18
GPIO 5
Connect to GND
Signal 3
GPIO 32
GPIO 3
GPIO 21
Connect to GND
Signal 4
GPIO 17
GPIO 16
GPIO 15
Connect to GND
There are
4 Ultrasonic sensors
utilized to measure the
traffic density
at each signal. Refer to
ESP32 & HC-SR04
interfacing guide to learn about Ultrasonic Sensor. The Ultrasonic Sensor
HC-SR04
is connected to ESP32 as follows.
Sensor Number
Trigger Pin
Echo Pin
VCC
GND
Sensor 1
GPIO 13
GPIO 12
Connect to 5V
Connect to GND
Sensor 2
GPIO 27
GPIO 14
Connect to 5V
Connect to GND
Sensor 3
GPIO 22
GPIO 23
Connect to 5V
Connect to GND
Sensor 4
GPIO 2
GPIO 4
Connect to 5V
Connect to GND
All the components are powered by a
5V supply
. Make sure that the ground of the power supply is connected to the ground of the ESP32.
You can use a pair of
breadboard
for assembly as ESP32 doesn’t fit into a single breadboard. Use smaller breadboard to fit two
ultrasonic sensors
in opposite directions. Also make sure that all components are correctly connected as per the diagram to ensure the proper functionality of the density based traffic light controller project.
Setting Up Blynk Application
Visit
blynk.cloud
and create a Blynk account on the Blynk website. Or you can simply sign in using the registered Email ID.
Click on
+New Template
Give any name to the Hardware such as “
Traffic Light Controller
“. Choose the Hardware type as
ESP32
and the connection type as
WiFi
Now we need to set up Datastreams. Therefore Click on
+New Datastreams
and Select
Virtual Pin
Create datastreams for
Red Light 1, Yellow Light 1
, and
Green Light
1
and assign the virtual pins as
V1, V2, and V3
respectively.
In the same way create data streams for
Red Light 2, Yellow Light 2
, and
Green Light
2 and assign the virtual pins as
V4, V5, and V6
respectively.
Do the same for
3rd Traffic Lights
and
4th Traffic Lights
. So the total number of data streams will be 12.
Now go to the Dashboard section and set up the dashboard.
For all the 4 Red LEDs, 4 Yellow LEDs, and 4 Green LEDs link all the widgets with the datastreams and click on save.
With the
free version
, you can only use
10 widgets
. If you want to monitor more than
12 data streams
you need to upgrade the app. In my case, I am using the free version. Therefore I am monitoring using only 3 traffic lights which will be 9 LEDs.
Go to the Home and click on Add Device. Name the Device “
Traffic Light Controller
” and Click on Create.
After the device is successfully created, you will get the
device ID
and
Authentication Token
. The Authentication Token will be used in the code.
Source Code/Program
The complete code for the Project “Density Based Traffic Light Controller with ESP32 & Blynk” is written in Arduino IDE.
You need to add
Blynk Library
to your Arduino IDE first.
In the code below, you need to change the WiFi SSID, Password and Blynk Authentication token from these lines. Replace it with your credentials.
1
2
3
char
auth
[
]
=
\"************************\"
;
char
ssid
[
]
=
\"************************\"
;
char
pass
[
]
=
\"************************\"
;
Here is the final code for the project. Copy it and paste it on your Arduino IDE editor. Make necessary changes in the code as mentioned aboved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Technologies used
Hardware components
- ESP32
- Arduino
- Raspberry Pi
- HC-SR04
Software & tools
- Arduino IDE
- Blynk
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.