Skip to content

Build a Custom Toy Traffic Light

In this tutorial I’m going to show you how to build a custom toy traffic light using an ATtiny13A and a custom 3D printed traffic light model.

Watch the tutorial video on YouTube then read the rest of the article for additional details.

Bill of Materials

3D Model

I designed this 3D model in Tinkercad and printed it on a Lulzbot Mini. It is made up of 3 sections. A top that holds the LEDs, a middle section that hides the wires for the LEDs, and a base that holds the electronics and power button. The 3 sections all snap together and can be glued for a more secure fit.

The top section has a front panel with three 5mm holes for the LEDs. The panel snaps into the body which has a hole for the wiring and for the middle section to snap on to.

The middle section has a back plate that snaps into place to hide the wires and it snaps into the top section and base.

It all comes together with the base, that holds the power button and all the electronics. It has a top that snaps into place.

Circuit

The traffic light circuit is very simple.

The ATtiny13A is powered by the CR2032 battery, positive terminal connects to pin 8 and negative terminal connects to pin 4. There is a power switch in place between pin 8 and the positive battery terminal. Pins 1-3 are not used.

The green LED is connected to PB0 (pin 5), the yellow LED is connected to PB1 (pin 6), and the red LED is connected to PB2 (pin 7). I used the 48 ohm resistors with the yellow and red LEDs to drop the voltage to a safe level. The green LED I am using works fine with 3V. Check your LEDs datasheet to determine the voltage requirements, not all LEDs are the same, you might need to add a resistor for your green LED.

Source Code

The traffic light program is also very simple.

#define	GREEN_PIN PB0
#define YELLOW_PIN PB1
#define RED_PIN PB2

int main(void) {
    DDRB = 0b00000111;
    PORTB = 0b00000000;
    
    while (1) {
        PORTB = (1 << GREEN_PIN);
        _delay_ms(30000);
        PORTB = (1 << YELLOW_PIN);
        _delay_ms(10000);
        PORTB= (1 << RED_PIN);
        _delay_ms(15000);
    }
}

First set PB0, PB1, and PB2 as outputs and set all the pins to LOW.

Now loop indefinitely:
Turn on the green pin for 30 seconds, then the yellow pin for 10 seconds, and finally the red pin for 15 seconds.

I used MPLAB X to compile the source code and the PICKit 4 to program the ATtiny13A. I will write another tutorial for how to use MPLAB X and a PICKit 4 with an ATtiny and link it here later.

Test on Breadboard (optional)

You can assemble the circuit on a breadboard if you’d like to test it out first. Follow the schematic and the image below.

Building

You will need to print your own Traffic Light 3D model or build one out of wood or cardboard.

Step 1: Add LEDs and wires to top section

Insert the 3 LEDs into the front plate with the leads all in the same direction. Bend the cathode (negative) leads down and solder them together. Add a wire to serve as a common ground for the LEDs. Attach a wire to each anode (positive) lead. Use a piece of heat shrink tubing on each anode to prevent a short. See the images below for an example.

Once everything is in place, attach the face plate to the body and pull all the wires through the bottom hole.

Step 2: Add middle section

Take the wires and run them through the middle section and snap on the back plate. Then snap the middle section into the top section. It should look like this:

Step 3: Attach to bottom section

Pull all the wires through the hole in the bottom section and snap the middle section in place. Like this:

Step 4: Add power switch to base

Solder two wires to the power switch and push it through the hole in the base.

Step 5: Assemble components on perfboard

Solder the the 8-pin DIP socket, resistors, and battery holder to the perfboard. Note that I ended up using a vertical CR2032 battery holder to save space. Attach all the wires from the LEDs and power switch to the appropriate pins. Follow the circuit schematic above.

Step 6: Compile source code and program the ATtiny13A

Download the source code and compile using MPLAB X (or your preferred IDE). Use a programmer such as the PICKit 4 to program the chip. I will create a separate tutorial on how to do this and link it here later.

Step 7: Insert the ATtiny13A and battery

Carefully insert the ATtiny13A into the 8-pin DIP socket and add a CR2032 battery. Remember to pay attention to the battery polarity. Here is what mine looks like:

That’s it! Now turn on the power switch and watch the traffic light cycle through all the LEDs.

Ideas and enhancements

Pins 1-3 are free on the chip so you have room to improve this project. Here are some ideas:

  • Add a button to move to the next light.
  • Add a potentiometer to control the duration the lights stay on.
  • Add a speaker and sound effects, see my Musical Death Star and Make a Musical Birthday Cake tutorials to learn how to add sound.
  • Add different traffic light patterns.

Leave a Reply

Your email address will not be published.