In this practical, we will create a simple yet effective smart traffic control system using an ESP32 microcontroller. This project will utilize edge computing to analyze and optimize traffic flow at an intersection in real time. By the end of this practical, you will understand how IoT benefits from edge networks and learn how to architect simple edge deployments.
Assembling the circuit
Now, we must wire up the circuit. Instructions for doing so and a corresponding diagram have been provided for your convenience as follows:
Figure 6.6 – Connection diagram for the practical
Connect the ultrasonic distance sensor (HC-SR04) to the ESP32 device. Connect the VCC pin to 5V, GND pin to GND, Trig pin to pin 17, and Echo pin to pin 18 of the ESP32 module.
Connect the servo motor’s (SG90) VCC pin to 5V, GND pin to GND, and data pin to pin 16 on the ESP32 module.
Connect the longer leg (anode) of the red, yellow, and green LEDs to pins 25, 26, and 27, respectively, on the ESP32 module using jumper wires. Connect the shorter leg (cathode) of each LED to GND through 220-ohm resistors.
That’s it for the hardware, and we can now move on to developing the software for the project.
Writing the code
Now, we can start writing the code. The code will be split into three sections: initialization, setup, and loop.
Initialization
Import the ESP32Servo library. You can get the library either from the Manage Sketch Library from the Arduino library or download it from this link: https://github.com/jkb-git/ESP32Servo/archive/refs/heads/master.zip. Define pins for the ultrasonic sensor, servo motor, and LEDs, and declare the variables for distance calculation, servo position, and LED duration::
#include <ESP32Servo.h>
Servo servoMotor;
int pos = 0;
int servoPin = 16;
const int sensorAlertPin = 17;
const int sensorRepeatPin = 18;
const int alertLEDRed = 25;
const int alertLEDYellow = 26;
const int alertLEDGreen = 27;
long duration;int distance;
With that, we have initialized the necessary variables. Now, we can look at creating the setup segment of the code.
Setup
Set up the serial communication speed, configure the pins as INPUT or OUTPUT using the pinMode command, attach the servo motor to pin 16, and move the servo to initial position (0):
void setup() {
Serial.begin(9600);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
servoMotor.setPeriodHertz(50); // standard 50 hz servo
pinMode(sensorAlertPin, OUTPUT);
pinMode(sensorRepeatPin, INPUT);
pinMode(alertLEDRed, OUTPUT);
pinMode(alertLEDYellow, OUTPUT);
pinMode(alertLEDGreen, OUTPUT);
servoMotor.attach(servoPin);
servoMotor.write(0); // put servo in initial position
servoMotor.attach(servoPin, 500, 2500); // attaches the servo on pin servoPin (16) to the servo object}
With that, we have created the setup segment of the code. Now, we can look at creating the loop segment of the code.
Loop
Perform the following tasks in a loop:
Send a trigger pulse from the ultrasonic distance sensor (by bringing it to LOW, HIGH, and LOW again with a short delay between transitions).
Read the duration of the echo pulse received and calculate the distance to the nearest object.
For monitoring purposes, print the distance result in the Serial Monitor. If the distance is greater than 100 cm, set the servo motor to 90 degrees and change the sequence of traffic light turn-on time to 0 seconds for red, 1 second for yellow, and 4 seconds for green. Otherwise, set the servo motor to 0 degrees and change the traffic light to 4 seconds for red, 1 second for yellow, and 0 seconds for green.
Implement the trafficLightControl function to control the LED traffic lights by specifying the duration for each LED state (red, yellow, and green):
void loop() {
sensorAlertPin digitalWrite(sensorAlertPin, LOW);
delayMicroseconds(5);
digitalWrite(sensorAlertPin, HIGH);
delayMicroseconds(15);
digitalWrite(sensorAlertPin, LOW);
duration = pulseIn(sensorRepeatPin, HIGH);
distance = duration * 0.04 / 2;
Serial.print(“The distance is : “);
Serial.println(distance);
if (distance > 80) {
servoMotor.write(95);
trafficLightControl(4, 1, 0);} else {
servoMotor.write(5);
trafficLightControl(0, 1, 4);}
delay(1000);}
void trafficLightControl(int redDuration, int yellowDuration, int greenDuration) {
digitalWrite(alertLEDRed, HIGH);
delay(redDuration * 1000);
digitalWrite(alertLEDRed, LOW);
digitalWrite(alertLEDYellow, HIGH);
delay(yellowDuration * 1000);
digitalWrite(alertLEDYellow, LOW);
digitalWrite(alertLEDGreen, HIGH);
delay(greenDuration * 1000);
digitalWrite(alertLEDGreen, LOW);}
Upload the code to your Arduino board.
With that, we now are ready to test the system and see it in action.
Leave a Reply