Arduino Fireflies

12,753

129

32

About: I live in suburban Pennsylvania with my wife and puppy. I pass the time building robots, photographing microbes and directing live TV. I enjoy learning any new skill that helps me Make! I enjoy even more p...

One of the things I look forward to with summers in Pennsylvania are fireflies in my backyard. I recently taught myself Adruino programming for the purpose of making this simple project. It's a great program to start with and is easy enough for any programmer, novice to expert, to build, modify and have fun with in only a few minutes. Let's get started.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What You'll Need.

To get your bugs blinking, you'll need these components:

  • Arduino. I started with the Nano, however any Arduino compatible micro-controller will do.
  • Yellow LEDs, 5mm. You can use up to 6 of them.
  • Resistors. You will need one resistor per LED to limit current. I used 470-ohm but anything above 150 ohms should be fine to protect your micro-controller.
  • Breadboard.
  • Jumper wire.

To complete the project for your backyard, you'll need:

  • Weather proof project box.
  • 9-volt battery with a connector. (Please see notes at the bottom of this section.)
  • Switch. (I chose these waterproof switches. If you are not using this outside, any switch will do.)
  • A few yards of wire to place the LEDs around the garden. I used about 10 feet of Cat5 Ethernet wire per LED.
  • A small breadboard or some perf board.
  • A weather proof cable gland through which the LED wires run. (You can omit this if you are not using this outside as well.)
  • Heat shrink tubing to protect your LED bug butts.
  • Green hook-and-loop (i.e. velcro) strips to affix the LED fireflies to plants and posts in your garden.
  • Male headers for plugging components into your small breadboard.

Tools:

  • Drill bits for the project box. (Use this opportunity to get yourself a nice step-bit. You'll be glad you did).
  • Hot glue gun.
  • Soldering iron.
  • Rotary tool (i.e. Dremel) for carving out space in the project box if you need it.

A Few Notes Here:

1. The battery choice was for a quick and easy start-up. Using a 9-volt battery permanently is a bit wasteful. You're better off using a 4x AA-battery holder for longer life (however you will need a bigger project box in which to fit it).

2. If you choose to deconstruct a Cat 5 Ethernet cable for the wires, make sure they are copper core and wrap them neatly around some PVC to keep them organized while you work. Again, I used about 10 feet of wire per LED. If you want to spread the lights out far and wide, by all means use longer wires!

3. Lastly, all the links I provided are mere suggestions. Please read through this entire Instructable before building or buying anything as you will gain a better understanding of how you would like to personally proceed.

Step 2: Build the Circuit.

This project uses the pulse width modulation pins on your Arduino. The micro-controller has 6 of these pins and you are welcome to use as many as you want. The circuit is pretty straight forward. Wire all power from the pulse width modulation (PWM) pins D3, D5, D6, D9, D10, and D11 to the positive ends of your LEDs. Wire the negative ends to the resistors and then to a common ground. (The resistors can go in front of or behind the LED. It makes no difference unless you want to safeguard against short circuits in higher currents.) I included a few schematics to help with wiring. (The diagrams where created using Fritzing design software.)

Step 3: The Code.

If you are a seasoned programmer, you'll find this code simplistic. It is a great code to start learning with as it introduces you to the use of variables, pinmodes, functions and even a random generator. The code is not as compact as it can be as I am sure the same effect can be achieved with arrays etc.

The code comments lay out the logic of each section. The entire code is embedded here and you may download the sketch below.

/*
This script flashes 6 LEDs (yellow, of course) in random order 
at random intervals using PWM.  
Each LED is controlled by it's own function.
*/

int led1 = 3;    // LED connected to PWM pin 3, etc. I used all 6 PWM pins.
int led2 = 5;
int led3 = 6;
int led4 = 9;
int led5 = 10;
int led6 = 11;

long randnum;      // randnum controls the time interval between flashes and 
long randbug;      //randbug controls which bug lights up.

void setup()  { 
  pinMode(led1,OUTPUT);      //Setting all PWM pins as outputs.
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT);
}

void loop(){  
  randbug = random(3,12);                 //randbug randomly chooses a function to execute, 
                                          //thus randomly chooses a bug to light up.
  if (randbug == 3) {
  bug1();
  }
  if (randbug == 5) {
  bug2();
  }
  if (randbug == 6) {                                     
  bug3();
  }
  if (randbug == 9) {
  bug4();
  }
  if (randbug == 10) {
  bug5();
  }
  if (randbug == 11) {                                     
  bug6();
  }
}                                                   

/*
 * Each of these functions work the same way.  'for loops' increase then decrease 
 * the output of that pin to control LED brightness.
 * 'randnum' is a random time interval between 10 and 3000 ms 
 * and chooses a time interval between bug flashes.
 * 'delay 10' is just for the fade effect.
 */
 
  void bug1(){
    randnum = random(10, 3000); 
        for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {       
    analogWrite(led1, fadeValue);                                        
    delay(10);                                                           
  } 
        for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {           
    analogWrite(led1, fadeValue);                                       
    delay(10);                            
  } 
    delay (randnum);  
  }
  
  void bug2() {
    randnum = random(10, 3000); 
        for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {               
    analogWrite(led2, fadeValue);                                           
    delay(10);                            
  }
       for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(led2, fadeValue);    
    delay(10);                            
  } 
    delay (randnum); 
  }
  
  void bug3() {
    randnum = random(10, 3000); 
         for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(led3, fadeValue);       
    delay(10);                            
  } 
         for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(led3, fadeValue);        
    delay(10);                            
  } 
    delay (randnum);   
  }
                
  void bug4(){
    randnum = random(10, 3000); 
        for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {       
    analogWrite(led4, fadeValue);                                        
    delay(10);                                                           
  } 
        for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {           
    analogWrite(led4, fadeValue);                                       
    delay(10);                            
  } 
    delay (randnum);  
  }
  
  void bug5() {
    randnum = random(10, 3000); 
        for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {               
    analogWrite(led5, fadeValue);                                           
    delay(10);                            
  }
       for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(led5, fadeValue);    
    delay(10);                            
  } 
    delay (randnum); 
  }
  
  void bug6() {
    randnum = random(10, 3000); 
         for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(led6, fadeValue);       
    delay(10);                            
  } 
         for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    analogWrite(led6, fadeValue);        
    delay(10);                            
  } 
    delay (randnum);                 
  }

Step 4: Build the Box.

Once you've flashed your Arduino with code and gotten your fireflies working the way you like, you might want to put them in the garden; that means a project box and some heat shrink to keep the Arduino and LEDs dry. Let's Make!

Step 5: Building Bug Butts!

  • Trim the LED leads to about 5mm.
  • Strip and tin the ends of the wires you're using, also about 5mm.
  • Slide 1mm heat shrink tubing over each wire end.
  • Solder the LED to the wire. (At this point, you should choose which wire in your pair will be your positive and which will be negative. I chose the solid wire as positive and the white wire as negative. Maintain that strategy through out the project to avoid headaches later!)
  • Slide the heat shrink up all the way over the bare wire and LED leads. Run a quick flame over them to shrink them to the wires.
  • Slide another piece of heat shrink over the LED and wires with the LED lens sticking out the end and melt it in place.
  • Slide a few pieces of heat shrink onto the wire through out it's entire length and melt it on every few feet to keep the wire tidy.

Step 6: Prepare the Project Box.

  • Use a rotary tool with a sanding drum bit to clean out any unneeded plastic in your project box. (Be careful not to cut away any screw mounts you might need to put your box back together.)
  • Decide where you will want your switch to be and LED wires to come out. I suggest the sides but use what ever works with your needs.
  • Use the appropriate size drill bit to make holes for your cable gland and switch.

Note: In the photo above, you'll see I made a "dummy cable." This is a bundle of 6 pairs of the wire I used for the LEDs with heat shrink to bunch them together. I used it to make sure the cable gland would fit nicely with the actual cable bunch and also to test the water resistance of the box once the switch, cable gland and lid were on. (After being submerged for 24 hours in 6-inches of water, it had very little moisture inside. I would be happy to call this box "weather resistant.")

Step 7: Bring the Power!

  • Determine how much battery and switch wire you'll need to reach your Arduino by roughly placing all three components in the project box. Trim the wires of the switch and 9V battery connector. Strip and tin the ends. Slide some heat shrink in place for the next step.
  • Cut away two male header pins from your strip (but keep them stuck together).
  • Solder the red lead of the 9V battery connector to one end of the switch. Solder the other end of the switch to a male header pin. Solder the black battery lead to the other male header pin.
  • As shown in the diagram above, the header pins will go into the breadboard to power the Nano at the VIN (positive) and GND (negative). The VIN pin can handle 7 to 12 volts. If you plan on powering your Arduino in a way other than a 9V battery, use a different supply pin.

Step 8: Modify the Nano If Needed.

As my project box was quite shallow, I needed to remove the ICSP header pins to fit. These pins are a secondary interface with your Arduino. Removing them will not damage your Nano as you can always load scripts through the USB port.

Note: If your Nano came needing header pins to be soldered on, simply omit these pins when assembling your Arduino.

Step 9: Wire the Inside.

  • Attach the cable gland port to the project box in the hole you drilled for it. If you are confused about how to use a cable gland, this video I found on YouTube shows one being assembled. (fast forward to 0:57.) Yours might have a rubber washer. This goes between the project box and the outside nut of the cable gland.
  • Gather the loose ends of the LED wires. Take this time to trim them to an equal length, strip and tin the ends. Feed the ends through the cap of the cable gland and use a piece of heat shrink to bunch the ends together, leaving enough length to reach the breadboard on the inside of the box.
  • Feed the wire bunch through the cable gland port into the project box and twist the gland cap to lock the wires in place, preferably around the heat shrink you used to bunch them together..
  • Separate the ground wires from the positive wires (remembering which ones you chose earlier). Solder together all the ground wires into one common ground. Attach a short wire from that bunch and finish it off with 1 male header. Use heat shrink to protect your bare solder joints.
  • Solder male headers on the ends of each positive wire. Again, use heat shrink.
  • Insert the positive end male headers into the breadboard to connect to the PWM pins on the Arduino.
  • Insert the common ground into the breadboard so that it passes through a current limiting resistor and then to GND on the Arduino.
  • Place in the battery and fit the switch through the hole in the box you drilled earlier. Fit the rubber washer between the project box and screw cap. Plug the power leads into the breadboard.
  • Snap or screw the lid onto the box. You're done!

Note: Notice in the schematics and in the developmental stages I used one current limiting resistor per LED. Typically each LED should get it's own resistor as typically, more than one LED is illuminated at once. The code does not allow for more than one LED to be lit at a time therefore using only one resistor is fine to protect the Arduino. This also saves space on the small breadboard or time soldering each LED with an in-line resistor. That said... WARNING!!! If you plan to alter the code so that more than one LED is illuminated at a time, you will need separate resistors for each LED.

Step 10: Use It.

Use Velcro straps or dabs of hot glue to affix the LEDs to plants, fences, pink flamingos or anything else in your yard. Use them inside by tucking them into wine racks, behind curtains or even hang the wires from the ceiling for a 3D floating effect in the dark! These would be a great touch for parties, weddings, film and photography.

Step 11: Going Further...

As stated before, this is an early version of this project, but it is full of so much potential! Run more LEDs by hooking up a shift register (See this instructable by JColvin91 to learn how.) Add a light sensor, solar charger, and timer for a "set it and forget it" feature! Mess with the code to add your own flare to the bugs. Share what you make and enjoy!!

UPDATE: In the last two weeks since this Instructable was published, many contributors have suggested brilliant improvements on code, hardware, and execution of this project. I strongly advise if you plan on building this, you read through the comments and replies for ideas on how to make these lightning bugs in ways I didn't plan for. It is in the spirit of open source making that I welcome all ideas that help evolve this project into more than I thought possible... and I thank all who made that happen.

Go. Make!!!

Backyard Contest

Participated in the
Backyard Contest

1 Person Made This Project!

Recommendations

  • CNC Contest

    CNC Contest
  • Make it Move

    Make it Move
  • Teacher Contest

    Teacher Contest

32 Discussions

0
None
WilkoL

8 weeks ago

I like it! I'm also thinking about some sort of synchronizing the flashes as I seem to remember that fireflies start with very few random flashes and slowly adding more and more synchronizes ones.

4 replies
0
None
WilkoLzaphodd42

Reply 6 weeks ago

After seeing that instructable, watching several videoclips on youtube and experimenting a lot, I have finally made something I'm content with. It now has some synchronization, but also a random disturbance (of the force....) Having all 12 leds blink at the same time for ever is boring.

I use several timers in the STM32F030, they all work with interrupts.
One for triggering a led to start its sequence and "look" at the other leds for some synchronization.
A second to make it blink a number of times and smooth on- and off- behaviour.
And a third timer takes care of the PWM for the leds.

A fourth timer gives off a pulse per minute, it is used to make the whole device go to idle mode after 5 hours without having the need for a real time clock.
Besides that I already have a LDR that takes care of detecting ambient light.

Now it starts when dark enough, goes to idle after 5 hours (all leds off) and then to deep sleep when it has become light again.

I measured the voltage on the (lithium ion) battery after a few days, it drops very slowly, now with the latest power reducing modes I guess it will last for several weeks before a recharge is needed.

If anyone is interested in the C-code for the STM32 chip, I'm happy to send it to you. I have written it with STM32CubeIDE and I use Low_Layer mode.

0
None
VighyWilkoL

Reply 7 weeks ago

You got me thinking and after a brief research I found that each firefly species has it's own "flashing signature", that would be an interesting addition to a project like this.

0
None
WilkoLVighy

Reply 7 weeks ago

Didn't know that, as the only flying, blinking lights here are made by Airbus, Lockheed and Boeing. I'll do some research too, maybe there actually is a species living this far North after all, and if so I'll copy that :-)

0
None
Viktor Fischer

6 weeks ago

Nice project for my grandchild. I reduced a little bit code.
/*
This script flashes 6 LEDs (yellow, of course) in random order at random intervals using PWM. Each LED is controled by its own function.
*/

int led[] = {3,5,6,9,10,11}; // LED connected to PWM pin 3, etc. I used all 6 PWM pins.
int nSize = sizeof(led)/sizeof(int); //length of an integer array

long randnum; // randnum controls the time interval between flashes and randbug controls which bug lights up.
long randbug;

void setup()
{
for (int i = 0; i < nSize; i++)
pinMode (led[i], OUTPUT);
}

void loop()
{
randbug = random(0,nSize); //randbug randomly chooses a function to execute, thus randomly chooses a bug to light up.
bug(randbug);
}

void bug(int Zahl)
{
randnum = random(10, 3000);
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
{ //each of these functions work the same way. for loops increase then decrease the output of that pin to control LED brightness.
analogWrite(led[Zahl], fadeValue); //randnum is a random time interval between 10 and 3000 ms and chooses a time interval between bug flashes.
delay(10); //delay 10 is just for the fade effect.
}
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
analogWrite(led[Zahl], fadeValue);
delay(10);
}
delay (randnum);
}

1 reply
0
None
zaphodd42Viktor Fischer

Reply 6 weeks ago

Thanks for the code! As I dive deeper into C++, I'm learning all the tricks to compact my coding and make it more efficient. The original code above feels like the "discrete version"... like when you build a spread out circuit of an IC. I'll make code updates with my later versions. Thanks again.

0
None
robertbu

7 weeks ago

Cool project with a great deal of growth and learning potential. Some comments and ideas:

+ Four AA batteries is a poor solution for an Arduino Nano board. Four AA batteries fresh from the factory would start at 6.60 volts. The voltage drops as the battery is used, and most of the power in the battery pack would be gone when the voltage reaches 4.0 volts. The minimum voltage to the raw power input pin on the Nano is 6.0 V, so only a slight bit of the batteries would be used before the Nano quits. Six AA or AAA batteries would work well. Rechargeable 2S (7.4 V) LIPO batteries is another good choice.

+ The voltage drop of a yellow LED is typically 2.1 V. Since a Nano uses 5 V, with the right resistor (47 Ohm), you can light two LEDs in series off each PWM pin with *no* additional power drain. The two LEDs would be synchronized, but they would not have to be near each other.

+ Software PWM (there are libraries and articles on the net), would allow all the pins on the Nano board to be attached to LEDs. This would give you up to 22 lights (or 44 if you placed two in a series).

+ If you would add a variable such as ‘currBug’ and assign the currently selected pin to this variable, you could rewrite one of routines to use ‘analogWrite(currBug, fadeValue);’ and get rid of most of the rest of your code.






1 reply
0
None
zaphodd42robertbu

Reply 7 weeks ago

Phew! That's allot to digest. I always thought that i should know everything before i post an instructable. But it's been more of a learning experience than ALL the research i did before hand. (Thanks to contributors like you.) Thank you for the info. And thank you for reading. I'll take your comments down and work them in to version 2.0!!

1
None
n4mwd

7 weeks ago

Interesting to watch. Another design possibility would be to use thin fiber optic fibers with all the LED's in the box. That way they wont be susceptible to corrosion as bad.

6 replies
0
None
zaphodd42n4mwd

Reply 7 weeks ago

Great idea!! I wonder how bright fiber ends would be. Probably easier to mount as well.

0
None
n4mwdzaphodd42

Reply 7 weeks ago

I remember there was a book on LEDs by Forrest Mims. In that, he drilled a hole down the center through the plastic of the LED and then glued in a glass fiber. There is some light leakage around the outside of the fiber, but you can paint it with aluminum spray paint if it bothers you. The ends are where most of the light is and, like the other poster said, you can use a blob of hot glue to make a diffuser. I don't know how far they will reach though. If hot glue doesn't work, little bits of a Styrofoam cup will.

0
None
zaphodd42n4mwd

Reply 7 weeks ago

I love his articles in Make Magazine. I remember reading about that very trick in Make. Its a great idea I'd love to explore.

0
None
n4mwdzaphodd42

Reply 7 weeks ago

Can't wait for Arduino Fireflies 2.0.

0
None
WilkoLn4mwd

Reply 7 weeks ago

Waterproofing is very important in my country too, I was thinking of embedding the led in a blob of hot glue. It may also work as a crude diffuser.

0
None
n4mwdWilkoL

Reply 7 weeks ago

Unless the copper wires are waterproof, the weather will eventually get to them and corrode them out. Unless you are using really thick wires.

0
None
josephchrzempiec

7 weeks ago

Hello awesome project. I'm not a coder my self. But i love making hardware, Firefly's are awesome. I have one question. The twisted pair wires where did you get them at that length? It looks like network wire. I need to find some like that.

1 reply
0
None
zaphodd42josephchrzempiec

Reply 7 weeks ago

Yes. It's Cat 5 Ethernet cable. 10 feet will give you 2 pairs of green and brown. (But if you want blue or other colors, you're welcome to use them.)
In the Materials section at the beginning, I provide a link to the cable i used. Read the note regarding the type of cable as well.
I wasn't a coder either. This was my first code to tell the truth. I think I'm hooked! Glad you liked it. Thanks for reading!!!

0
None
Chip Zempel

7 weeks ago on Step 11

Lovely! I live in California, I really miss the fireflies from the Midwest where I grew up. In the Blue Bayou restaurant in Disneyland, they had fireflies like this on long flexible wires, so the lights were moving as they flashed on and off. There was probably some sort of motor at the base that kept the wires in motion, but it made them very convincing. Thanks for sharing - great instructable!

1 reply
0
None
zaphodd42Chip Zempel

Reply 7 weeks ago

Well, Disney has thought of everything. Thank you for enjoying it!