New Jar of Fireflies


Introduction: New Jar of Fireflies

When I ran across the Jar of Fireflies Instructable sometime this past summer, I knew I had to build it.  Full credit to Keso for an excellent set of instructions!  I wouldn't have gotten to the point of developing the firefly behavior without such a solid base for building the hardware, as well as firmware to start it out with.  If you have already built a firefly jar according to those plans, it should be straightforward to reprogram it with my firmware to change its behavior.

I wanted to go a step farther when I built the firefly jar as a Christmas present.  My goal was to have multiple fireflies interacting, including males that sometimes synchronize with each other, and females that respond to the flashes of males. I also wanted to base it on the behavior of our backyard firefly, Photinus pyralis , the common Eastern firefly, which is also one of the best studied.  I wrote my own software for the microcontroller to simulate this behavior.  The full project is documented here , but this Instructable should be enough to follow.

The latest version of the firmware is available on GitHub .

You will need two things to use this software:
Both of these are outside the scope of this Instructable.  For the first, please refer to the detailed instructions at the original Jar of Fireflies .  If you need help with programming the AVR, a good place to start is the AVR tutorial from Adafruit .

I tried making a video to demonstrate the flashing behavior.  The quality isn't great (it's from an iPhone, and I reduced the quality to upload it):

Step 1: Hardware

The only changes I made to the original hardware were the addition of a push on/push off switch (scavenged from a stick-on LED light) and a 6-pin programming header to allow reprogramming of the jar after it had been built. To make testing and debugging easier, I also built a prototype board on a 2"x2" perfboard with the same schematic, so it is electrically identical but easier to see what it's doing, and less fragile.  I've added a view of the point-to-point soldering on the back of the prototype board in case that is helpful to anyone.  The prototype board can also be used as a target board for programming ATtiny85 (and -45) chips for other purposes.  In the real jar, the switch and header are fixed to the underside of the lid with J-B Qwik to keep them well attached.

Step 2: Software

After I built the firefly jar, I had planned to modify the software to change its behavior somewhat.  I was completely new to AVR programming, and I realized that I didn't understand much of what was going on in the original code, so I decided to write my own from the ground up.  It uses the AVR's internal timers for controlling the timing and brightness of the LEDs.

The simulated fireflies exhibit the following behavior, based on observations of the species Photinus pyralis , the common Eastern firefly (more details and references ):
  • Male fireflies will appear one at a time, flashing at a regular interval, 5.9 ± 0.6 seconds, and gradually synchronize with each other.
  • If all males are in sync, a female may respond to a male's flash after a delay of 2.1 ± 0.2 seconds.  The appearance of another male out of sync will cause the female to stop responding to the first one.  The female's flash is 1/10 the intensity of the males'.
  • Each pulse is 0.5 second in duration with a steep increase and slower falloff, which I modeled as y=x*e-x2
  • Females are stationary, they will flash at the same LED each time, while males can "move" around, flashing at randomly selected LEDs.
  • One reaching the end state of 6 males flashing and one female responding, it will continue that pattern until it is reset at a random interval, about 5-20 minutes.
The code was compiled with avr-gcc 4.2.0.  I used an ATtiny85, and it also works with an ATtiny45 on my prototype board.  There are also two shorter example programs included that can be used as a base for different behaviors.  All source code is freely available under the GPL .

Download the source code and compiled .hex files here .



    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest

    23 Discussions

    I've had so much fun making these (even did some for a wedding), so thanks again for this! Seriously, thank you.

    Have you ever considered adding a Piezo or something and have the fireflies get angry if you tap the jar? Like they go into a special sequence where they all randomly flash and have fast, agitated pulses. That would just be fun. If anybody tries this, please report back with your results.

    I wish I understood what all this means I would love to make this

    Never mind the part about the boot sequence not working. My chip had just failed to overwrite and still had it disabled.

    The boot sequence has never worked for me. It's just ignored.

    Also, I think it would be neat to have the boot sequence actually run the entire cycle once with the settings adjusted faster. Like a quick preview. Then go into the real thing. Anybody know how to do this? I mean, I know how to make it go faster, but not how to make it do that once and then start and loop the real thing only. I'm using ATtiny85, so there should be enough space to repeat the entire code, right?

    I want the fireflies to stay on a little bit longer, but when I change PULSELEN from 20 to 40, I get LEDs which get stuck steady on for over a minute every now and then. I think this is happening when a new cycle starts. Can anybody help?

    5 replies

    It is rather hard wired in the C code, I'm afraid. You're overrunning the end of the pulse arrays. You'll also have to change the length of the arrays mpulse and fpulse to be length 40 to match (and pad them out to stay on longer in the meantime). You always want the last value of each array to be 0 to keep the LED off between pulses. Otherwise you can make the pulse look like anything you want.

    OMG, I actually understood that! And it works! THANK YOU! :)

    Now they are all off between cycles. However, there's a full minute where nothing happens before they start up again. Is there an easy change to make them begin again sooner?

    A full minute between flashes? Or after a full cycle of building up to all 6 LEDs flashing? Either way, it shouldn't be that long. There are some other timing variables set up in the init() function you could play with.

    Yes, it's after all six flash together and before the single flashing starts again. This was with no changes to the code. I thought it had gone into sleep mode early, but then it started again. I timed it the next time and it was just over a minute where absolutely nothing lit.

    Unrelated, is there a proper way to disable sleep mode? I can set it to a negative number and tell it the count has to be less than that, and that works, but is there a better way?

    Also, is there only ever one female? Can I make it randomly have more?

    And is there a way to make it randomly select between several different mpulse and fpulse choices?

    Anyway, thanks so much for your help. I actually have it working in a way that I really like. I'm just curious about how much I can tweak it.

    On line 170, it sets nmales=0 after each full cycle, so it will appear all off until the first male randomly turns up. If you make that 1, it will always have at least 1 going.

    Your method works fine to disable sleep, you could also just take out the "if(bigcount>=SLEEPAFTER)" statement entirely in the main loop to eliminate it.

    Adding more females would take some larger changes in the logic. It's certainly possible, but that's up to you.

    Choosing different pulses is certainly possible as well, you'd have to decide when it's going to choose which pulse. At the beginning (or each time through the full cycle) would be fairly straightforward.

    Glad you enjoy it!

    On your prototype board do you have a socket that the ATtiny85 chip is sitting in? If it is i cant seem to find one anywhere. Do you happen to have a part number if that is what you are using. :)

    1 reply

    Sorry it took me so long to reply. I think I used Digi-Key part no. 3M5461-ND.

    I can't seem to get the firefly software unzipped. could anyone send me a makefile or an unzipped set. Thanks ~Corry

    1 reply

    Sorry you had trouble with the zipped file. I have put the firmware up at github, where it should be easier to browse and get in a format you can use.

    I just got all the parts today, I have a question on your picture with all the LEDs on the board in step two you have five resistors on the board but in the schematic you show 3? maybe the other resistors are for the batterys?

    1 reply

    The schematic is correct.  On the prototype board, there are two pairs of 20 Ω resistors (each pair connected in series), to make 40 Ω each, which matched better with the LEDs I used on that on that board than the 100 Ω used on the surface mount board.

    You may want to adjust the values of those resistors depending on the exact LEDs you use.

    The code (firefly.hex) from the original Keso Firefly jar works with this schematic and an ATTiny45, but I get no response using pyralis.hex.    This is my first effort building, using, coding AVR, etc, so I may be missing something crucial.  I'll experiment and let you know if I find a solution.

    1 reply

    It seems to be the case that the current version of pyralis.hex I have posted does not work on an ATtiny45.  I'm not sure why that is right now, previous versions worked fine for me.  I will look into it when I get a chance.

    Sorry about that.  You might try compiling it yourself, changing the Makefile appropriately (set DEVICE = attiny45).

    I've changed the text above to reflect that for now.