Introduction: Synchronizing Fireflies

Picture of 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

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

Picture of 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:
Firefly Synchronization Ad Hoc Networks

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

Picture of 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.


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


  • WinAVR

Step 3: The Circuit

Picture of 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

Picture of 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

Picture of 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

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.

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

Picture of 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,


Nice! Definitely gonna make it!

HasanF1 (author)2015-11-27

this is my led luster . this luster have color changing and infinitive color

GeorgeR21 (author)2015-09-15

Would it be possible to use multiple LDR sensors, one for like each side, for example? I'll try it and see what happens, but if someone already know, i would love to hear :)

You did a great job there.

john mosier (author)2014-05-15

you should post a video

CrazeUK (author)2014-02-02

Hey, How interesting and thanks for the instructable.

I want to try and make this for my nephew and niece who are autistic.

However i would like to make a slight change. I want to have a number of these in close proximaty in a room. But be able to hit a button to disrupt any one of the individuals. which would throw all of the synces flies out of sync. So they can re-synchronise.

Is this possible?

How difficult would it be?

Alternatively is it possible to do it on using one Arduino to control all of them on a single board with the same effects as outlined above?


alex_weber (author)CrazeUK2014-02-03

The easiest way to disrupt the fireflies would be a light source, that you switch on. Another option is to simply cut the power for all fireflies.

Using a single Arduino is not really an option as the somehow randomized patterns can only emerge if every firefly is autonomous.

khanki (author)2014-01-15

I have 2 questions to ask:-

1. In C file, what is the use of <avr/eeprom.h> header file?

2. after downloading the hex file, it is found to be larger than 1KB flash

Plz reply it soon.

Arya42 (author)2013-10-31

I'm thinking about making this project but I'm not sure about the numbers on ATtiny13. Does it need to have the numbers 0624 and 20pu? I found different ones on ebay, like 0721 10pu. Would these work the same?

alex_weber (author)Arya422013-11-01

The 10pu ones are ATtiny13V. They are able to work with lower voltages. Otherwise they should work fine.

Arya42 (author)alex_weber2013-11-01

Thank you, that helps a lot. Also, where did you get the square-shaped photo resistors? I can only find the round ones.

DragonDon (author)2013-10-09

This is pretty cool. What happened to the store?

Ghost Wolf (author)2009-09-05

Do you need to program every single ic?

rtalada (author)Ghost Wolf2013-09-05

You could program every IC to receive a new program via light, then one reprogrammed firefly could program all of his buddies :D Cooperative firefly updating!

alex_weber (author)Ghost Wolf2009-09-06

Yes, you have to. Gets a bit tedious, but there is no other way.

JD_Mortal (author)alex_weber2011-08-22

The "other way" would be to use an ATMEGA 328 P (Arduino Duemilanove) with a LED-ARRAY setup. This involves a slightly more complex program, but it saves you some soldering and hardware costs.

You use the 13 digital outputs to create a grid for the LEDs 6x7 = 42 max leds. You use the 6 analog inputs with 7 stacked resistors (making a grid array of inputs) with the light sensors. Or you can use less leds, and have the extra digital pins as part of the analog input array.

Since the LEDs don't need full power, or constant power to look ON... You turn on each led in the grid, of each row, while you cycle through the grid. (Look up the 3D LED array, and you will see what I am talking about.) Additionally, adding a capacitor to the LED will help it appear ON and brighter between grid cycles.

Plus you get the added bonus of being able to scroll letters and do a-life programs!

Love this setup BTW. Would be neat with them all freely inside a jar, frozen in epoxy or mounted on a screen in the jar.

Ghost Wolf (author)alex_weber2009-09-06

Once you turn it off (0Vs) will the ic lose it's memory?

alex_weber (author)Ghost Wolf2009-09-06

Yes and no. Yes, it will loose its main memory. And no, it will not loose its program. The program is stored in flash ram, like on your USB memory stick.

Ghost Wolf (author)alex_weber2009-09-07

What is the main memory used for?

alex_weber (author)Ghost Wolf2009-09-07

The main memory is used for variables while executing the program. It is used to store, which part of the program is executing right now. If the controller looses power, the main memory is lost. If you turn power on again, it will start the program from the beginning, because it has lost its state. It is like booting up your PC. Data on your harddisk is still there but data in memory is gone. Does that clear things up?

Ghost Wolf (author)alex_weber2009-09-07

Yes thank you very much

Ghost Wolf (author)Ghost Wolf2009-09-08

Can you use different LEDs?

alex_weber (author)Ghost Wolf2009-09-08

Sure. The sensor detects only brightness, not color.

Ghost Wolf (author)alex_weber2009-09-09

Thanks i just wanted to know, a another question I have Is there any photos of the back side?

alex_weber (author)Ghost Wolf2009-09-10

No, I'm sorry.

Ghost Wolf (author)alex_weber2009-09-12

I was just wondering because I want to how you supply power to the unit

Hamid_f (author)2011-11-30

Hi Alex,
I remembered an old german tv documentary (maybe Spektrüm) about non-linear and chaotic systems, fractals and some structures called "cooperative aggregation" or "cooperative memory" (i don't know exact term), if you remove a little pixels of a picture after iterations lost information can be repaired (rebuilt). brightness of each pixel is sum of neighbors brightness with particular weight. a focused camera on a tv was setup of this idea. do you know about this?

alex_weber (author)Hamid_f2011-11-30

No, unfortunately I don't.
If you find it somewhere, please let me know. Sounds interesting.

abend (author)alex_weber2012-05-17

That sounds a lot like Hopfield networks, which are a type of artificial neural network.

knexsuperbuilderfreak (author)2011-09-11

Why use a socket when you can soder the chip right to the board and you could bend the leds down maybe or they might just light a ton of them up.

The socket is for reprogramming them. The is not enough room for an ISP header.

eulaliaaaa! (author)2010-08-05

(Piano music) You would not believe your eyes, if ten million fireflies, Ate out your eyes and then farewell. They fill the open air, and scatter body parts everywhere, I don't mean to be rude but an arm is over there.

♫ I'd like to make myself go pee....But planet earth is gory. It's hard to stay coherent when I'm all but torn apart. And it appears my dog consumed my heart.....♫

trailleadr (author)eulaliaaaa!2011-08-21

lmao, now that's funny!!

an1153 (author)2011-08-21


broper (author)2011-08-21


mkpeker (author)2011-04-10

Thanks for your good project but unfortunately this circuit doesnt matches with the code on step7.

True circuit:

pin1: 10k to vcc
between pin2-3: LDR(best 10k LDR)
between 3-4: 1k resistor
pin 5 and 6 not connected
pin7: green led by 220 ohm to GND

Thank you anyway

elektroking (author)2010-05-20

i have a tiny13 and want to put in te program of the fire flies but with mikroc PRO for AVR it doesn't work. Witch programmer have jou used for the program?


AndyGadget (author)2010-05-03

Any chance of restoring the video?
I'd really like to see this.

GOwin (author)2009-12-27

 very nice!

Is there a way to "talk" to each firefly (ie ask one to turn itself on or off)?

Xenel (author)2009-03-07

Synchronizing Christmas tree lights anyone?

lasermaster3531 (author)Xenel2009-07-18


twist2b (author)2009-07-01

What if I want to have a couple all over my room, they go off all day, but by night they can synchronize when there is no light. Here is the question though, how large of a distance can you get? And how can you make something like this to work with larger distances?

o_k_boy (author)2009-06-03

hi alex, great project - i'm a newcomer to avr programming and have been reading your blog and instructables carefully. they have been very useful, so thank you very much. i know this version of firefiles has been superseded now, but in the schematic you have the ldr positioned between pins 1 and 2 of the avr - is this correct or should it be between 2 and 3?

alex_weber (author)o_k_boy2009-06-03

Hi o_k_boy, yes, you are right, good catch. The ldr is between 2 AND 3. You can check also the source code for the pin definitions. Cheers, Alex

Callum Snowden (author)2009-01-25

Could you do this with 1 arduino board and 5 photoresistor / led combinations??

Hi uberdum05, yes, you could. But you would loose some of the non deterministic behaviour that arise from using independent devices with slightly different timings. Every firefly has its own internal oscillator and they are all a bit different. If you do it with a single Arduino, I think it will be much less chaotic. Cheers, Alex

Carlos Marmo (author)2008-10-30

Wonderful work! Much Style! Congratulations!

Goodhart (author)2008-09-20

Oh cool, a hardware version of Emergence theory......I will have to have a go at this in the future. :-)

kiowamike (author)2008-09-15

Great project! I am putting together the parts for this thing but, I am having trouble finding the LDR's. Since I don't speek Dutch or German, I was wondering if you have a part number. Thanks.

alex_weber (author)kiowamike2008-09-16

Hi kiowamike,
try this at digikey:
Maybe you have to adjust the resistor connected to the LDR.

About This Instructable




More by alex_weber:64pixelsArduino XMAS hitcounterSynchronizing Fireflies
Add instructable to: