Four way Traffic light system using 8051 Microcontroller and 7 Segment - Gadgetronicx

Four way Traffic light system using 8051 Microcontroller and 7 Segment

R- Red, Y- Yellow, G-Green, GL- Green Left
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.


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.


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.

  • 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 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 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 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.



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.


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.


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

  1. #include<stdio.h>
  2. #include<reg51.h>
  3. void delay(void);       
  4. void count1(void);    
  5. void count2(void);
  6. sbit NR=P0^0; sbit NY=P0^1; sbit NG=P0^2; sbit NGL=P0^3; //Setting bit for LED's north
  7. sbit SR=P0^4; sbit SY=P0^5; sbit SG=P0^6; sbit SGL=P0^7;   //Setting Bit for LED's South
  8. sbit ER=P1^0; sbit EY=P1^1; sbit EG=P1^2; sbit EGL=P1^3;  //Setting Bit for LED's East
  9. sbit WER=P1^4; sbit WEY=P1^5; sbit WEG=P1^6; sbit WEGL=P1^7; //Setting Bit for LED's West
  10. unsigned char a[10]={0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f}; //Array for displaying digits on segment 1,2.......9,0
  11. unsigned char b[7]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d}; //Array for displaying 0 to 6
  12. unsigned int i,j,s,k;             //Assigning Integers
  13. void main()                        //Main program
  14.   {
  15.      NR=1; NY=1; NG=0; NGL=1;       // I phase forward green for north lights
  16.      SR=1; SY=1; SG=0; SGL=1;         // forward green for south lights
  17.      ER=0; EY=1; EG=1; EGL=1;        //Red signal for east
  18.      WER=0; WEY=1; WEG=1; WEGL=1;   //Red signal for west lights
  19.       {
  20.        count1();                            //Calling out subroutine to display digits in 7 segment
  21.       }
  22.      NR=1; NY=1; NG=0; NGL=1;     //I phase yellow signal
  23.      SR=1; SY=1; SG=0; SGL=1;     
  24.      ER=1; EY=0; EG=1; EGL=1;    
  25.      WER=1; WEY=0; WEG=1; WEGL=1;
  26.       {
  27.        count2();                         //Calling out sub routine for displaying counts for yellow
  28.       }
  29.      NR=0; NY=1; NG=1; NGL=1;      //II phase red signal for north lights
  30.      SR=0; SY=1; SG=1; SGL=1;      
  31.      ER=1; EY=1; EG=0; EGL=1;    
  32.      WER=1; WEY=1; WEG=0; WEGL=1;
  33.       {
  34.        count1();
  35.       }
  36.      NR=1; NY=0; NG=1; NGL=1;     //II phase yellow lights for North and south
  37.      SR=1; SY=0; SG=1; SGL=1;
  38.      ER=1; EY=1; EG=0; EGL=1;     
  39.      WER=1; WEY=1; WEG=0; WEGL=1;
  40.        {
  41.        count2();
  42.        }
  43.      NR=1; NY=1; NG=1; NGL=0;     //III phase Green left for north and south
  44.      SR=1; SY=1; SG=1; SGL=0;
  45.      ER=0; EY=1; EG=1; EGL=1;     
  46.      WER=0; WEY=1; WEG=1; WEGL=1;
  47.        {
  48.        count1();
  49.        }
  50.      NR=1; NY=1; NG=1; NGL=0;    //III phase yellow lights
  51.      SR=1; SY=1; SG=1; SGL=0;
  52.      ER=1; EY=0; EG=1; EGL=1;     
  53.      WER=1; WEY=0; WEG=1; WEGL=1;
  54.      {
  55.        count2();
  56.        }
  57.      NR=0; NY=1; NG=1; NGL=1;     //IV phase Red signal for North and south
  58.      SR=0; SY=1; SG=1; SGL=1;
  59.      ER=1; EY=1; EG=1; EGL=0;     
  60.      WER=1; WEY=1; WEG=1; WEGL=0;
  61.      {
  62.        count1();
  63.        }
  64.      NR=1; NY=0; NG=1; NGL=1;      //IV phase Yellow signal for north and south
  65.      SR=1; SY=0; SG=1; SGL=1;
  66.      ER=1; EY=1; EG=1; EGL=0;      
  67.      WER=1; WEY=1; WEG=1; WEGL=0;
  68.      {
  69.        count2();
  70.        }
  71.      }
  72.   void count1(void)     //Sub routine for displaying numbers in segments for red signal
  73.     {
  74.    P2=0x3f;
  75.     for(j=0;j<=4;)
  76.       {
  77.        for(i=0;i<=8;)
  78.        {
  79.         P3=a[i];
  80.         i++;
  81.         delay();
  82.         }
  83.        j++;
  84.        P2=b[j];
  85.        P3=0x3f;
  86.        delay();
  87.       }
  88.     }
  89.   void count2(void) //Sub routine for displaying numbers in segments for yellow signal
  90.     {
  91.       P2=0x3f;
  92.       for(i=0;i<=8;)  
  93.         {
  94.          P3=a[i];
  95.          i++;              
  96.          delay();
  97.         }
  98.        P2=0x06;         
  99.        P3=0x3f;
  100.        delay();          
  101.   }
  102.   void delay(void)      //Sub routine for creating delay
  103.     {
  104.       for(s=0;s<=230;s++)     //Creating delay using number count
  105.        {
  106.          for(k=0;k<238;k++);
  107.        }
  108.     }


  • Modifying the array can extend your timings to different lights.
  • 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  Download 

Share this with others through social networks if you like it. Feel free to post your comments and opinions regarding this post and i am happy to help you with queries. Thank you for visiting and Happy Coding.


  1. Do you have such four way traffic light circuit in which we can add direct delay to RED & GREEN using keypad No need of 7 Segment Display..... jus system should ask timing for different RED & Green on LCD display

    1. Using keypad to offer delay input is a tedious process. However you could alter it by using two buttons one to increment the delay and other to decrement it. This should make your task easier

  2. can you modify it to density based traffic control using IR sensor n removing display plz ?

    1. Actually many visitors asking for this density based traffic light, but iam not sure that it can be implemented using IR sensor. I will Try my best

    2. hope u succeed very soon


We are working hard as we can to provide you precise information with working Circuit Designs and Programs. However if you find any content inappropriate or misleading kindly suggest modification or improvement through Contact Us form. For queries and opinion regarding the article kindly post a comment here, we will assist you.