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).
- 100001;
- 010001;
- 001001;
- 001011;
- 001100;
- 001010;
- 001001;
- 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.