Introduction: Flickering LED Candle
Take a dollar-store "flickering" LED candle, add an AVR ATtiny13 and a little code, and you get an LED candle that looks almost real.
Step 1: Open Up the Candle Casing
A thumbnail seemed to be the best tool for this job. The casing isn't glued. There's just a friction-fit post that goes into a receiving hole in the cover. Work around the cover edge and the base part will begin to come loose. Don't get into a hurry because the wires connecting to the LED module inside are very fine and easy to break. We'll be re-using these wires, so be careful.
Step 2: Remove the LED Module
The LED, connected to its base, is friction-fit into the base of the plastic candle flame. Twist slightly and pull to remove. Make note of the wire colors, as they may be different from the unit I used. I'll be using "yellow" for the negative and "red" for the positive.
Step 3: Move the Cathode Wire
We won't be using the original circuit, which is a low-side switch that just flickers the LED off for a few milliseconds periodically. Carefully unsolder the yellow wire and move it to the LED cathode on the middle pin. The wire is really fine. Use a hot soldering iron to melt the original connection. Add a little fresh solder to the center pin. Then you can hold the wire against the center pin and reflow the solder joint easily.
Step 4: Program Your Chip
We'll be cutting off the unused pins of the ATtiny13, so be sure to program the chip before you do that! I use a USBtinyISP programmer and a SparkFun breakout board in a solderless breadboard. We're using the internal oscillator of the tiny13, so there's no need to burn any of the programming fuses. You can use the hex file provided or compile your own with the provided source code.
Some notes about the source code: I used a generic random number generator because the stdlib rand() function is almost twice as large. When you only have 1024 bytes of Flash memory, every byte counts! Also, the millisecond timer doesn't seem to line up with real wall-clock time. But since exact timing isn't really important in this application, I just eyeballed the timing. Purists might cringe, but I'm a pragmatist. :)
To program using the supplied hex file on a Linux system, use this command line:
avrdude -p attiny13 -P usb -c usbtiny -U flash:w:flicker.hex
WinAVR users will probably know the right incantation. I don't do Windows. :D
Update: flicker2.zip contains the second version of the code, featuring two flicker patterns (flicker-up and flicker-down), along with adding watchdog protection to reset the chip if the mainline code should freeze.
Some notes about the source code: I used a generic random number generator because the stdlib rand() function is almost twice as large. When you only have 1024 bytes of Flash memory, every byte counts! Also, the millisecond timer doesn't seem to line up with real wall-clock time. But since exact timing isn't really important in this application, I just eyeballed the timing. Purists might cringe, but I'm a pragmatist. :)
To program using the supplied hex file on a Linux system, use this command line:
avrdude -p attiny13 -P usb -c usbtiny -U flash:w:flicker.hex
WinAVR users will probably know the right incantation. I don't do Windows. :D
Update: flicker2.zip contains the second version of the code, featuring two flicker patterns (flicker-up and flicker-down), along with adding watchdog protection to reset the chip if the mainline code should freeze.
Attachments
Step 5: Trim the Chip Legs
Since we're only using pins 4, 5 and 8, trim off the rest of the pins with a set of flush cutters.
Step 6: Make Some Connections
The red (positive) lead was cut in an earlier step. Now you will strip about 3/16 of an inch of insulation from each of the free ends of the red lead. Then tin the exposed wire. Tin the remaining pins on your tiny13 chip, too. This makes attaching the fine wires much easier, because you can hold the wire against the chip pin and reflow the solder joint with a hot soldering iron.
The red lead from the LED module connects to pin 5. The red lead from the battery goes to pin 8.
For the ground connection, use a fine-pointed needle-nose pliers to bend the pin in a "U". With a sharp utility knife, score the insulation of the yellow (negative) wire and pull it apart to expose a small section of bare wire. Place that section of bare wire in the "U" you just bent and solder carefully.
The red lead from the LED module connects to pin 5. The red lead from the battery goes to pin 8.
For the ground connection, use a fine-pointed needle-nose pliers to bend the pin in a "U". With a sharp utility knife, score the insulation of the yellow (negative) wire and pull it apart to expose a small section of bare wire. Place that section of bare wire in the "U" you just bent and solder carefully.
Step 7: Add Some Insulation
Vinyl electrical tape makes a good candidate for insulating the exposed leads. Cut a narrow strip and slip it between the chip body and the pins, then fold it over. Once insulated, fold the pins over the bottom of the chip.
Step 8: Test Your Circuit
Now is a good time to install the battery and check your work.
Step 9: Put It All Together
Re-insert the LED into the bottom of the plastic flame. Tuck the chip into the case where it won't wedge against the on-off switch. Finally, put the base of the candle back, seating the post on the bottom into the socket in the cover.
Step 10: Look! a Flickering Flame!
If everything has worked well, you now have a flickering "flame" atop your LED candle. Brag to your friends. The units I bought came 2 to a package, so you could easily demonstrate a before-and-after.