Jars of Fireflies using ATTiny85 and Arduino

Posted on October 9, 2011 in Art, Portfolio

Like many people, I stumbled across a fantastic article over at Instructables outlining how to create some beautiful jars of electronic fireflies. It was an instant bookmark for me, and filed away as a ‘rainy day’ kind of project. A few months after I saw the post, I was given a few really nicely sized mason jars and the idea to execute this project was stirred. However, upon further inspection, I realized that the original jar of fireflies sketch was not very easy to replicate at all, and I knew I had to make my own adaptation.

Trying to replicate the original project

Let me first say that I do not mean to say that I think the original author did a bad job or that his project was ‘bad’. Instead, I have to take the blame for my struggles with this project as inexperience on my on behalf with this specific type of technology. Specifically, the original author chose to use a mix of low-level C, assembly and manufacturer-supplied approaches that make it very difficult for a beginner or ‘outsider’ to the world of AVR programming to follow. Nonetheless, I spent about two months earlier this year trying to tease apart the layers of code and trying to understand how it all works and fits together. Needless to say, I failed.

As anyone who has dabbled in both the Arduino framework and AVR Studio 5 can tell you, there is a world of difference in complexity both in environment and code readability. Proponents of ‘old school’ methods of programming microcontrollers seem to cringe at any mention of the Arduino framework, as if it takes the ‘real man’ work out of programming microcontrollers.

What kept me fussing with the original project code for so long was the fact that the author actually took the extra step to find some real data about firefly luminosity and pattern behavior and translated it into code to use on a microcontroller to control LEDs. In other words, he found a way to light up LEDs exactly the way a real firefly had in the past! Now THAT’S realism!

Chasing this implementation sucked. It was easy to port his LED patterns over to Arduino, but I just couldn’t quite figure out how to easily store/retrieve these large arrays of numbers in memory. This was compounded by the fact that microcontrollers actually have at least three different types of memory (SRAM, RAM, etc). Storing, manipulating and conceptualizing things stored in these different types of memory is not easy with the current version of Arduino, but improvements are included in the changelog for the upcoming 1.0 release!

Making an original implementation

Fast forward a few months of toiling with the original project and I’m tired of hitting dead ends. I put the project on hold while I do some other projects, until work revs up for my Artist in Residence show at UNK. I realize I should really take another stab at the project, and now its not just for my own personal enjoyment; its for a show, with deadlines and responsibilities to be accountable for. With this new motivation I realize I needed to make some compromises and admit that I just wasn’t capable of replicating that exact project as I saw it on Instructables. But I still wanted LEDs to blink inside of my mason jars!

I realized that to me, a firefly exhibits a very specific behavior that is easy to replicate in code; simply turning it’s light on and off. In my head, this wasn’t just an instant-on, instant-off phenomenon, it was a quick fade-in and a slow fade-out. The other thing that makes fireflies interesting to watch is that they repeat this behavior at somewhat unpredictable intervals, but at somewhat predictable rates. In other words, if you see a firefly light up in your yard, you don’t say to yourself, “It’s going to light up again in another 1.2 seconds.” Instead, you stand there waiting for another 3-5 seconds, because you know the damn thing just has to light up again soon.

All of these little observations amounted to one main idea: all that would need to be done to simulate fireflies would be to randomize everything I could get my hands on in the code about the fade-in / fade-out behavior. Randomizing the exact amount of time it takes to fade in, fade out, wait between fades and ‘flicker’ should give it the personality I was looking for.

Schematic

The schematic for my implementation is pretty simple, and only uses 4 LEDs (though you might be able to find a way to add another 2 or more in there somehow if you’re ambitious!) Since the ATTiny85 only has two pins that are capable of PWM, I chose to charlieplex the LEDs to get more bang for the buck (again, very similar to the original project here).

Parts list

1 – ATTiny85 (microcontroller)
1 – 8-pin DIP socket
2 – 100ohm resistors (or whatever your LEDs need)
4 – amber-yellow LEDs (or whatever color you want)

Programming the ATTiny85

Here’s where things really start getting fun. Thanks to the awesome work of the High-Low Tech Group at the MIT Media Lab, programming ATTiny chips with a stripped down Arduino bootloader is actually extremely easy! In fact, they have a fantastic tutorial right here about how to do it, and it explains the process better than I could have: http://hlt.media.mit.edu/?p=1229

Be sure to check out the rest of the High-Low Tech Group site, they have some amazing tutorials there, such as how to build speakers using paper and copper tape, embedding Arduino into washable clothing and etching things with salt and vinegar!

UPDATE 10/11/2011: MAKE Magazine just released a great video tutorial about how to program ATTiny85 chips using HLT’s instructions. Same information, but in a very easy-to-follow format:

Wiring it all up

Once you’ve got the ATTiny85 programmed and have an idea of how the circuit works, its time to solder it all up. The original Jar of Fireflies project used surface-mount parts, which increased the complexity and required skill quite a bit. I did create one jar using SMD LEDs with a DIP8 ATTiny85, and that was enough for me! If you want to get good at soldering, use surface-mount parts! But I wanted to make a total of five of these jars, and after realizing the amount of labor that goes into one SMD jar, I decided not to make any more. Instead, I had my friend Adrian Sanabria-Diaz wire up some regular 5mm LEDs and implement the rest on perfboard. He used twisted pairs taken from CAT5 cabling to make the job easier.


In action

Thanks to Adrian Sanabria-Diaz, a small army of firefly jars were put together and installed in my Artist in Residence show. We both got a kick out of the daisy-chain wiring job that uses alligator clips to connect each jar to a single power supply (a hacked USB charger). Reminds us quite a bit of Baghdad Batteries!

Future work and improvements

The first thing that happens when I put one of these in someone’s hands, their first instinct is to tap on the jar to try to get the fireflies to light up. And, perhaps too realistically, nothing special happens. Therefore, the next iteration of this project will surely need a piezo transducer attached to the lid! The ATTiny85 does have an open ADC pin in my schematic…

Furthermore, the power supply of this project was left a bit ambiguous, so in the next version I plan to build in support for some sort of battery and external power support.

Finally, I would love to make kits out of these things, with nice circular PCBs, mason jars and everything for people. But I’ve never made a kit before, and I’m quite busy these days. Please leave a comment if you like the idea of a kit, and if enough people are interested, I will give it a shot!