You are on page 1of 4

A smart traffic management system utilizing sensor data, communication and automated

algorithms is to be developed to keep traffic flowing more smoothly. The aim is to optimally
control the duration of green or red light for a specific traffic light at an intersection. The
traffic signals should not flash the same stretch of green or red all the time, but should
depend on the number of cars present. When traffic is heavy in one direction, the green
lights should stay on longer; less traffic should mean the red lights should be on for a longer
time interval. This solution is expected to eliminate inefficiencies at intersections and
minimize the cost of commuting and pollution.
The working of the project is divided into three steps

·If there is traffic at both the signals, then the system will work normally by controlling the
signals one by one.

·If there is no traffic near the 1st signal, then the system will skip this signal and will move on
to the 2nd one.

·If there is no traffic at any signals, the system will stop at the current signal and will only
move on the next signal if there will be traffic at the next signal.

So this is the flow chart of the project. First it will take the data from the sensor and utilize it
and then will look for traffic at each signal. If traffic is on both sides it will run normally. If it
senses more traffic at 1st signal then 1st signal will go green and if more traffic at 2nd signal
then second signal goes green.

Code:

//Working of ultrasonic sensor

Trig sends ultrasonic waves and echo receives them and records duration.

Vcc is connected to 5V of Arduino and GND is connected to ground of Arduino.

//Variable Declaration

From Line 1 to 10 is variable declaration.


int red1, green1, yellow1 are assigned to pin 8,6,7 respectively of Arduino which are the
signals of traffic light 1.

int red2, green2, yellow2 are assigned to pin 3,5,4 respectively of Arduino which are the
signals of traffic light 2.

int pingPin is assigned to pin 10 of Arduino, which is connected to trigger of Ultrasonic


sensor B.

int pingPin2 is assigned to pin 12 of Arduino, which is connected to trigger of Ultrasonic


sensor A.

int echoPin is assigned to pin 9 pf Arduino, which is connected to Echo of ultrasonic sensor
B.

int echoPin2 is assigned to pin 11 pf Arduino, which is connected to Echo of ultrasonic


sensor B.

//void Setup

By using pinMode function we declare traffic light signals red1, green1, yellow1, red2,
green2, yellow2 will be giving output.In our case LED’s will light up.

//void loop

First we declare variables distance1 and distance2.

distance1 is the distance form ultrasonic sensor 1 to traffic in front of signal 1.

distance2 is the distance form ultrasonic sensor 2 to traffic in front of signal 2.

distance1 = calculatedistance(pingPin,echopin);

We have created a calculatedistance function to get distance from ultrasonic sensor to


traffic.

This function is defined in further lines.

Similarly distance2 = calculatedistance(pingPin2, echopin2);

Then we use a if statement to compare distances from both ultrasonic sensors.

In first case,

//line 27 to 46
distance1 is greater or equal to distance2

When distance1 is equal to distance2 we change signals red and green of both traffic light to
be low or off. And yellows will be high or get light up. This is done with the help of if
statement.

When distance1 is greater than distance2

We change traffic lights signals red1, green2, yellow1, yellow2 to low or off. And red2,
green1 is high or light up. This means traffic light 2 is green and Cars can pass as there is
more traffic on signal 2. This is achieved by while loop.

In second Case,

//line 50 to 70

distance2 is greater or equal to distance1

When distance2 is equal to distance1 we change signals red and green of both traffic light to
be low or off. And yellows will be high or get light up. This is done with the help of if
statement.

When distance2 is greater than distance1

We change traffic lights signals red2, green1, yellow1, yellow2 to low or off. And red1,
green2 is high or light up. This means traffic light 1 is green and Cars can pass as there is
more traffic on signal 1. This is achieved by while loop.

//microsecondsTocentimeter function

// line 75

long micorsecondsToCentimeters function converts microseconds to centimeter.

Why return micorseconds / 29 /2 ?

the answer is because Sound travels at 343 meters per second, which means it needs
29.155 microseconds per centimeter. So, we have to divide the duration by 29 and then
by 2, because the sound has to travel the distance twice. It travels to the object and then
back to the sensor.

//line 80 to 95

Now we will see how the calculatedistance function works

It takes two arguments pingPin and echoPin.

First we declare variables duration and cm.

pingPin is set to pinMode output.

Then we control pingPin function give trigger to ultrasonic sensor that generates ultrasonic
waves.

First pingPin is low and after delay of 2 microseconds it is turned on that is high to generate
waves. Then it is turned back off or low.

Then echoPin is used to get input from the ultrasonic sensor of the duration waves take to
come back.

Whatever input we get from echoPin is stored in duration variable.

then we run the function micorsecondsToCentimeter using duration argument and store the
output in variable cm.

Then calculate distance function returns cm as the distance.

You might also like