Automatic Traffic Light Using PIC Microcontroller Code, Circuit Diagram and Explanation - Electronify
Automatic Traffic Light Using PIC Microcontroller Code, Circuit Diagram and Explanation - Electronify
You are here: Home / PIC Based Projects / Automatic traf c light using PIC Microcontroller
The objective of this project is to design a traf c light control system. This traf c light
controller is used at the intersection that consists of a main road and two side roads.
A four way traf c light control system with count down timers is to be designed and
constructed. The system is to be developed with the PIC16f877A chip being the
microcontroller that is programmed to do the task of controlling. Figure shows the
drawing of the 4-way junction, where each way has its traf c light and counter.
Low power LEDs are used for every traf c light with different colors, namely red,
yellow and green. The red LED indicates stop driving, the yellow LED indicates start
stopping and the green LED indicates drive. The sequence of altering the LEDs
according to their color is as shown in the gure below: Green-Yellow-Red-Green.
Twelve LEDs are used;three to each traf c light.
7-segment LED displays are used to show the current count value. Since all of the
traf c lights are working simultaneously, each one is to display a different digit than the
other. When a traf c light is tuned green, its corresponding 7-segment displays start
counting down from a speci c value and decrements until zero is reached. After this
the counter starts by a new count value at the moment the yellow light turns on. When
the red light turns on after the yellow took its time, the count continues to decrement
until reaching zero. This means that the same 7-segments, on each traf c light, are used
to display the count when cars are allowed and not allowed to pass. In terms of
counting, the yellow and red are considered one set while the green is another set.
Turn on led_Red1
Turn on led_Green2
Wait 30 seconds
Turn on led_Yellow1
Turn on led_Yellow2
Wait 3 seconds
Turn on led_Red2
Turn on led_Green1
Wait 20 seconds
Turn on led_Yellow1
Turn on led_Yellow2
Wait 3 seconds
Software Development
The code developed for the PIC is written in the C language. The compiler used to write
the C code is mikroC PRO for PIC V. 6.6.3. After the c code is successfully compiled, a
HEX le is generated.
CODE:
1 /*
2 *Projectname:
3 Automatictrafficlightcontrolwithmicrocontroller
4 *Copyright:
5 (c)lET'STHINkBINARY,2017.
6 *RevisionHistory:
7 V0.0
8 *Description:
9 AutomatictrafficlightusingPICMicrocontroller.
10 *Testconfiguration:
11 MCU:PIC16f877a
12
13 Oscillator:8.0000MHzCrystal
14 SW:mikroCPROforPIC
15 http://www.mikroe.com/mikroc/pic/
16 *NOTES:
17 ThisOurGroup(thankstojoinandparticipate):
18 https://www.facebook.com/groups/816450708460518/
19 Facebookpage(thankstolikeandshare):
20 https://www.facebook.com/LetsThinkBinary1728910547340654/
21 YoutubeChannel(thankstosubscribe):
22 https://www.youtube.com/channel/UCTIAzxEkyeA6HTvl_VHdTw
23 */
24
25 #include"Display_Utils.h"
26
27 unsignedshortshifter,portd_index;
28 unsignedshortportd_array[4];
29 intdigit,digit1,digit10,i;
30
31 sbitled_Green1atportC.b0;
32 sbitled_Yellow1atportC.b1;
33 sbitled_Red1atportC.b2;
34 sbitled_Green2atportC.b5;
35 sbitled_Yellow2atportC.b6;
36 sbitled_Red2atportC.b7;
37
38 voidinterrupt(){
39 PORTA=0;//Turnoffall7segdisplays
40 PORTD=portd_array[portd_index];//bringappropriatevaluetoPORTD
41 PORTA=shifter;//turnonappropriate7seg.display
42
43 //moveshiftertonextdigit
44 shifter<<=1;
45 if(shifter>8u)
46 shifter=1;
47
48 //incrementportd_index
49 portd_index++;
50 if(portd_index>3u)
51 portd_index=0;//turnon1st,turnoff2nd7seg.
52 TMR0=0;//resetTIMER0value
53 TMR0IF_bit=0;//ClearTMR0IF
54 }
55
56
57 voiddisplay(){
58 digit=i%10u;
59 digit1=conversion(digit);//prepareonesdigit
60 portd_array[0]=conversion(digit);//andstoreittoPORTDarray
61 portd_array[2]=conversion(digit);
62 digit=(char)(i/10u)%10u;
63 digit10=conversion(digit);//preparetensdigit
64 portd_array[1]=conversion(digit);//andstoreittoPORTDarray
65 portd_array[3]=conversion(digit);//andstoreittoPORTDarray
66 }
67
68 voidmain(){
69
70 ADCON1=6;//ConfigurePORTXpinsasdigital
71 TRISA=0;//ConfigurePORTAasoutput
72 PORTA=0;//ClearPORTA
73 TRISD=0;//ConfigurePORTDasoutput
74 PORTD=0;//ClearPORTD
75 TRISC=0;
76 PORTC=0;
77 OPTION_REG=0x80;//Timer0settings
78 TMR0=0;//clearTMROL
79 digit=0;
80 portd_index=0;
81 shifter=1;
82 GIE_bit=1;//EnaableGIE
83 TMR0IE_bit=1;//EnaableT0IE
84 //INTCON=0xA0;//EnaableGIE,T0IE
85
86 do{
87 //PHASE1
88 PORTC=0;
89 i=30;
90 for(i;i>=0;i){
91 display();
92 led_Green1=1;led_Red2=1;
93 delay_ms(400);
94 }
95 //PHASE2
96 PORTC=0;
97 i=3;
98 for(i;i>=0;i){
99 display();
100 led_Yellow2=1;led_Yellow1=1;
101 delay_ms(400);
102 }
103 //PHASE3
104 PORTC=0;
105 i=20;
106 for(i;i>=0;i){
107 display();
108 PORTC=0;
109 led_Green2=1;led_Red1=1;
110 delay_ms(400);
111 }
112 //PHASE4
113 PORTC=0;
114 i=3;
115 for(i;i>=0;i){
116 display();
117 led_Yellow2=1;led_Yellow1=1;
118 delay_ms(400);
119 }
120
121 }while(1);//infiniteloop
122 }//endofprogram
Results:
Compile the PIC code and get the hex le from it.
For simulating with PROTEUS ISIS hit run button and then you will get above output.
Resource:
You can download the MikroC Source Code and Proteus les etc from this link traf c light
controller:
This Our Group (thanks to join and participate):
https://www.facebook.com/groups/816450708460518/
Facebook page: https://www.facebook.com/Lets-Think-Binary-1728910547340654/
Youtube Channel (thanks to subscribe) :
https://www.youtube.com/channel/UCTIAzxEkyeA6HTvl_VHd-Tw
Share
0 Share
31 Tweet Pin0 Share
0 Share
0
FILED UNDER: PIC BASED PROJECTS TAGGED WITH: PIC TRAFFIC LIGHT, TRAFFIC LIGHT, TRAFFIC LIGHT CONTROLLER
SOCIAL LINKS ELECTRONIFY LINKS
About us
Career
Amazon store
Contact Us
Download proteus