DIY traffic light with PIC microcontroller.

Open source hardware project.

  • Automatic cycle of lights.
  • Super simple boost converter driven from PIC microcontroller for LEDs supply.
  • Easy construction.
  • Lights speed adjustable by potentiometer
  • Powered by 4xAAA or AA batteries. In theory can also be powered by LiPol cell.
  • Very small current consumption. Long battery life.
  • Push button for power ON/OFF and manual advance of lights.
  • Controlled by PIC16F1824 microcontroller
  • Fun thing to play with
The circuit:

 Traffic light circuit


This project was inspired by my daughters kindergarten. One day they showed up with some electronic parts including 555 timer and one led for each color and wanted a traffic light for the kids game. I was not happy with that design, and wanted some more power, so I designed a completely new circuit and printed circuit boards. Of cource with a PIC microcontroller. Fortuantely I found out at the time that it is possible to order the PCB-s of 3 different colors just suitable for traffic light. So I managed to come up with very nice circuit and overall design, that I am happy with. And hopefully children are also happy with it.

Technical description:

PIC microcontroller generates PWM signal for boost converter circuit (Q1;L1;D3). It is mainly constant frequency and constant duty cycle. Software only corrects the duty cycle a little for different lights so that all lights seem equal intencity. The 36V stabilitron D4 is important component for safety, it clamps the maximum voltage to 36V in case the leds are disconnected.  The boost converter for LEDs do not need much output capacitance because LED-s are not sensitive to current fluctuations. Also the output current is limited by inductor current, so all suits very well.

Q1, Q2 and Q3 are used for switching between the lights. Microcontroller again takes care of the timing.

RV1 potentiometer is used for adjusting the lights cycle speed. Microcontroller reads the potentiometer position and adjusts the cycle duration accordingly.

Circuit as a pdf here:trafficlight.pdf


The latest code uses the state machine approach for easily managing the lights sequence. I actually had to measure the real traffic light changing speed from the video made on the cross section. Interesting thing is most people do not know that for a period of time the red and yellow lights are both on before the green appears! Even those who have driven in the city for years!

PWM signal is generated by the PIC built is PWM module so that is really easy too. Only configure the parameters and go! PWM values are fixed values found out during testing so no difficult feedback system there. All plain and simple.

The one button is coded as follows:

  • Short push (when off) -- Power on. Actually wake up from sleep
  • Short push (when on) -- Advance to the next color manually.
  • Long push (hold button for little longer) -- Power off. The microcontroller is in sleep during OFF state. All other things turned off.

Firmware assembler source code here:mainfoor.asm

Compiled hex file for directly programming into PIC16F1824 here:valgusfoor.hex(rename downloaded the file -- remove .txt from the end)

Originally compiled under the MPLAB IDE v.8.92 but also MPLAB X should work very well.

Note that in the circuit diagram it shows PIC16F1823 but this is repaced with newer PIC16F1824 in the final circuit!

And yes the firmware is quite state of the art and maybe a little overkill for so simple application but I had a good time coding and recoding it ;) Eventually I am happy with it!

 Printed Circuit Boards:
3D thumb

I made the layout in the Kicad Pcbnew and ordered the three different color boards from the PCB factory -- Red,Yellow,Green. This way I got exactly one mainboard for every 3 light boards. Also the mainboards are in 3 different colors to choose from.

 Board in KiCad

Here is how the board looks in the Kicad Pcbnew.

The original Kicad files here (and also Gerber files for ordering PCB-s from PCB manufacturer)

The boards need to be cut open with saw. Could be made with V-groove but I did not use it because the V-groove panelizing is at extra cost for the prototype PCBs. So design is originally for cutting the boards with hacksaw (later I turned to small narrow kerf circular saw).


Enclosure is most easy to make form empty carton juice containter. Holes cut and boards glued in with hot glue gun. All covered with black or gray carton. Sun shades also forom carton glued to the lights. Hatch made to the back to access the boards and the batteries. With pin headers soldered to the boards the components are then easily wired together with jumper wires.