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

    Hi Frank, where can i put the USB Female connector in 8051 inorder to connect it in computer? Thanks..

    Reply
    1. Guest

      It can also use version 8 in this project?

      Reply
      1. Guest

        May i know how will I provide 5volts supply in the circuit?

        Reply
        1. Frank DonaldFrank Donald (Post author)

          You can use a Adapter for it or search for 5v regulator circuits.

          Reply
      2. Frank DonaldFrank Donald (Post author)

        Hi,
        Version 8 in the sense?

        Reply
    2. Frank DonaldFrank Donald (Post author)

      You can use a serial to USB converter in here. You will find plenty in the market

      Reply
      1. Guest

        Where can I put the pins of usb female connector in the diagram?

        Reply
        1. Frank DonaldFrank Donald (Post author)

          Hi,

          No you cannot do that directly. You have to use specialized USB comm IC’s which is capable of acting as an intermediary device between. 8051 (Serial communication) and USB. Read about FT232 IC, you might find what you are looking for.

          Reply
  2. santhosh kumarsanthosh kumar

    hai sir how to download zip file

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Santhosh,
      The downloads icon is at the bottom of the post check it out

      Reply
  3. Guest

    Sir what’s the use of crystal frequency? C1 and c2 = 22pf and c3 is 2.2pf? Is that right sir?

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi,

      Yes that’s right. C1 and C2 is meant for stablilized crystal operation. C3 is for smoothing the input, you can skip C3 in your setup if you are getting supply from DC adapter or stable supplies

      Reply
  4. Dnyaneshwar Chavan

    Sir iam working On this project. i need more information about this project… cound u help me and send me the detail about this project… Thank You

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Dyyanseshwar,

      All the information about this project listed in the article.

      Reply
  5. Sn Trio

    Hye i’m just a student and currently working on this project. Could you help me and send me the details about this project as soon as possible. Thank you.

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Sn Trio,
      Whole code and explanation is given above, what else you need.

      Reply
  6. Arnold

    and how many volts are the LED’s? Students only here.. hehehe thanks 🙂

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Arnold,
      Red, amber, green must be around 2.2V to 2.4V

      Reply
  7. Arnold

    What do you call the components parallel to c1 and c2? i’m not familiar with it.. hehehe thanks.. Can you send me the whole components needed and the value itself? Thank you very much.. here’s my e-mail a_pelegrino@brastel.com.ph

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Arnold,
      Use 22pf caps for C1 and C2, they are meant for stabilized running of crystal.

      Reply
  8. Abubakar Safiyanu

    sir i am compiling the code it give me an error saying
    ” cannot open source input file “reg51.h”: No such file or directory ”
    what can i do? please help.

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Abubakar,
      The code above was build using IDE 8051 which is pretty old and i don’t think it is in use anymore. Please replace that header with your IDE’s 8051 header file. Rest of the code shouldn’t be a problem. Do try it this way and if any problem occurs let me know 🙂

      Reply
  9. Muzammil

    Please send me hex file of program
    on this email. mojujutt5@gmail.com

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Muzammil,
      The file link was provided below.

      Reply
  10. juden

    sir , can you change code of the time interval of red and green light to 30 seconds both. yellow is okay with ten and with reset ,start and push button

    Reply

Leave a Comment

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