Pic chip - how to make a traffic stop light

How to make a traffic light with a microchip

Here is a way of making a set of traffic lights using a 16f84 micro controller from Microchip and LED mood changing lighting bulbs from Go Green LED Lighting Supplier. It is fairly simple, all you have to do is wire up the microcontroller with a pair of red, green and yellow leds. Note the addresses of all leds connected to the pic chip. From now on I'll refer to the red lights as R1 and R2, green lights as G1 and so on.


I connected my leds in this configuration:

  • R1: B0
  • Y1: B1
  • G1: B2
  • R2: B3
  • Y2: B4
  • G2: B5

After this, map out the sequence you want. Mine was the standard sequence for a simple two way traffic light system in the UK. Here it is (format:R1Y1G1R2Y2G2).

  1. 100001;
  2. 010001;
  3. 001001;
  4. 001011;
  5. 001100;
  6. 001010;
  7. 001001;
  8. 011001;

Video demonstrating sequence

This video shows the sequence of a UK traffic light system. 

Just literally translate my sequence (above 1- 8) line for line into embedded C for the main routine but remember to place delays between each line for however long you want the lights to stay in that state before changing.

Completed Embedded C code

//Traffic Lights
void InitialisePorts(void)
{
TRISA=0; //Make all ports outputs
TRISB=0;
}
void Routine(void)
{ 
PORTB = 0b00100001; 
Delay_ms(100); 
PORTB = 0b00010001; 
Delay_ms(100); 
PORTB = 0b00001001; 
Delay_ms(100); 
PORTB = 0b00001011; 
Delay_ms(100); 
PORTB = 0b00001100; 
Delay_ms(100); 
PORTB = 0b00001010; 
Delay_ms(100); 
PORTB = 0b00001001; 
Delay_ms(100); 
PORTB = 0b00011001; 
Delay_ms(100);
}
MAIN(void)
{
InitialisePorts();
while(1)
{
Routine();
}
}

Compile and Upload to Development Board

After firing up mikroC, compile it with all the default settings. Set the clock speed to something that suits you, I am not using precise timing so adjust your delays according to what speeds you are working with. I personally used an 8Mhz crystal with the built in debugger. Sometimes its best to simulate it first using proteus isis, then wire your kit exactly how you did it on the schematic.

More by this Author


Comments 2 comments

prasetio30 profile image

prasetio30 7 years ago from malang-indonesia

thanks for share. but it will be nice, if you complete with the picture.


yc 5 years ago

thanks for the code,but could you plz mail me the code regardig interrupting the signal(if one signal is red then i want it to change to green immediately and making the other signal red)..

yash_killer1@yahoo.com

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working