## Introduction: Synchronizing Fireflies

Have you ever asked yourself how do hundreds and thousands of fireflies are able to synchronize themselves? How does it work, that they are able to blink all together without having a kind of boss firefly?

This instructable gives a solution and shows how this synchronization can be achieved.

I have always been fascinated by self organization of insects. A couple of years ago I wrote a Java-Applet that simulates a square of thousands of fireflies. It worked well and was fun to watch. This time I have done it in hardware.

Credits for inspirations goes to Keso and his Jar of fireflies https://www.instructables.com/id/E7U5HYMSVIEWP86SAL/.

Any comments or corrections are welcome.

Update 2008-09-12: There is a new version of the fireflies online at Synchronizing Firefly Howto. It has a custom PCB for every firefly. And you can buy a kit at the Tinker Store.

Here is the video:

## Step 1: How It Works

The algorithm

What can be observed is that the fireflies start with random blinking. But as time goes by, they are able to slowly synchronize with their nearest neighbors. And these neighbors are synchronizing themselfes with their neighbors and so on an so on. Until the whole tree or the whole valley blinks in the same cycle.

And what is it good for? It is used to attract other specimen. With all the blinking in sync it is much easier to find a partner.

One of the easiest algorithm to explain this behaviour goes like this: You have a value that holds the power to flash. As time passes this power will slightly raise. If the power reaches a certain level, the firefly flashes and the power is consumed. The rate at which the power raises is nearly the same for all fireflies. So they have the same frequency but not the same point in time to flash.

While slowly charging with power the firefly is able to detect a flash of another firefly nearby. It adds then a higher value to its power value. Some kind of power boost, if you wish. That means the next flash will occur earlier than the one before. And next one even earlier, until these two are flashing exactly at the same point in time and with the same speed.

You can find more on this algorithm e.g. here:

The Hardware

I decided to use my previous instructable (Programmable LED) as starting point. It consists of a microcontroller, an LED and an Light Dependent Resistor (LDR). That should be enough to simulate a simple firefly. It is able to flash, to see and to count.

I just had to modify the program and the orientation of the LED and the LDR. LED and LDR must been placed in a way that one firefly circuit is able to interfere with another. So one LDR must be able to "see" the LED of another firefly. And it should not only see one neighbour but more. That can be done by letting the LED and the LDR pointing up from the ground and use some white paper to reflect the flashes.

## Step 2: Materials and Tools

For a grid of 5 by 5 fireflies, you need:

• 25 x 1K Ohm resistor
• 25 x 100 Ohm resistor
• 25 x LDR (Light Dependent Resistor), e.g. M9960
• 25 x LED, 1.7V, 20mA (reg, green, blue, what ever you like)
• 25 x ATtiny13, 1KB flash RAM, 64 Bytes RAM, 64 Bytes EEPROM
• 25 x sockets
• prototyping board
• wire

The cost for one firefly should be about 1.50 Euro if you get some rabate when ordering in larger numbers.

The sockets are only neccessary, if your programs are buggy. If you feel confident with your development skills, you can spare them. ;-)

You can surely replace the ATtiny chip with any other microcontroller as PIC, PICAXE or BasicStamp to name a few. Just take the smallest and cheapest you can get. I go with Atmel as I had the programmer already and my first project with a ATtiny13 worked just fine.

Tools

• Soldering iron
• Solder wire
• AVR programmer
• 5V Power supply or
• 4 AA rechargeables

Software

• WinAVR

## Step 3: The Circuit

The circuit is not very complicated as you see. The LED is connected to an I/O port, the LDR to an ADC port (to enable seeing light differences, not only black and white).

For more details on the circuit see here: Programmable LED.

With my last instructable I got some comments about using the LED as light sensor. Yes, using the LED as a sensor. I gave it a try and it basically worked, I could detect light and shadow.
But I was not able to do use it in a more analog way as I was using the LDR before. At least I was not able.

## Step 4: On the Breadboard

I started with two up to five fireflies in a row before I moved to the prototype board.

## Step 5: On the Prototype Board

As everything works on the breadboard you can move on to use it on a prototype board.

If you are not familiar with soldering, I recommend this instructable.

Reprogramming this thing is not very funny but possible. All controllers are socketed. You have to bend up the LDR a bit and with a bit patience you are able to pull the controller out.

There is a lot of space for improvements. As most of the time.

• Use a printed circuit board (PCB) to save some solder wire and make the soldering and packaging much easier.
• Use SMT components to reduce the size of the circuit.
• Make a board with 10 by 10 fireflies. Or 20 by 20?
• Place the LED near the LDR

## Step 6: Software

I will not go into details on how to actually program a microcontroller, there are better pages to learn how to do that. See https://www.instructables.com/id/E5H5UDWB5UEUKIKV8V/

The complete program fits into 552 bytes, that makes only 276 instructions on Assembler level. That is pretty nothing, as there is still some startup and configuration code within it. And it consumes only 54.5% of the flash memory so you can still add more features to that.

I bet the core algorithm can be implemented in less than 100 bytes.
Someone?

The steps the program executes are:

`1. blink 5 times (say hello)2. compute the average brightness and store it3. power = power + 14. if brightness >= daylight (is it daylight?)    * sleep some seconds5. if brightness > average brightness (that was a flash)    * power = power + 1006. if power > MAX    * flash    * power = 0 7. goto 3`

Actually this is a bit simplified but you get the idea.

Upload the program and start the fireflies. If everything is ok, they should be able to synchronize in about a minute or two.

I had some problems in getting the fireflies synchronized. As they are running on their internal oscillator, they all have different speeds. I thought it would help to recallibrate them by changing the delay loop of every chip. My girlfriend asked, what I was doing and I told her. She thought, that that is like cheating. Fireflies are not callibrated either. What should I say? She was right. So I decided not to do it and searched for another solution. Keep in mind to go for the easiest solution that could possibly work.

## Step 7: Conclusion

It is really fun to watch the fireflies, maybe more than building and programming them. You see all sort of patterns that emerge and disappear after some cycles. It can not be predicted and acts chaotic but at some point all fireflies catch up and flash at the same moment. It's like: Yes, they did it. Again.

Hope you enjoyed it.

Kind regards,
Alex

Participated in the
The Instructables Book Contest