Four way Traffic light system using 8051 Microcontroller and 7 Segment

four-way-traffic-light-system-8051-microcontroller

Traffic light system was one of the fascinating applications of Embedded systems and have been using the same till this day. I have previously posted a simple Traffic light system for one way roads with small timings check it out if you are interested in it. This is the four way traffic light system using embedded systems which was bit complex in nature as we need to consider the traffic flow in four different directions providing appropriate timings to each of the lights. 

This system uses 8051 microcontroller ( AT89C52) , 7-segments and LED’s for indication. The LED’s which was used as lights was connected to the Microcontroller by means of common Anode configuration. In this configuration the Microcontroller was used to sink the current from the LED to its ports. That means logic 0 signal in the Microcontroller switches the LED ON and logic 1 signal switches the LED off. Here we are using 6 MHz crystal for the 8051 Microcontroller operation and you can use upto 12MHz crystal with this controller.


COMMON CATHODE 7-SEGMENT:

Common-cathode-common-anode-pin-configuration-7-segment

In this design, we are about to use a Common Cathode 7-segment in which the LED’s are connected in a manner sourcing from the Microcontroller. As you can see in the above pin configuration there is two common ground pins, we can use any one of it. The 7-segment should be connected to the port in the following order P2.0 to pin “a” of the 7 segment, P2.1 to b , P2.2 to c and ends up with P2.7 to h. These are the connection configurations and components we are going to use in this 4-way Traffic light system using 8051 Microcontroller.

DESIGN:

four-way-traffic-light-scheme-layout

The above diagram illustrates the traffic flow layout of the four way road. And this is just a model of the four way road ,schemes and layout may subjected to change. I have chose this one for easier explanation of the traffic flow.

The traffic flow can be classified in to four phases in the above diagram and i have considered the North as starting point of this traffic flow. And in the above scheme vehicles are allowed to make a free right turn so we need to consider only two directions straight and left. So the green signal was classified into two types one for G for permitting vehicle to proceed forward and GL for permitting vehicles to left.

PHASE I-

  • Initially Vehicle from A needs to travel to F and from E to B roads.
  • So in the first Phase forward green signal in A and E permits vehicles to pass through while East and west roads are stopped by red signal.

PHASE II-

  • Phase II permits the vehicle to pass from G to D and from C to H roads.
  • Traffic flow from rest of the two roads North and south was stopped by means of Red signal.

PHASE III-

  • Phase three permits traffic flow in the left directions from A to D and from E to H.
  • Traffic flow in East and west are stopped by means of red signal.

PHASE IV-

  • Phase four permits traffic flow from C to F and from G to B.
  • Traffic flow in the North and south are stopped by means of red signal.
  • The cycle repeats again from Phase I to Phase IV and thus the traffic is regulated.

NOTE:

The above scheme is just an example and are subjected to change in real time roads, as different four way traffic flow schemes are followed widely around the world. This illustration was aimed to make you understand the flow and guide in designing the system according to the flow of traffic.

TIMING OF THE SIGNALS:

The timing is one of the important factor to consider in a traffic light system. Here in this design i have programmed in such a way a red light will be on for 50 seconds and 10 seconds for Yellow light. So adding up a traffic flow or a Green signal will be On for 60 seconds totally before switching the flow to the next phase.

The 7 segment was used in this design to to display the timing to the vehicle users in the road. This ease up the vehicle users and provides knowledge of the timing left before the switching of the next signal.

PROGRAM OF TRAFFIC LIGHT SYSTEM:

The coding was done using Embedded C language using IDE 8051 software. The code was given below.

#include<stdio.h>
#include<reg51.h>
void delay(void);       
void count1(void);    
void count2(void);
sbit NR=P0^0; sbit NY=P0^1; sbit NG=P0^2; sbit NGL=P0^3; //Setting bit for LED's north
sbit SR=P0^4; sbit SY=P0^5; sbit SG=P0^6; sbit SGL=P0^7;   //Setting Bit for LED's South
sbit ER=P1^0; sbit EY=P1^1; sbit EG=P1^2; sbit EGL=P1^3;  //Setting Bit for LED's East
sbit WER=P1^4; sbit WEY=P1^5; sbit WEG=P1^6; sbit WEGL=P1^7; //Setting Bit for LED's West
unsigned char a[10]={0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f}; //Array for displaying digits on segment 1,2.......9,0
unsigned char b[7]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d}; //Array for displaying 0 to 6
unsigned int i,j,s,k;             //Assigning Integers
void main()                        //Main program
  {
     NR=1; NY=1; NG=0; NGL=1;       // I phase forward green for north lights
     SR=1; SY=1; SG=0; SGL=1;         // forward green for south lights
     ER=0; EY=1; EG=1; EGL=1;        //Red signal for east
     WER=0; WEY=1; WEG=1; WEGL=1;   //Red signal for west lights
      {
       count1();                            //Calling out subroutine to display digits in 7 segment
      }
     NR=1; NY=1; NG=0; NGL=1;     //I phase yellow signal
     SR=1; SY=1; SG=0; SGL=1;      
     ER=1; EY=0; EG=1; EGL=1;     
     WER=1; WEY=0; WEG=1; WEGL=1;
      {
       count2();                         //Calling out sub routine for displaying counts for yellow
      }
     NR=0; NY=1; NG=1; NGL=1;      //II phase red signal for north lights
     SR=0; SY=1; SG=1; SGL=1;       
     ER=1; EY=1; EG=0; EGL=1;     
     WER=1; WEY=1; WEG=0; WEGL=1;
      {
       count1();
      }
     NR=1; NY=0; NG=1; NGL=1;     //II phase yellow lights for North and south
     SR=1; SY=0; SG=1; SGL=1;
     ER=1; EY=1; EG=0; EGL=1;      
     WER=1; WEY=1; WEG=0; WEGL=1;
       {
       count2();
       }
     NR=1; NY=1; NG=1; NGL=0;     //III phase Green left for north and south
     SR=1; SY=1; SG=1; SGL=0;
     ER=0; EY=1; EG=1; EGL=1;      
     WER=0; WEY=1; WEG=1; WEGL=1;
       {
       count1();
       }
     NR=1; NY=1; NG=1; NGL=0;    //III phase yellow lights
     SR=1; SY=1; SG=1; SGL=0;
     ER=1; EY=0; EG=1; EGL=1;     
     WER=1; WEY=0; WEG=1; WEGL=1;
     {
       count2();
       }
     NR=0; NY=1; NG=1; NGL=1;     //IV phase Red signal for North and south
     SR=0; SY=1; SG=1; SGL=1;
     ER=1; EY=1; EG=1; EGL=0;      
     WER=1; WEY=1; WEG=1; WEGL=0;
     {
       count1();
       }
     NR=1; NY=0; NG=1; NGL=1;      //IV phase Yellow signal for north and south
     SR=1; SY=0; SG=1; SGL=1;
     ER=1; EY=1; EG=1; EGL=0;       
     WER=1; WEY=1; WEG=1; WEGL=0;
     {
       count2();
       }
     }
  void count1(void)     //Sub routine for displaying numbers in segments for red signal
    {
   P2=0x3f;
    for(j=0;j<=4;)
      {
       for(i=0;i<=8;)
       {
        P3=a[i];
        i++;
        delay();
        }
       j++;
       P2=b[j];
       P3=0x3f;
       delay();
      }
    }
  void count2(void) //Sub routine for displaying numbers in segments for yellow signal
    {
      P2=0x3f;
      for(i=0;i<=8;)   
        {
         P3=a[i];
         i++;               
         delay();
        }
       P2=0x06;         
       P3=0x3f;
       delay();           
  }
  void delay(void)      //Sub routine for creating delay
    {
      for(s=0;s<=230;s++)     //Creating delay using number count
       {
         for(k=0;k<238;k++);
       }
    }

Modifying the array can extend your timings to different lights. 

ROOM FOR MODIFICATIONS :

  • Changing the lighting schemes or increasing the phases can be done by replacing the signal bits in the program.
  • Additional lights for pedestrian can be added just by connecting LED’s parallel to the ports, MC capable of sinking currents of multiple LED’s.
  • Array can be extended upto 90 seconds of timing.

I have also added the Proteus simulation file and hex file of this program below. Get the files here 

Four Way Traffic Light
Four Way Traffic Light
Four-way-Traffic-light.rar
Version: 1
117.2 KB
2067 Downloads
Details


Four Way Traffic Light
Four Way Traffic Light
Four-way-Traffic-light.rar
Version: 1
117.2 KB
2067 Downloads
Details

78 Comments

  1. Tanny

    Hello, can you please tell me the components that are required for this project including resistors and capacitors (if needed)

    Reply
    1. Frank DonaldFrank Donald (Post author)

      C1 and C2 is 22pF. And the current limiting resistors connecting to LED and 7 segment should be of 200 ohms.

      Reply
  2. Amy

    What are the values of resistors and capacitors used?

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Cap values : 22pf and Resistor 470 ohms.

      Reply
  3. MhoElectronics

    Good project. I would have preferred if the timing was count down instead. It would have looked more like the real deal. Counting time upwards, the drivers may not know where it ends, but a countdown sure ends in 0…That said, I think i like your approach.
    Thanks.

    Reply
  4. alamin

    hi can u send me the simulation video from the proteous. just from the phone to the laptop the actual video that working on proteous

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Alamin,
      You can download them from the option given at the bottom of this article

      Reply
  5. RAHUL JAMES

    Sir can you please provide me the circuit components that you used Please send me on this email rahuljames111@gmail.com

    Reply
  6. i tried to download hex file but it asking me about registration again and again

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Muzammil,

      Try now, it should be working. Cheers

      Reply
  7. Arnold

    Sir can I use arduino uno here? Thanks..

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Arnold,
      Code and design needs to be redone for this. But that should be very easy to do

      Reply
  8. Cate

    Hi sir. I’m done simulation. How can i put or burn the hex file in microcontroller? Thanks.

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Cate,

      You need a programmer board and a programmer software for that. Contact your local Eletronics store for getting a programmer board for your microcontroller also enquire which software you can use with the board

      Reply
  9. Rafael ArujRafael Aruj

    Hi Frank, can we put 2 digits timers on all 4 corners? if yes, please show us the diagram. thank you….more power to you…..

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi Rafael,

      Yes that’s possible. You should drive all the four 7 segments parallel from the microcontroller ports. But make sure you meet the power requirement of 7 segment by placing any power IC between them.

      Reply
  10. Arnold Jr Pelegrino

    Hi Frank, i’ve tried this project and it’s good. Now, instead the counter is increment how to make it decrement? Thank you so much

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi Arnold.

      Change the lines of code to following
      75 for(j=4;j>=0)
      77 for(i=8;j>=0)
      80 i–
      83 j–
      92 for(i=8;i>=0)
      95 i–

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *