Sei sulla pagina 1di 12

Traffic Lights

A Project By

Perry Andrews

Based on the PIC16F84A Micro controller.

18 September 2004
Contents
Introduction....................................................................................................................2
Design and Development ...............................................................................................3
Construction.................................................................................................................10
Conclusion ...................................................................................................................12
References....................................................................................................................12

Figure 1 - Road Layout ..................................................................................................3


Figure 2 - Flow Diagram................................................................................................4
Figure 3 - Sequence Diagram ........................................................................................4
Figure 4 - Circuit Diagram.............................................................................................9
Figure 5 - Prototype .....................................................................................................10
Figure 6 - The Completed Project................................................................................11

Table 1 - Parts List .......................................................................................................10

Introduction
With this project I wanted to develop a platform to experiment with traffic flow. The
starting point was to create a simple set of traffic lights for a ‘T’ junction commonly
found on British roads. The lights also include a filter light on one set of lights
allowing traffic to turn from the main road into the side road. This will be explained
further later.

A sensor is used to detect when cars are waiting to turn into the main road. The lights
change for a set period of time and then change back. See the flow chart later for
timings and sequence.

The idea is to expand the project later to make the traffic lights more intelligent. As it
stands the lights do not detect traffic flow and cannot allow for heavy traffic coming
from any direction.

Traffic Lights – Perry Andrews Page 2


Design and Development
I started the design by drawing a road layout showing the junction complete with
traffic lights. I labelled the different directions A, B & C with C being the side turning
and B having the extra filter light. This drawing is shown below:

Figure 1 - Road Layout


The filter light is used to allow traffic to turn into the side road from direction B.
Traffic coming from direction A is stopped and the filter light indicates it is safe for
traffic from direction B to turn across the main road into the side turning.

From the road layout I could determine the basic steps required for the simple
simulator:

1. Initialise the PIC


2. Change the lights on the main road to green
3. Wait for a car to arrive in the side turning
4. Turn on the filter light for a period of time
5. Change the lights in side turning to green for a period of time
6. Return to step 2

The flow diagram below contains this logic complete with the times:

Traffic Lights – Perry Andrews Page 3


Start
Traffic Lights

Turn off all led's


Initialize Turn on all led's wait 2 seconds
except Red

Change lights A Wait for 2 Wait for section Change lights A


to Red seconds C + B to Green

Wait for 2 Turn lights B Wait for 8


seconds filter seconds
Wait for 1
second
Wait for 1 Change lights B
second to Red

Change lights C Wait for 15 Change lights C


to Green seconds to Red

Figure 2 - Flow Diagram

The actual change of the traffic lights from red to green and back is not shown in the
flow diagram. The sequence in Britain is RED – RED & AMBER – GREEN and
GREEN – AMBER – RED. This is shown in the diagram below:

Figure 3 - Sequence Diagram

The reason red and amber are both lit together is so you can tell the next light in the
sequence is green and not red.

Traffic Lights – Perry Andrews Page 4


The C program is shown below:

/* Traffic Lights Simulator */


/* (c) Perry Andrews Sept. 2004 */
/* 1st Revision */

void wait (int t)


{
unsigned int i ;
unsigned int j ;
for ( j=0; j < t; j = j + 1 )
for ( i=0 ; i < 35200 ; i= i + 1 ) ;
}

void changeToGreen (char lght)


{
// wait 1 second
wait(1);
if ( lght == 'A' )
{
output_high_port_b(1);
wait(2);
output_low_port_b(0);
output_low_port_b(1);
output_high_port_b(2);
}
if ( lght == 'B' )
{
output_high_port_a(1);
wait(2);
output_low_port_a(0);
output_low_port_a(1);
output_high_port_a(2);
}
if ( lght == 'C' )
{
output_high_port_b(5);
wait(2);
output_low_port_b(4);
output_low_port_b(5);
output_high_port_b(6);
}
if ( lght == 'D' )
{
output_high_port_a(1);
output_high_port_b(1);
wait(2);
output_low_port_a(0);
output_low_port_a(1);
output_high_port_a(2);
output_low_port_b(0);
output_low_port_b(1);
output_high_port_b(2);
}
}

Traffic Lights – Perry Andrews Page 5


void changeToRed (char lght)
{
// wait 1 second
wait(1);
if ( lght == 'A' )
{
output_low_port_b(2);
output_high_port_b(1);
wait(2);
output_low_port_b(1);
output_high_port_b(0);
}
if ( lght == 'B' )
{
output_low_port_a(3);
output_low_port_a(2);
output_high_port_a(1);
wait(2);
output_low_port_a(1);
output_high_port_a(0);
}
if ( lght == 'C' )
{
output_low_port_b(6);
output_high_port_b(5);
wait(2);
output_low_port_b(5);
output_high_port_b(4);
}
if ( lght == 'D' )
{
output_low_port_a(2);
output_high_port_a(1);
output_low_port_b(2);
output_high_port_b(1);
wait(2);
output_low_port_a(1);
output_high_port_a(0);
output_low_port_b(1);
output_high_port_b(0);
}
}

void filter (void)


{
// wait 2 seconds
wait(2);
output_high_port_a(3);
}

void init (void)


{
// Turn on all lights
output_high_port_a(0);
output_high_port_b(0);
output_high_port_a(1);
output_high_port_b(1);
output_high_port_a(2);

Traffic Lights – Perry Andrews Page 6


output_high_port_b(2);
output_high_port_a(3);
output_high_port_b(4);
output_high_port_b(5);
output_high_port_b(6);
// Wait 2 seconds
wait(2);
// Turn off all but red lights
output_low_port_a(3);
output_low_port_a(2);
output_low_port_b(2);
output_low_port_a(1);
output_low_port_b(1);
output_low_port_b(6);
output_low_port_b(5);
}

unsigned char key ( void )


{
unsigned char count = 0 ;
unsigned char oldv, newv ;
oldv = input_pin_port_b(7) ;
while ( count < 20 ) {
newv = input_pin_port_b(7) ;
if ( oldv == newv ) {
count++ ;
}
else {
count = 0 ;
oldv = newv ;
}
}
return oldv ;
}

void main (void)


{
/* Select the Register bank 1 */
set_bit ( STATUS, RP0 ) ;
/* set bits 3 + 7 of PORTB for input */
TRISB = 0x88 ;
/* set bit4 of PORTA for input */
TRISA = 0x10 ;
/* now use Register bank 0 */
clear_bit ( STATUS, RP0 ) ;

// Test Lights
init();

// Main program loop


while (1) {

// change lights A + B to Green


changeToGreen('D');

// Wait for Switch B7


while ( key () == 0 );
while ( key () == 1 );

Traffic Lights – Perry Andrews Page 7


// Change lights A to red
changeToRed('A');

// turn on filter B
filter();

// wait 5 seconds
wait(8);

//change lights B to red


changeToRed('B');

// change lights C to green


changeToGreen('C');

// wait 10 seconds
wait(15);

// change lights C to red


changeToRed('C');

}
}

The program has seven sub programs including ‘main’ which all C programs must
have. ‘main’ is the first program to start and this is where the main program logic
resides. The steps are labelled and loosely tie up with the steps on the flow diagram.
This should make it easy to follow. The first sub program to be called is ‘init’ which
turns on the LED’s to test. The other sub programs are:

• changeToGreen – which changes the set of lights from red to green


• changeToRed – which changes the set of lights from green to red
• wait – pauses program operation for a number of seconds
• key – detects the button pressed

The changeToRed and changeToGreen sub programs are supplied with a character
denoting which set of lights to change A, B or C. There is a forth option D which
changes both A and B together.

The program uses about 350 blocks of the 1024 blocks allowed.

Traffic Lights – Perry Andrews Page 8


The circuit diagram is shown below and is similar to the other PIC circuits I have
designed:

Red

+5V
Lights A
Orange

100R 1K 14
+VE 6 Green
4 RB0
MCLR 7
SW1 RB1
8
13 RB2
RB7 17
RA0
PIC 16F84 Red
16 18
OSC1 RA1
100n 1
22K RA2 Orange
3.2768MHz 2 Lights B
RA3
15 10
OSC2 RB4 Green
11
GND RB6 RB5 Green
5 12
10p 10p

Red
Lights C
Orange

Green
10 x 270R
0V

Figure 4 - Circuit Diagram

Traffic Lights – Perry Andrews Page 9


Construction
After testing the program on the PIC development board I collected together all the
required components and built the test circuit on breadboard. This is an easy way to
test all the components and to give an idea of layout in the final permanent
construction. The breadboard is shown below:

Figure 5 - Prototype

This project was constructed using Vero strip board, as it is quick to work with. A
parts list is shown below:

Description Quantity Maplin Stock Code


Vero Strip Board 1 pc 100mm x 60mm JP49D
PIC16F84A 1 VS87U
Red LED’s 3 WL27E
Orange LED’s 3 WL29G
Green LED’s 3 WL28F
Green Triangle LED 1 YY55K
3.2768MHz Crystal 1 FY86T
10pf Capacitors 2 RA33L
100nf Capacitor 1 RA49D
1K0 Resistor 1 M1K
100R Resistor 1 M100R
22K Resistor 1 M22K
270R Resistor 10 M270R
PCB Switch 1 KR88V
18Pin DIL Socket 1 HQ76H
Table 1 - Parts List

Traffic Lights – Perry Andrews Page 10


I used a crystal as the timing source because of its accuracy. The R-C network would
have worked just as well in this application and would have been a bit cheaper. If this
is desired then replace the crystal with a resistor and variable resistor. The variable
resistor can then be altered to adjust the timing.

To start constructing the project I marked the positions of the LED’s. I grouped the
LED’s together as sets A, B and C. Set A is top right, B is top left and C is at the
bottom. This order is the same as shown on the road layout. Next the position of the
IC socket was found.

The socket was orientated so that the outputs used for the LED’s were in the best
position for the LED’s. I also located pin 5 of the socket on the same copper strip as
the LED cathodes to save an additional jumper wire. The socket was then soldered in
place and the copper strips cut between the pins of the IC socket. The resistors were
soldered in next along with the jumper wires. Next the switch and capacitors were
located and soldered in place. Last were the LED’s and the crystal.

Figure 6 - The Completed Project

All that was left to do was to plug in the PIC and connect the 5V supply to test.

Traffic Lights – Perry Andrews Page 11


Conclusion
The aim was to produce a platform for further study and development of Traffic Light
Simulation. The program was kept simple to allow the project to be completed in a
short space of time. The project took about ten hours to complete including the
program development and construction. The construction was the longest part of the
project and more time spent here reduced the possibility of errors. The circuit design
was simple which also made construction easier compared to a version using digital
electronics.

My initial timings were reviewed after programming and running a test using the
development board. I found that one second between lights changing was not enough
so I increased the time to two seconds. The change of lights is much smoother
because of the change. I also increased the length lights ‘C’ were green from ten to
fifteen seconds.

Below I have listed some future developments for this project:

1. Add buttons for lights ‘A’ and ‘B’ so the traffic can be detected on the main
road.
2. Change the program to detect traffic flow to determine the time each set of
lights are green.
3. Make the filter light work more intelligently by detecting if traffic is waiting.
4. Add another set of lights to work on crossroads.

References
Matrix Multimedia Ltd
C for PICmicro microcontrollers by Rob Miles
PIC Tutor development board

Maplin Electronic Supplies


Supplier of all components including the PIC development board.

Crownhill Associates
Supplier of PIC developer boards and PIC Micro’s.

Traffic Lights – Perry Andrews Page 12