Introduction: Arduino Fireflies

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 passi…

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.

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