Skip to main content

Traffic Light Control System

Hi guys...
This project is a traffic light control system for a 4-way junction...I will explain the whole project in great details. The circuit schematic and the code in C will be provided. If you would like to order this product within MALAYSIA, you may contact me via email.


Please email me via moa29000@gmail.com for the schematic and other project files. However, the source code can be purchased with/without my support from the following BinPress store:
http://robotaaly.binpress.com/#pricing

You have two options for your purchase:
The software used to develop the schematic diagram and to draw the PCB layout is CadSoft Eagle Professional V6.1.0 and the PIC C compiler used is mikroC Pro.

Watch the following video to take a general overview of the project:



Description of the Project

A 4 way traffic 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 traffic light and counter.

Low power LEDs are used for every traffic 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 figure below: Green-Yellow-Red-Green. Twelve LEDs are used; three to each traffic light.

7-segment LED displays are used to show the current count value. Since all of the traffic lights are working simultaneously, each one is to display a different digit than the other. When a traffic light is tuned green, its corresponding 7-segment displays start counting down from a specific 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 traffic 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.
The circuit board designed supports in-circuit serial programming (ICSP) for the PIC. This support eases the way to the designer to program the microcontroller without the need to plug the microcontroller in and out repeatedly.

Circuit Design and Fabrication


Traffic Light LEDs Interfacing


The light LEDs that were used were soldered in a strip board as shown in schematic diagram. They are connected to Port A and Port D of the PIC by jumper wires that are connected to header connectors whose schematic is shown in the right side of the figure.



Since LEDs are low power devices, then the current passing through them is to be limited. This is done by using proper resistors. Since the rated current for them is 20mA and the voltage of Vcc is 5V, then the resistor values can be determined by Ohm’s law as follows

For safety issues, 300Ω resistors were used.
There are 3 LEDs for every traffic light in the strip board. One leg of each LED is connected to Vcc and the other is connected to one resistor. This makes them as active-low output devices because holding their corresponding PIC pins to low turns them on and vice versa.


7-segment Display Interfacing


Eight common cathode 7-segment displays are used in this 4-way traffic light control system. Figure 2‑3 show a 7-segment LED display and its pin description.



It is a 10-pin display device. The pins are: a, b, c, d, e, f, g, DP (dot) and com. The pins labeled as “com” are internally connected to each other. Since common cathode displays have been used, the display is activated when the “com” pins are held high (5V). When a 7-segment display is activated, holding any other pin at low voltage will light the corresponding segment connected to that pin because this creates a voltage difference across the segment LED and ,hence, current flows. The DP pin is not implemented in this project because our interest is to show digits without fractions.

The digits displayed in every 7-segment range from 0 to 9. Since the required numbers to display are more than 9, two 7-segment LED displays (left and right) were used for every traffic light and this is why 8 displays were used in this project. The same strip board used to solder the LED lights is used to solder the two 7-segment displays on it as shown in Figure 2‑2. Since the number of pins to fully operate them is 9 and the total number of displays required is 8, multiplexing is required. The data pins (a, b, c, d, e, f and g) for the left and right displays in every strip board are connected to each other because they are multiplexed.

Multiplexing is made by using a 3 to 8 decoder. The IC (74LS138N) is used to select which 7 segment to show the digit (see IC3 in Figure 1‑1). This chip has 3 input pins labeled A, B and C and 8 output pins labeled Y0, Y1,.., Y7. The input pins are connected to the microcontroller for it to select which 7 segment to light up. One of the output pins is connected to one 7-segment terminal via its “com” pin. The way how the selection is made is done in Chapter 3. Y0 is used to select the right 7-segment of the first traffic light, Y1 is to select the left 7-segment of the first traffic light and Y2, Y3 are used to select the right and left 7-segment of traffic light 2 and so on.
To make it easy to display the numbers on the 7-segment, another decoder chip (4511N) is used. This chip is binary coded decimal (BCD) to 7-segment driver (see IC2 in Figure 2‑1). It is used to convert the BCD values received by its input pins to the digit to be displayed in the 7-segment. Since the number of data pins required on the 7-segmnet display is 7, the decoder has 4 inputs and 7 output pins. Later in the Software section, I will show you how to use BCD to display specific digits.

Figure 2‑4 shows the PCB layout drawing of the circuit of the system. The area of the board is . The size is enough to include all of the electronic components in one board.



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. 5.01. After the c code is successfully compiled, a HEX file is generated. It is the machine code that the PIC understands. This code is transferred to the microcontroller using a suitable PIC programmer.

7-Segment Display Selection

As discussed before, a 3 to 8 decoder is used to activate a certain 7-segment. Selecting a 7-segment display is done by holding its “com” pin to a high voltage. Putting it to low will deactivate it and makes it blank. The table below shows the way how each 7-segment display is selected according to the input pins.

Input Pins (Select)
Output
C
B
A
Traffic Light Selected
0
0
0
TL1 Right
0
0
1
TL1 Left
0
1
0
TL2 Right
0
1
1
TL2 Left
1
0
0
TL3 Right
1
0
1
TL3 Left
1
1
0
TL4 Right
1
1
1
TL4 Left

BCD to 7-segment Decoder Operation
The digits display on the selected 7-segment depends on the BCD value at the input of the 4511N chip. The table below summarizes the way how to display digits from 0 to 9.

Input Pins (Select)
Output
D
C
B
A
Displayed Value
0
0
0
0
0
0
0
0
1
1
0
0
1
0
2
0
0
1
1
3
0
1
0
0
4
0
1
0
1
5
0
1
1
0
6
0
1
1
1
7
1
0
0
0
8
1
0
0
1
9
1
0
1
0
Blank
1
0
1
1
Blank
1
1
0
0
Blank
1
1
0
1
Blank
1
1
1
0
Blank
1
1
1
1
Blank

Time Division and Delay

Simultaneous Display:

Since all of the data pins (LED segment pins) of the eight 7-segment displays are connected to each other. Therefore, no single LED display can be activated or can show a digit simultaneously. It seems to be impossible to show different digits on different LED displays at the same time, but it is not!

The capability of executing instruction of the microcontroller with very high speeds allows us to overcome this problem and be able to show different decimal values on different LED displays at seemingly the same time.
This issue can be easily handled by activating the required LED displays alternatively with high speed and sending the data in the same manner.

Selecting and sending data alternatively with high speeds allows us to see different digits on different displays as if they are actually simultaneously displayed. This is because a human eye cannot notice the digit alteration and display selection when high frequencies are used.

When data are sent to the display relatively slowly, flickering might occur and the alteration might be noticed. When different digits are sent to a display with no delay but the execution time between the first digit and the second, the display will look having a constant digit without changing. This is due to very high frequency of data sending in such a way that makes the LED segments required to show both of the digits light all together with the average power of the signals. Hence, there should be a compromise when specifying the amount of delay the data sent to the displays.

Counting Step delay: 

The time between each decrement shown on the display is to be one second. Using the built-in delay function in the MikroC compiler is not a good choice because calling a delay function urges the microcontroller to wait without doing anything until the delay time finishes. The microcontroller is to send data continuously without stopping because the last 7-segment display will only be activated and the rest will keep being off until the delay time finishes.

Solving this issue is made by using the timer interrupt handler. Timer 1 is the timer used to allow a one-second interval between each decrement. It is a 16-bit timer where the low byte and the high byte can have values between 0x00 to 0xFF. This means that the timer can have count up to 0xFFFF=65535 before it raises its overflow flag The crystal used has a frequency of 24MHz and Timer 1 has been assigned a timer input clock prescale value of 1:8. This means that the timer oscillator frequency is: 24MHz/8 = 3MHz

In the PIC16F877A microcontroller, one instruction cycle consists of four oscillator periods, so, the time taken by every timer decrement is: 4/3MHz = 1.333us.

Since this timer is a 16-bit one, the maximum delay time that can be obtained is: 1.333us * 65536 = 87.36ms

This delay is short because we need one second time. A choice is to let the timer increment for 60ms and run it 16 times to get almost 1s. So, the timer is to count 60ms/1.333us =4500 times  to get 60ms. Counting up (positive count) was chosen, therefore, the decimal value that is to be put into the timer high and low registers is:
x = 65536 - 4500 = (20424)dec = (5038)hex

So, the high byte register will be assigned a hex value of 50 and the low byte one will have a hex value of 38.

Interrupt:
Interrupts are enabled by setting the corresponding register. Timer 1 has an interrupt flag which is set when an overflow occurs. Ones it is set, the microcontroller handles its routine. In the interrupt function a variable called (loop_1s) is incremented and the timer is reset to the default values and the overflow flag is cleared. The counting on the display decrements each time this variable (loop_1s) reaches the number 16, achieving a 1 second decrement. When the decrement happens then this variable is reset to zero and this process keeps looping as long as the system is in the run time.

Concept behind the Algorithm Developed

Before starting to program, a plan was set on how to really achieve the controlling, show the digits correctly and light the required LEDs according to their colors at the correct time. The figure below shows the sequence of the LED lighting according to their color. The sequences have been divided into 8 phases. The starting phase is phase 1. 


The phases are:

Phase 1: TL1 green LED is on allowing cars to pass while all of the others are turned red.

Phase 2: TL1 yellow LED is turned on warning the cars to prepare for stopping while all of the traffic lights are turned red.

Phase 3: TL2 green LED is on allowing cars to pass while all of the others are turned red.

Phase 4: TL2 yellow LED is turned on warning the cars to prepare for stopping in its way while all of the traffic lights are turned red.

Phase 5: TL3 green LED is on allowing cars to pass while all of the others are turned red.
Phase 6: TL3 yellow LED is turned on warning the cars to prepare for stopping while all of the traffic lights are turned red.

Phase 6: TL4 green LED is on allowing cars to pass while all of the others are turned red.

Phase 7: TL4 yellow LED is turned on warning the cars to prepare for stopping while all of the traffic lights are turned red.

Phase 1, phase 3, phase 5 and phase 7 are the phases involving a green LED to light up. These take 15 seconds while the others involving a yellow LED to light up take 2 seconds.

Figure 3‑2 show the timing diagram of the traffic light LEDs for one cycle. Each cycle takes 68 seconds.


When the count of the right 7-segment in a traffic light reaches a value of zero then it starts again from 9 and the left 7-segment of that traffic light decrements by one. For traffic light TL1, this is done by the following code segment:

  else if (T1RightVal < 0){  //Check if TL1 right digit reached zero and
    T1RightVal = 9;          //decrement the left digit
    T1LeftVal --;
  }

A variable acting as flag was used to indicate the ending of the current phase so that the role of the second phase comes into place. The phases are made using C functions which are called continuously in each cycle. A phase cannot be entered except when the flag of the previous phase is set. The criteria of setting a phase flag is when the left 7-segment of a traffic light in each phase reaches zero. When a phase flag is set, the digits to display in next phase are initialized. This is done in the code below:

if (LeftVal <0){        //Check if left TL 7-segment digit is less than zero
    phaseFlags[phaseNo] =1; //Set flag to indicate finishing the current phase
    T1LeftVal=T11; T1RightVal=T10; //Set the display values for the next phase
    T2LeftVal=T21;   T2RightVal=T20;
    T3LeftVal=T31;   T3RightVal=T30;
    T4LeftVal=T41;   T4RightVal=T40;
  }

The phases are made to complete each other. This means that when the count has reached a value of e.g. 36 while the red color is on in one phase, the count will continue to 35 in the next phase as long as the red color is on. This continues until the count is zero, then a new count is started. The tables below show the starting and ending times of the counters on each TL for every phase.


Phase 1
Phase 2
Phase 3
Phase 4

Starting
Finishing
Starting
Finishing
Starting
Finishing
Starting
Finishing
TL1
14
0
52
51
50
36
35
34
TlL2
16
2
1
0
14
0
52
51
TL3
33
19
18
17
16
2
1
0
TL4
50
36
35
34
33
19
18
17

Phase 5
Phase 6
Phase 7
Phase 8

Starting
Finishing
Starting
Finishing
Starting
Finishing
Starting
Finishing
TL1
33
19
18
17
16
2
1
0
TlL2
50
36
35
34
33
19
18
17
TL3
14
0
52
51
50
36
35
34
TL4
16
2
1
0
14
0
52
51

It can be observed that the digit to display on TL1 in phase 1 is 14. This is the starting time when its green color is on. Since green takes 15 seconds, it keeps decrementing until reaching 0. The starting time of TL2 in phase 1 is 16, this keeps decrementing reaching a value of 2 then this count continues to 1 as shown by the starting time in phase 2 for TL2.


You may download the full code from the BinPress store with the following options:

You may order this product by contacting me via email: moa29000@gmail.com.
The price is RM1500.

If you liked this article, please do us a favor and share our Android app by clicking on the photo below:

Comments

  1. The code is not available..
    Kindly mail me the code.
    Regards
    Ashutosh Tiwari
    ashutosh.tiwari0812@gmail.com

    ReplyDelete
  2. You may purchase it from BinPress Store:
    http://robotaaly.binpress.com/

    You have two options: with my support or without it.

    ReplyDelete

Post a Comment

Popular posts from this blog

Brainy Trainee: A Human Performance Game for Android Devices

Dear all, We have to admit it! We all know that we've got rusty brains that need some lubricants to operate as expected. Your memory has been weak and your concentration has gone. What brings these back to you is the PRACTICE. So let me introduce to you my brain workout game for Android devices. Well, your brain's lubricant for today is a game that I have developed some days ago. Guess what! It's not only a fun game, it's also a game that improves your performance by time. With joy you learn. Its name is "Brainy Trainee". It is in a form of amusing tests that give you a measurement of your performance percentage. You got to challenge yourself to get a higher score. Check out the following video: http://www.youtube.com/watch?v=H76AKh6tkxg The current supported tests are: Attention or perception test: Your task here is to find out where the last black circle has lastly been. The following image shows a shot of this test. The gre

UnitBullet Converter

This is my unit converter app which I've recently developed for Android systems. It supports a wide varietyof measurements with many units. The design is elegant and the results can be spoken out by manually clicking a "speak" button or automatically when the result changes! This is a feature that adds value and significance to this app as it relieves the burden of frequently looking at the screen to write down the results. This page is a documentation of this app and the way how you may use it. You may download the app from here. User Interface Details The first screen that shows up when you start the app is: This window contains the following: A list of measurement categories: which is a list of all of the supported measurement types like angle, area and distance measurements. A list of units from which conversion is to be performed like centimeters, feet and inches in the distance and length category. A list of units to which conversion is to b