Beat Detection and Music Visualizer using Processing, Arduino, LEDs, and a glass head.
I had a strand of RGB LEDs that I ordered from China which turned out not to be individually addressable as I thought, so I came up with this project as a way to put them to good use! Be sure to watch the video to get the full effect.
The software which runs the music visualizer and beat detection is built in Processing 2.0, and communicates with an Arduino in the base of the head over USB, which then sets the colors of the LEDs. The Arduino part is based on the excellent RGB LED Strips tutorial over at AdaFruit.
* Everything used in the AdaFruit tutorial, including Arduino Uno, USB cable, non-addressable analog RGB LEDs, power supply, transistors, MOSFETs, and a breadboard and wiring to put it all together. AdaFruit sells most of those things, but you can also get the LEDs from eBay or AliExpress, and the rest from an electronics shop like Fry's.
* A prototyping board. I got mine from RadioShack for around $3.
* A few headers to slot the proto-board on to the Arduino. A pack of 100 in strips should be around $3 or less.
* Some solder.
* (Optional) Some 4-pin JST connectors, to put a detachment point between the Arduino and the LEDs. AdaFruit sells these for about $2.
* A glass head. Mine came from Pier 1 and was about $20.
* Some cotton balls (for the brain) and pillow stuffing (for the rest)
* A wooden box to use as a base. I used the bottom half of an unfinished jewelry box from Michael's for around $5.
* Some silicone glue to attach the head to the base. Although any reasonably strong glue would probably work.
* 2 screws, nuts, and nylon standoffs to attach the Arduino to the box. Arduino mounts can take M3-0.5 or UNC 4-40 screws, depending on what's available to you. I got all of these at the hardware store for less than $1.
* A little bit of electrical and masking tape
* A soldering iron
* A drill or small saw to cut holes in the box
* Screwdriver and pliers to attach the Arduino board
* A Mac with Arduino 1.0 and Processing 2.0 installed. (You can also use a Windows or Linux PC)
Step 1: RGB LED Strips Tutorial
The first step is to follow the AdaFruit RGB LED Strips tutorial.
At the end of that tutorial, you should have a working circuit on a bread board, and verified that your LED strips and power supply are functioning properly.
Step 2: A Head Full of Cotton
After the LEDs are working is a good time to fill your head with fluffy things. In general my technique was to alternate layers of stuffing and LEDs, and then sort of massage things to get the shape of the brain correct, and the light distributed evenly. In general, the pillow stuffing diffuses the light much better than the cotton balls, but the contrast between the two textures is very interesting. (And the source of inspiration for making the brain!) Another trick was that for my strand, the red, green, and blue LEDs were separate from each other, which resulted in a pleasingly unequal light distribution in some areas. Also, there seemed to be no limit to the amount of stuffing I could cram in there.
One subtle note: With my particular glass head when the lights were off and the LEDs were on, the eyes were a little hard to pick out. To help your brain recognize the shape as a face, I put a layer of translucent masking tape over the eyes and trimmed it with a razor blade. A simple solution!
Step 3: Beat Detection, Music Visualization, Arduino, and Processing
Since the LEDs are not addressable, the entire strand can only be one color at a time. I wanted an input source that would be dynamic enough to be interesting, but simple enough to be represented by a single color. I was going to party in a few weeks, so I figured musical beat detection would be a fun thing to try.
Initially, I wanted the head to detect beats with a microphone. Unfortunately, I had trouble getting the one I had to function with the Arduino, so I switched to a different input source I'd been meaning to play with: Processing. As a side benefit, I didn't have to filter out any ambient noise!
In order to control the Arduino from Processing, I used Firmata (a generic library for controlling microcontrollers from a PC). The Firmata code for Android and Processing, and a brief tutorial are here: http://playground.arduino.cc/interfacing/processing.
Processing ships with the Minim library for dealing with audio, and there was an example provided for using the Fast Fourier Transform (FFT) to analyze spectrum. I found the output to be pretty non-intuitive, so I wrote a music visualizer to help me "see" the music, and try to identify visually when a beat had taken place.
I started getting pretty good results by looking for large changes in any of the FFT "buckets", which roughly correspond to octaves. Ie, if an octave has been at the same volume (usually silent) for a while, and then suddenly has sound, my software guesses that it's a beat. If not a beat, something interesting that you are probably paying attention to!
To get a color, each bucket is assigned a range on the color wheel (which slowly rotates with time.) When the beat happens in a bucket, the Arduino/head is illuminated with the bucket's associated color. Additionally, there's a pretty quick fall-off so that each beat fades away within a second or so. The color of the beat being sent to the Arduino is indicated by the horizontal bar at the bottom of the visualizer.
The program also has some keyboard controls to adjust the vertical scale of the visualizer, the color rotation, or the sensitivity of the beat detection.
Lastly, the Processing sketch analyzes audio from the default Audio Input port of the computer. The computer's operating system controls whether this is your microphone or the input jack on your sound card. On my Mac, I used SoundFlower to route the computer's regular audio (such as from iTunes) into a virtual Audio Input port which I made the system default, so that the Processing sketch could analyze it. Definitely awkward but it worked.
Step 4: Object Permanence
This thing was going to be lighting things up at a party, so I figured a bread board was a little too fragile. Enter prototype boards! These little guys are one step up from the bread board: more permanent, but you've got to solder stuff. I got mine from a RadioShack up the street, but there are lots of options on the internet.
My kind didn't have copper connecting each strip, so I had to make connections between components by soldering the leads to each other directly. With enough solder and a steady hand, this worked surprisingly well! I only needed a few extra pieces of wire, and that was mostly to keep the layout looking neat. Also one critical inspiration was that I could solder on both sides of the board.
If your LED strip didn't come with a connector, this would be a good time to put one in. This will allow you to detach the LEDs from the Arduino, which is very useful for building a housing around it.
The last clever thing I did here was to organize the layout of the board and then solder on some headers such that it could be "snapped" onto the Arduino similar to a shield. Annoyingly, one set of headers on the Arduino (the one that includes the ground) is offset by about half a pin. I had to bend the header before soldering it to the proto-board in order for it to align properly.
Step 5: All Your Base Are Belong to Head
After lining up the Arduino inside the wooden box, I marked where I would drill holes for the Arduino's USB and power jacks, the LED connector cable, and the Arduino's mounting screws. In my case, a super small saw (like on a swiss army knife) was a more useful tool to create square holes than the drill.
I opted to use only 2 mounting screws because that would allow me to hide them under the neck of the glass head. Plus the holes for the power and USB jacks made the Arduino pretty firmly wedged in there. To keep things tidy, I taped down the connector cable.
Glueing on the head was surprisingly easy. I placed it on the box where I wanted it, and traced a circle around the neck. I picked a silicone glue because it's super goopy and I wanted there to be good contact between the wood and the relatively curvy bottom rim of the neck. The glue starts to set in about 5 minutes, so I had plenty of time to squeeze out a thick ring of it over my tracing and then place and align the head on top. For good measure, I also applied more glue around the outside, and used a paper towel to wipe away any extra. And then vigorously washed my hands with soap! The glue takes about 24 hours to cure and doesn't smell great while doing it - I recommend sticking it outside or in the garage if you have one.
Step 6: Improvements
As cool as the visualizer was, it was kind of a pain to be tethered to a computer that had to be running my special software (and that bizarre audio input setup.) The piece would be better if it was stand-alone and could accept audio from a more general source. To accomplish that, I'd put an audio input jack into the base and do all the beat detection logic on the Arduino itself. If I was feeling fancy, I might add a small screen or tiny LED array in the base to show the visualization.