I’ve made plenty of LED Throwies in my day but I only recently discovered Karl Lunt’s low power LED firefly project. I was immediately inspired to make my own LEDfireflies. Unlike LED Throwies which will only last 4 or 5 days per battery, these LEDfireflies utilize ATTiny AVR microcontrollers and the LED as a light sensor (cool!) to get several weeks of flashing on a single battery.
LEDs are commonly used as lights (they are diodes tuned specifically to emit light and packaged in translucent enclosures) but they can also be used as photodiodes to detect light. If you have an Arduino kicking around, you can easily experiment with this concept. Check out Zorink’s Instructable for a step-by-step and also Mike Cook’s page for more info and a video demo.
This LEDfirefly project has a very short materials list which means you can build them for under $2USD per firefly though you will need to invest in an AVR programmer ($22USD) and will need some basic electronics tools.
Note: It’s possible to use an Arduino as an AVR Programmer though that’s outside of the scope of this tutorial.
- ATTiny13A (Google)
- 1 M ohm, 1/8-watt resistor (1/4-watt works too!) (eBay)
- CR2032 Battery Holder (TNRBattery)
- CR2032 Battery (Amazon)
- Diffused LED (Amazon)
- Soldering Iron and Solder
- Helping Hand (Amazon)
- Needle Nose pliers
- Small wire cutter
- USBTinyISP AVR Programmer (Adafruit) (Note: This kit requires a USB cable USB 2.0 A-Male to B-Male Cable)
- Board Vice (Amazon) (optional but helpful for assembling the USBTinyISP)
- Solder Sucker (Amazon) (optional but helpful for assembling the USBTinyISP)
- Jumper Wires (Amazon)
- Breadboard (Amazon)
Programming the ATTiny
The following tutorial was created on a Mac running OSX 10.8. However, you can also accomplish everything under Linux or even Windows.
1. First you’ll need to assemble the USBTinyISP Programmer. This is what you’ll use to speak with and upload files from your computer to the AVR microcontroller. LadyAda provides detailed instructions for how to do this. Note: when you’re installing the resistors to the board that you should use jumper wires (instead of of the supplied resistors) for R4 and R7.
2. Once you’ve assembled the USBTinyISP Programmer, you’ll need to download and install the CrossPack development environment for Atmel’s AVR microcontrollers. If you’re running Linux, refer to Lady Ada’s AVR Tutorial and if you’re using Windows, check out AVR Studio.
3. Connect the programmer to your computer via USB and open a terminal window (Applications > Utilities > Terminal). Test the interface to see if the programmer is recognized by entering the following command:
avrdude -p attiny13 -c usbtiny -n
It should send back the following message:
avrdude: initialization failed, rc=-1 Double check connections and try again, or use -F to override this check. avrdude done. Thank you.
This confirms that your computer is connected to and recognizes the USBTinyISP Programmer. If you’re on a Mac, the green LED light on your programmer will now likely be illuminated. Cool!
Note: If your test instead sends back:
avrdude: Error: Could not find USBtiny device
You should double check your USB connection and try changing USB ports and running the test again. You might also double check your soldering work and make sure you put all of the components in the right places. If you’re still having trouble after that, spend some time on the Adafruit forum and, if necessary, with your favorite search engine.
4. Once you’ve confirmed that your computer can connect to and recognize the USBTinyISP Programmer, the next step is to wire up the microcontroller on your breadboard. I’ve created a diagram to help illustrate how to wire up the pins correctly.
Note: This illustration is created from a bird’s eye view and assumes that the “holes” on the USBTinyISB 6-pin connector and the “legs” of the microcontroller are all facing down towards the table.
When you’re finished wiring everything up, it should look like this (right side of breadboard).
Note: I’ve built several LEDfireflies and have hacked together a small cable connector jig to simplify the process (see left side of breadboard on photo above). This allows me to quickly plug in the 6-pin USBTinyISP cable to my breadboard without fumbling with the jumper wires each time.
5. Now you’re ready to program the chip. First let’s run a test to make sure your computer can communicate with the microcontroller. Open up your terminal and enter:
avrdude -p attiny13 -c usbtiny -n
It should return the following message:
avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e9007 avrdude: safemode: Fuses OK avrdude done. Thank you.
If you instead get an error message, check your connections (the breadboard wiring can be confusing and you may have done it backwards). When you get stuck, refer to the Adafruit forum and the Internet for help.
Next, you’ll need to download a copy of Karl Lunt’s code (mirror) to flash the microcontroller. Note that the zip file includes the C source file and a .hex file. Unzip the folder and place it somewhere convenient like your desktop. Then inside the terminal, change to the correct directory:
Now upload the .hex file to the microcontroller:
avrdude -p attiny13 -c usbtiny -U flash:w:fireflyLED.hex:i
It should send back a success message. Cool!
Assemble the LEDfirefly
Now that you’ve successfully programmed the ATTiny microcontroller, you can begin to build your LEDfirefly. Here’s a bird’s eye view wiring diagram of an assembled LEDfirefly.
1. Take your LED and gently pry the two leads (legs) apart as if you were going to create the letter A. Now wrap one of the leads from the 1 M ohm, 1/8-watt resistor around one of the leads of the LED (about 1cm down from the glass base of the LED). Then wrap the second resistor lead around the second LED lead.
Note: It doesn’t matter which lead connects to which – these resistors work in either direction.
Also note: The shorter lead of the LED is negative. I recommend marking the negative lead with a sharpie (near where it meets the glass base) so that you can identify it later.
2. Now solder the resistor to the LED.
3. Once you’ve finished, you can cut the LED leads short (be sure to leave the leads from the resistor uncut). Now take the negative lead and attach it to the ground pin on the ATTiny13A. Then take the positive lead and solder it to the pin beside the ground.
4. Now that the LED and resistor are soldered to the ATTiny13A, the last step is to connect them to the battery holder. Flip the battery holder upside down and position the ATTiny13A on top so that it’s ground pin is closest to the negative terminal on the battery. Solder a short jumper wire from the ground pin of the ATTiny13A to the negative terminal on the battery holder.
5. Finally, solder a short jumper wire from the VCC pin of the ATTiny13A to the positive terminal of the battery holder.
6. Insert the CR2032 battery into the battery holder.
Note: The “+” and text on the battery should face outwards so that you can see them.
7. The LED should now start flashing. If you place the LED directly under a strong light (or in direct sunlight) it will stop flashing.
Once you’ve successfully built your LEDfirefly, the next step is figuring out what to do with it. Assuming you plan to install it outdoors, you’ll want to weatherproof the electronics in some way. While the possibilities are endless, a simple solution is to place the LEDfirefly in a zip lock bag. However, I’ve found that a more elegant approach is to place my LEDfireflies in small translucent takeaway sauce containers.
I also like to place a 1/2″ rare earth magnet in the bottom of the container as it allows me to easily throw and attach the LEDfirefly to any ferromagnetic surface.