Do It Yourself
Digital Fireflies
A fun project coded in
Arduino for the ATmega328 Microcontroller
(December
2012)
This was a simple little project that was really just an excuse for me to learn Arduino. I bought some really attractive Ball canning jars at the market and thought that it would be really cool to use them to make some kind of bedside firefly jar. I thought that I would use an array of RC transistor timers to drive some vintage 70's amber lens LEDs I had in stock. But I wanted these virtual fireflies to act like the real thing, and I soon realized that there would have to be 4 separate operations to depict a realistic firefly blink, with a quick fade-on period, a brief stay-on period, a slower fade-off period, and a long off-rest period. Visualizing the large array of discrete RC timers I would need to create several fireflies was going to be prohibitively complex to shove into a jar lid, so I thought that this would be a really good job for a microcontroller. Proto-typin'! I got an Arduino UNO R3 board from Adafruit Industries that acts as a USB interface to an ATmega328 microcontroller, and I wrote the code on my PC using the Arduino open-source software. 6 of the 14 ATmega328 I/O pins provide 8-bit pulse width modulated (PWM) output with the 'analogWrite()' function and I wrote the code to create the illusion of 6 firefly tails, giving each of them their own 'personality' through independently varying rates and fluctuations, so that the sequence would be ever changing. I also wanted my fireflies to respond to the environment, because as we all know the real things would never come out to do their dance before twilight - so I added a photoreactive circuit using a common photo resistor and a 2N3904 transistor to turn on the LEDs only when the ambient light was suitably dimmed. This was going to end up being a self-contained autonomous display, so I would prototype it on the UNO board to prove the concept, then I would remove the programmed Atmega328 microcontroller chip and transplant it to my own smaller battery powered board that would contain the completed circuit, which I would build into the bottom of the jar lid. I wrote this code as a single continuous loop program with 24 subroutines to independently control the four states (fading on, staying on, fading off, and staying off) of each of the 6 output pins (ledPin function) to drive each LED, and global variables had to be set to keep track of what state each firefly was in and at what point in that state, so that the results could be written to the pin outputs and those variables could also be updated with each cycle of the loop, which constantly changes the write values, creating the illusion of multitasking six independent routines with a single processor. It is a very simple program and it works satisfyingly well - more satisfying was the fact that I was able to finish this code and have it debugged within just a day or two of beginning to read up on what the heck Arduino was. It is quite literally that easy to work with. If you have any coding experience at all in BASIC or C then you will have no problem transposing your coding chops to Arduino. It's awesome!
Here is my digital firefly code, which you can cut and paste into Arduino and upload to your own compatible microcontroller: |
----------------------------------------------
/* ledPin is a preassigned
variable that controls output to the selected pin number.
void setup() {
void loop() { // Loop Start // Pin 3 Fade Up Sequence:
// Pin 5 Fade Up Sequence:
// Pin 6 Fade Up Sequence:
// Pin 9 Fade Up Sequence:
// Pin 10 Fade Up Sequence:
// Pin 11 Fade Up Sequence:
// Loop End
------------------------------------------------ This program can be condensed a great deal, but I have laid it out so that it is easy to understand and you can see what is going on. I encourage anyone to hack my code and make it their own. Change around the variables and make the fireflies behave however you want - make red devil fireflies - or screaming white ones - it is all up to you. They are after all YOUR fireflies! |
It Is A-Liiiive! Transplanting the programmed ATmega328P from the UNO to my dedicated board. Ignore the diode on the left - there was just too much voltage drop with the AAA batteries so I bypassed the V+ input protection that I had originally intended. Beware of Static - the killer of CMOS! I have a ground lead on my workstation that goes straight to the cold water pipe, and you need one too. Make sure that your body is well grounded while attempting to dislodge the ATmega328 from its comfortable and safe home on the Arduino board. To handle CMOS is to be well grounded. A good life philosophy. The photoresistor and 2N3904 transistor will turn on the LEDs when light levels are dimmed sufficiently, and they will be turned off when ambient light gets bright. R9 is a trim pot, which you need to set the light sensitivity. I decided for size reasons to use 3-AAA lithium batteries for power at 4.5VDC. The output pin numbers of the ATmega328 are different than those on the UNO board, so I have noted both on the schematic. One tip too is that if you decide to add the photoreactive circuit then it is necessary to put some kind of light shield around the photoresistor to prevent the light from the LEDs from feeding back and shutting down the display, as this will cause a rapid flicker. The R10 10K pull up resistor will prevent the microcontroller from resetting itself during operation. And even though I used a generic perf board and was not going to be using this chip for data or any other kind of high frequency I/O I still put a ground plane under the socket and around the clock crystal - just cause.
For esthetics I also enhanced the appearance of the LEDs by putting on a few thin coats of semi-opaque pearlescent nail polish. This really diffused the light from the clear amber lenses and gave the fireflies a realistic look in the final display. Here is the schematic for
the final dedicated board for the programmed ATmega328:
|
|
Making Multiple Copies: I made a few firefly jars to give away and did some tweaks, such as removing the trim pot and inserting added series resistors instead. Here I show some details about assembly: I dipped the LEDs in light pearl nail polish and let them dry overnight. Here above I have snipped the LED leads, cut the shrink tubing, and cut and stripped the wire, which I actually recycled from 1980's era serial printer cables (great 24awg solid copper!). Here above I show the two layers of shrink tubing I used to insulate the leads, on the right I have not yet shrunk the outer sleeves. Finished firefly tails!
|
Fran's Writings on Design and Engineering || Fransworld Daily Updates
All images and text are copyright Frantone Electronics. No content of this website may be published or distributed without prior written permission from Frantone Electronics and any reproduction or manipulation of the content of this website for any purpose is strictly prohibited. |