My design goals for this project were:
1. It must be kid-proof - including an inquisitive toddler.
2. It should be properly compatible with Duplo sets.
3. It must be an authentic traffic light sequence - in the UK, that's Red, Red+Amber, Green, Amber, Red.
4. The battery should last a long time between changes.
5. It must be cheap!
In total, not counting the cost of the Lego parts (which were all scavenged from sets lying around the house) this traffic light cost me less than £5 UKP. Here's a video of it in action:
This is my first Instructable, so bear with me!
Step 1: Ingredients
Here are the bits you'll need to make this traffic light. Obviously you can vary according to personal taste or what you have lying around - all of these things were scavenged from odd lego sets and electronic parts that I had already.
2x standard Lego 2x4 black bricks (part code 3001)
1x Lego Technic black ribbed hose (part code 78). I used this for the 'pole', as it's flexible so much harder for kids to break!
1x Lego Traffic Cone (I couldn't find a part number, but it's in this cheap set: 5679)
1x Lego Duplo 2x2x1 black square brick (part code 3437)
1x 5mm Red LED
1x 5mm Yellow LED
1x 5mm Green LED
(I think that using LEDs with coloured, rather than clear, plastic gives the best effect)
1x small tactile pushbutton with a round button (like this)
1x CR2032 coin cell
1x CR2032 battery holder (like this)
1x ATTiny45 or ATTiny85 microcontroller
2x very small machine screws + bolts (I think I used M3 x 25mm)
A blob of BluTac or similar putty-like substance
A small length of hookup wire - preferably solid core rather than stranded. Having multiple colours, especially red, yellow and green, helps.
As for tools, you'll need:
Soldering iron and solder
Wire strippers and cutters
Drill or Dremel with a selection of small bits, including 5mm.
Superglue (be careful at all times not to glue your skin - that stuff sets FAST and strong).
Hot glue gun
Some way of programming the microcontroller - you can use an Arduino (using these instructions), or a dedicated programming tool such as an USBtinyISP.
A breadboard or other electronic prototyping environment, for testing the program.
Step 2: Programming the Microcontroller
The microcontroller is the mini computer that provides the brains for the traffic lights. We need to make sure that we've got it working before we start assembling anything.
First of all, download the code - attached to this article. Flash it to your ATTiny using your chosen programming environment. If your environment allows you, you should also set the fuses in the ATTiny to use the 1MHz internal clock, and no brownout detection (lfuse=0x62,hfuse=0xDF,efuse=0xFF). This isn't critical, but will improve your battery life.
Now plug your ATTiny into a breadboard, connect up the LEDs and switch as in the circuit diagram. When connecting the LEDs, connect the flat side of the LED to GND, and the other side to the pin on the ATTiny. Now connect up the battery holder, and insert the coin cell. You should see the red LED light up. Press and release the switch, and it should go to red+yellow, then green. Press and release the switch again, and it should go to yellow, then red. If you're unhappy with the timing, then you can tweak the TRANSITION_TIMEOUT constant.
Once you're happy that it works as you'd like, unplug the power and we'll move on to building it into the Lego blocks.
Step 3: Check That Everything Fits
There's not much space to play with in this project, so it's important to make sure that you know how you're going to fit everything in.
First of all, check that your LEDs will fit the 3 round tubes on the bottom of one of the 2x4 bricks. It'll probably be a little too tight at this stage, but almost fit. That's fine.
Check that your switch and battery holder will fit completely inside the traffic cone - the switch goes in first, and the battery holder will probably need to go in diagonally.
Finally, check that your hose (or other choice of 'pole') fits snugly in the top of the traffic cone. It might not push all the way in yet - that's OK, so long as it fits the opening.
Step 4: Mounting the Microcontroller
To keep things small, we're going to solder the microcontroller directly to the battery holder. Start by turning the battery holder over, so that its pins are in the air. Identify the VCC and GND pins on the microcontroller - these will connect to the + and - pins on the battery holder, respectively. Turn the microcontroller so its pins are pointing up in the air, and bend those two pins outwards towards the battery pins. You should be able to bend the battery pins over, so that both pins on the microcontroller touch both pins on the battery holder. Make a note of its position.
At this point, add a dab of superglue to the top of the microcontroller, and glue it carefully and quickly into place on the battery holder. Now solder the two connections.
We also want to connect the reset pin to VCC. It's directly opposite that pin, so solder a small wire straight across.
Identify the red, yellow and green LED pins, and solder wires to those. You should allow about 6 inches of wire. It'll be a lot easier later if you colour-code those wires.
Identify the switch pin, and solder a 3 inch wire to that. Solder the same length of wire to the GND pin (which is already connected to the battery holder). Finally, solder a second, 6 inch wire to the GND pin.
At this point, if you're paranoid, you can try temporarily connecting up the rest of the circuit again, to check that it still works. If you're happy, snip off the unused pin on the ATTiny.
Step 5: Mounting the LEDs
First we want to mount the pole onto the traffic cone base. My traffic cone had a ring of small protrusions inside the stud on top, which were just smaller than the diameter of the ribbed hose. The opening of that stud was exactly the right width to accept the hose. I found that to insert the hose fully, I had to make a shave a little bit off 4 sides of the end of the hose, to align with the protrusions in the opening. I could then insert the hose, and twist it to kind of lock it in place. You could also make a slit in the end, to give it enough flexibility to get past the protrusions.
Once you're happy that the hose fits, add a dab of superglue and permanently fix it in place. You should find that it's pretty much impossible to pull it out - an important bit of kiddyproofing.
Next step is to mount the LEDs in the two small black 2x4 bricks. You'll have realised by now that we're going to mount the LEDs in the 3 small tubes in the bottom of the brick - pushed in from the top of the brick. First step is to drill the holes in the top of one of the bricks. If there's any difference between your two bricks, then choose the one that's got the nicest looking bottom.
Grab your drill / dremel, and a 5mm bit for drilling metal. Drilling slowly and carefully, drill through the top of the brick, in the space exactly between 4 adjacent pips. You'll probably find that your drill bit is slightly larger than the internal diameter of the tube, in which case just drill far enough into the tube to allow an LED to fit snugly inside, with its base flush with the top of the brick. Repeat for the other two tubes.
With your other 2x4 brick, first hack out most of the length of the underside tubes - they'll just get in the way of the back of the LEDs. Check that your two bricks fit snugly together, one on top of the other, with an LED in place.
You also need to make a hole for the pole. I used a drill with a bit just the same size as the external diameter of the ribbed hose piece, just touching the edge of the brick, as seen in the picture. I then also hacked out a small groove in the plastic on the top of the LED brick, to make sure that the two bricks still fitted snugly together with the hose in place.
Now stick the LEDs properly into their holes (in the right order - red, yellow, green!). Line up their flat sides so that they're all along one side of the brick. Bend the legs on the flat side so that they lie flush to the brick, and touch each other along the length of the brick. Solder those legs together, and trim off any excess wire. Now bend the other legs flush to the brick, and trim off all but a very short length of wire, making sure that they're not touching each other or any other wires.
Pass the red, yellow, green and long GND wire through the hole in the middle of the pole piece, and push the battery holder into the base of the cone. Ensure that you'll have enough wire to connect through the pole and to the LEDs, allow a small amount (1-2 inches) of slack, then trim those wires to the right length. Solder the GND wire onto the interconnected flat-side LED legs, and the other wires onto the other legs of the appropriate LEDs.
At this point, let's be paranoid and check that everything still works if we stick the battery in the holder.
If it's all good, then check that the two 2x4 bricks still fit together snugly with the pole in place, then add a dab of superglue to the pole/brick joint, and check that it holds securely. Now add superglue to the joint between the two bricks, and join them together permanently. Wipe off any excess glue from around the join quickly, before it sets.
Step 6: Mounting the Switch
The next task is to mount the switch to the base. I chose to mount it quite high, on the slope of the cone - you could put it anywhere that you can make it fit (making sure that the battery holder etc. can also still fit inside the base).
Most of these small pushbuttons come with four pins. By now, you now which two pins you're going to use (since you've tested the circuit) - usually it's two on the same side of the body. Trim off any unused pins.
Measure the size of the part of the button that you actually press. On mine, it was a circle roughly 5mm in diameter. You want this hole to be as snug a fit as possible (remember the childproofing - you don't want drool getting inside!), so choose a drill bit that's as close as possible in size. Using an appropriate drill, drill a hole into the cone where you want the button. Check that it fits from the outside first, and make any necessary adjustments.
Solder the remaining two wires from the microcontroller onto the switch. Stuff it inside the base, and check that the button can still be pressed (though you might need to stick a finger inside the base to hold it still at this stage. Now that all the electrical connections are made, you can test the circuit again to be sure that it's still working, and that the switch does the right thing. At this point, once you've verified that everything is still working, it's a good idea to stick a copious amount of hot glue over the pins of the ATTiny. That helps to avoid the stresses of being stuffed into the base from dislodging any of the connections.
All fine? Good, now you need to make sure that the switch is now secured in position. I used a large blob of Blu Tack, stuffing it into the top of the base, behind and around the switch, bit by bit, until the switch was securely wedged into position. Now stuff the battery holder (making sure you've inserted the battery) and microcontroller assembly into the base, maybe adjusting the Blu Tack wadding as necessary. You need to end up with everything packed in as tightly as possible, at a minimum with nothing protruding beyond the level of the bottom of the base - you should be able to stand it on the table, and it should make full contact. I found that I could just achieve this - but that I wasn't able to attach the cone properly on top of a Duplo brick - the battery holder poked down onto the studs of the brick and stopped them from fully engaging with the cone.
At this point, if this isn't to give to kids, and you're not worried about being able to stand the traffic light properly onto Duplo, then you could stop and consider the project finished. But I needed to be able to seal the base from prying fingers, whilst still being able to change the battery occasionally, and be able to attach it to Duplo. Read on for how I achieved that...
Step 7: Mount the Base
The first task in sealing the base is to make sure that the cone and the 2x2 brick used for the very bottom of the light can fit together properly.
What I found was that I'd wedged the battery holder in pretty well, but that one corner of it was still interfering with one of the studs on top of the 2x2 brick that I wanted to use for the base. So, I just used a craft knife to carefully slice that stud off from the 2x2 brick. With it gone, the cone and brick fitted together perfectly. Do whatever you need to do.
To attach the 2x2 and the cone together semi-permanently, we're going to use the machine screws. Check that the screws, and the nuts that they connect with, are small enough to fit inside the Lego bricks. If you've got the right size, then the screw head will fit perfectly in the gap between the slope of the cone, and the corner of the base of the cone. Measure the diameter of the threaded part of the screw, and find a drill bit to match. Carefully drill holes in opposite corners of the lip of the cone, where the screws will go through it.
Check you're happy with how the screws sit, then connect the 2x2 to the cone. To make sure that the holes will line up properly, pass your drill bit through the hole in the cone, then drill again vertically until you've made a hole in the 2x2. Repeat for the other hole. Check that the screws still fit well.
If you're using the same size screws as me, you'll probably find that if you try to fit the 2x2 onto another piece of Duplo now, then the screws are just a bit too long, and interfere with the studs on the other piece. If so, use a hacksaw, or a Dremel cutting wheel, to cut the screws a little shorter. Be careful not to overdo it though - you only need to cut off a small amount.
You should now be able to fit the nuts onto the screws. This is quite fiddly, and a pair of tweezers may help to hold the nut in position for the first couple of turns of the screw. Tighten the screws (but not too tight - I managed to crack a brick the first time I did this). Although tempting to avoid small parts coming loose, I don't advise using threadlock to secure the nuts - many threadlock compounds are toxic, and your kids are sure to put this traffic light in their mouths at some point.
Step 8: All Done!
At this point, you should have a finished, working traffic light. Congratulations!
Before handing it over to your kids, check that there are no sharp edges, loose pieces or surplus glue. If necessary, file down any sharp edges on the screw heads.
My calculations predict a battery life of 2-3 months under average use. So far the battery in mine has lasted well over a month of heavy use, and is still going strong.
Thanks for reading this far, and I hope you enjoy your new toy!