Introduction: Smart Traffic Light Using Matlab and Arduino
Road transport is one of the primitive modes of transport
in many parts of the world today. The number of vehicles using the road is increasing exponentially every day. Due to this reason, traffic congestion in urban areas is becoming unavoidable these days. Inefficient management of traffic causes wastage of invaluable time, pollution, wastage of fuel, cost of transportation and stress to drivers, etc. 1 but more importantly emergency vehicles like ambulance get stuck in traffic. Our research is on density based traffic control with priority to emergency vehicles like ambulance and fire brigade. So, it is very much necessary to design a system to avoid the above casualties thus preventing accidents, collisions, and traffic jams2 . The common reason for traffic congestion is due to poor traffic prioritization, where there are situations some lane have less traffic than the other and the equal green signal duration for both affect the wastage of resources and drivers are stressed.
This Project Contains two part
1. Arduino Interfacing with Matlab
2. Image processing with Matlab.
Step 1: Arduino Interfacing With Matlab
Requirement
1. Arduino and Cable
2. Matlab (I am using Matlab 2018a)
3. Bread Board.
4. LEDs
5. Jumping Wires
First step will be to add Arduino Support Package for Matlab
Test Your Connection with following code
clear all;
a = arduino();
clear a;
a = arduino('COM3', 'Uno'); //COM port may differ check your device manger for more detials
writeDigitalPin(a, 'D13', 1);
After executing this code Built-IN led will be high for Arduino Uno.
If its working fine till here now its turn to move image processing.
Step 2: Number of Vehicles Count
Now its turn to count number of car in each lane.
It will work as follow:
Taking Image of Empty Road.
I = imread('road.png');
Taking Image of Road with vehicles.
J = imread('roadandcars.png');
subtracting both images.
K = imabsdiff(I,J);
Converting resulting image into black and white image
th = graythresh(K);
L = im2bw(K,th);
Cropping each lane
Lane1=imcrop(L,[266 0 270 266 ]);
Lane2=imcrop(L,[536 265 270 266]);
Lane3=imcrop(L,[267 537 270 266]);
Lane4=imcrop(L,[0,267,270,266]);
//Your dimension and point may vary.
Counting connected object
[LabeledIm1,ConnectedObj1] = bwlabel(Lane1,4);
[LabeledIm2,ConnectedObj2] = bwlabel(Lane2,4);
[LabeledIm3,ConnectedObj3] = bwlabel(Lane3,4);
[LabeledIm4,ConnectedObj4] = bwlabel(Lane4,4);
Ploting each part
subplot(221),imshow(Lane1),title('Lane1'),
subplot(222),imshow(Lane2),title('Lane2'),
subplot(223),imshow(Lane3),title('Lane3'),
subplot(224),imshow(Lane4),title('Lane4'),
ConnectedObj1,2,3&4 will show number of car count.
For this project there will be two image will I have designed using Paint.
To vary number of Vehicles you can edit image in paint then save it.
Step 3: Adding Both Code Togather
Now we will count number of vehicles and change traffic light according to traffic in lane.
writeDigitalPin(a, 'D13', 1);
writeDigitalPin(a, 'D12', 0);
writeDigitalPin(a, 'D11', 0);
pause(5);
writeDigitalPin(a, 'D13', 0);
writeDigitalPin(a, 'D12', 1);
writeDigitalPin(a, 'D11', 0);
pause(2);
writeDigitalPin(a, 'D13', 0);
writeDigitalPin(a, 'D12', 0);
writeDigitalPin(a, 'D11', 1);
pause(5);
So let me explain the program
There are 3 light Green Red and Yellow connected to D13,D12 and D11.
This program is switching between these lights.
The Main program will work like this but it will be for 12 lights.
Green1(a,R1,G1,Y1);//Turning Green1 on
Red2(a,R2,G2,Y2) //Turing Red2 on
Red3(a,R3,G3,Y3) //Turing Red3 On
Red4(a,R4,G4,Y4) //Turing Red4 on
pause(5) /Wait
[c1,c2,c3,c4]=CountCar();//Count Vehicles in each lin
eLights(c1,c2,c3,c4,Th,a,R1,Y1,G1,R2,G2,Y2,R3,G3,Y3,G4,Y4,R4)//Turn on light according the number of Vehicles
Same code will be repeted for each traffic. As there are 4 traffic Poles.
I am Putting Main code on Github.
Threshold is 5 in each line when number of vehicles will exceed threshold the lane with more then threshold will turn green.Otherwise it will run in sequence defined in coding. For more details check mainProgram.m files.
video demo @
Get full code at :
https://github.com/DheerajSingh1107/Smart-Traffic-...
Insta : @iamDheerajSingh
Become a patron: