A whole mangrove forest, lighting up all at once, plunging into darkness, then lighting up all again – in near-perfect synchrony. How do thousands of fireflies coordinate with each other? Who is the conductor of this silent symphony?

The fireflies phenomenom is astonishing. The web page http://ncase.me/fireflies/ provides an amazing explanation of this history and mechanics around it.

Let's create virtual fireflies using multiple Circuit Playground! The NeoPixel ring provides the flash, the light sensor can detect it and we can tie it all up with a bit of code in the Microsoft MakeCode code editor. Let's get started!

A Black woman's manicured hand holds a round microcontroller with lit up LEDs.
Circuit Playground Express is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and...
In Stock

As mentionned in the article, each fireflies has a clock that drives when they decide to flash.

each firefly has its own individual internal clock

A clock in this case is like a counter, so we will start by adding a clock variable to our program.

and every time the clock “strikes twelve”, it flashes.

We can use a forever loop to repeat code that increments the clock.
When the clock reaches "noon" (let's pick 8), we turn on the NeoPixels briefly and turn them off to simulate a flash.

Another key observation from the article is the clock adjustment process:

when you see a nearby firefly flash, nudge your clock a little bit forward.

We can use light sensor on the Circuit Playground Express to detect a nearby flash.

We can use the on light bright event to add code that will run when a bright light is detected. When this happens, we bump the clock and play a sound... unless we are also flashing (and clock is greater or equal to 8)!

Based on your light condition, the threshold predefined for on light bright might not be good enough. In such case, we can use set light threshold to tune this value. You can make it a trial and error approach (using a binary search) or use serial to log values and do a deeper analysis (We'll dig deeper into logging and analysing values in another guide). In this case, we've just approximated the best value for the particular setting.

We also beef up the brightness of the LEDs to the maximum (255) to get the brightess flash possible.

It's time to test it:

  • Gather your Circuit Playground Express boards (you'll want at least 3),
  • Flash the fireflies code on each of them
  • Find a dark place
  • Turn them on and wait.

At first, you will hear sounds triggering at various moments but they will start to group up. When all board will sync, you should not hear any sound anymore...

This guide was first published on Oct 11, 2017. It was last updated on Oct 11, 2017.