Sunday, August 30, 2009

Toy Traffic Lights



This is something I've been meaning to make for ages. My kids love toy cars (ok, lets face it, all kids love toy cars!). There's been lots of car related fun in our household (some of which has been documented on filthwizardry: "Toy cars and trucks from recycling", "shower curtain village playmat" and "home made toy carwash") and one of the kids favourite pastimes in the car is to shout "RED means STOP!" and "GREEN: GO!" (as loudly as humanly possible). I figured it was about time I used this as an excuse to spend some time in the garage making something with flashing LEDs :)

Between Lin and myself we've managed to amass quite a bit of cheap 'junk' that can be hacked together for something like this. I was going to make something flimsy using a battery pack and a drinking straw, but I was persuaded to make something a bit more kid-proof using a dollar store wooden box, an Altoids tin and the tube from a black felt-tip pen. Here's a picture of all the bits (there are three 150 ohm resistors are missing):



The plastic domes are the discarded remnants of 25c kids toys purchased from a vending machine in our local Taqueria, they looked like they'd make good LED covers (and they were used as wheels in another crafty distraction: "Toy cars and trucks from recycling"). The chip is an ATtiny13.

First up was to drill some holes: two in the box, one on the side for the switch and one on the top for the pen tube; one in the base of the altoids tin (again, for the pen tube) and three in the back of the tin for the LEDs.

Then I painted it black.



The wiring for the base/box is pretty simple, just an AA battery pack wired to a switch with the main wires threaded out of the hole in the top of the box. I stuck the pen casing in at this point and threaded the wires through to the top.



Now it's time for the wiring.

First off, I hot glued the base to the Altoids tin. Then I glued the LEDs into their holes with the cathodes all facing in one direction so I could solder them together to form a ground rail.




I soldered on three, 150 ohm, resistors to an 8-pin dip socket (pins 5, 6 and 7), soldered the positive lead from the base to pin 8 on the socket. Then I connected the LEDs to the resistors and finally connected the ground pin (pin 4) to the ground rail. That's it!





When I was writing the code I didn't think ahead about which pins were going to be connected where in the final project and ended up with the red and green pins mixed up (in software), so the initial sequence was backwards; but then that's why I used a socket rather than directly soldering the uC ;). Easy to fix with a quick software update.

Here's the final working project (yes, the yellow works too, I just didn't take a picture of it):



I've included the code below. As always, it's pretty simple:

### code starts here ###
(I found a nice way of displaying code in blogger. I talk about setting it up here)

/*
* trafflic_lights.c
*
* Created on: Aug 29, 2009
* Author: Paul
*/
#include <avr/io.h>
#include <avr/delay.h>

#define RED_LED PB2
#define YELLOW_LED PB1
#define GREEN_LED PB0

#define RED_DELAY 10
#define YELLOW_DELAY 3
#define GREEN_DELAY 15

uint8_t i = 0;
uint8_t j = 0;
void delay_seconds(uint8_t pSecs)
{
for(i = 0; i < pSecs; i++)
{
for(j = 0; j < 4; j++)
_delay_ms(250);
}
}

int main(void)
{
DDRB = 0xFF;//set all to output

//start traffic light sequence
while(1)
{
PORTB = (1 << RED_LED);
delay_seconds(RED_DELAY);
PORTB = (1 << YELLOW_LED);
delay_seconds(YELLOW_DELAY);
PORTB = (1 << GREEN_LED);
delay_seconds(GREEN_DELAY);
}
}

10 comments:

Unknown said...

What is the chip it runs on? You don't really mention it in the article.

That said, it's awesome. I might have a crack at this myself.

PaulBo said...

Oops, thanks for pointing that out! It's running on an ATtiny13 (I only have these and a few ATmega8's at the moment). I'll update the article.

Unknown said...

Also, what programmer are you using (as in, the hardware).

I know a bit about electronics, though I haven't done much in 15 years. Ive been getting interested lately, and may be getting a kit from sparkle labs for Father's Day, to start learning more about it.

PaulBo said...

Hey, that's great! I got the sparkle labs kit about 7-8 months ago for exactly the same reason. I love the way it was put together and I enjoyed working through the projects.

I put together a few blog posts about my experiences learning how to use the AVR microcontrollers, they cover the set-up I've been using:

http://fangletronics.blogspot.com/2009/04/starting-with-avr-microcontroller.html

http://fangletronics.blogspot.com/2009/05/basic-avr-io.html

I'm planing on putting up a few more posts on working with the AVR soon; something on inputs I think. I'm hoping to be inspired to try and make something interactive for my kids to play with :)

Unknown said...

Excellent, thanks for the links.

I was also a little bewildered by the choices (arduino, etc), and your posts make it a little clearer.

Anyway, I'll subscribe to your blog, and I'll do the sparkle kit stuff, and go from there. Thanks again.

Zak said...

Fun! I might build one of these, but I'll add a dip switch to select the cycle you want. (Eg. Flash red / flash yellow / US cycle / UK cycle, etc.)

Paul Tunstall said...

I have been looking for a pair of wireless traffic lights that emit a signal to the other one when its time to change. I have two little boys who would LOVE a pair of lights to control their junction. There must be millions of kids that would like a set. We should get them manufactured in China and sell them worldwide? Paul.tunstall1@btinternet.com (in london, UK)

Kiwisaft said...

actually traffic signs work like this:
loop{
red
red+yellow
green
yellow
}
;)

Unknown said...

They work like that in the UK, not in the US though.

Neat stuff though, you're on hackaday.

PS: stay away from the comments there, everyone is still complaining about everything.

Unknown said...

Since in the US each light is on by itself you could save 2 resistors and just use a single one for all 3 LEDs. (Or use no resistors and modulate the LEDs with PWM and have the batteries last longer). Neat project, I think I will build one for my 4yo to play with and my 1yo to taste.