How to Make LEDs Flash to Music With an Arduino

697K356339

Intro: How to Make LEDs Flash to Music With an Arduino

This is a guide to make an LED flashing circuit that "beats" to an mp3 file on your computer, which can add an awesome effect to any sound-related device, or even your room (if you extend the concept with LED drivers or plenty of resistors/transistors).

The LED flashing circuit makes use of the Minim audio library and the Arduino program Processing to analyze sound, producing a response based on a snare drum hit, a bass drum hit, and a hi-hat hit from the percussion of the audio file.

STEP 1: Parts/Tools Needed

Here is what you'll need to make the LED beat flashing circuit:

Hardware:
1. An Arduino microcontroller board. There are many versions of the Arduino, but I would recommend the Arduino Duemilanove. You can buy one online for around $20.
2. Three LEDs (different colors preferred, so you can see the difference in beats easier - I used a red, yellow, and green LED)
3. Three resistors (depending on your LEDs, the resistor value will be different - check the ratings on the LEDs to see what resistance corresponds to their maximum brightness, without burning them out)
4. A solderless breadboard
5. Some wire, to use as leads from the Arduino to the LEDs/resistors on the breadboard
6. A computer
7. A USB cable (A to B)

Software:
1. Arduino Software Environment
2. Processing Software Environment
3. The "arduino" library for Processing
4. The BeatWrite / BeatListener code from the minim JavaSound library examples

Download links for the software will follow in Step 3.

STEP 2: Build the Circuit on the Breadboard

In order to have the Arduino access the LEDs, you have to build a small circuit on a solderless breadboard. The breadboard has two parts: the inner rails (which run width-wise) and the outer rails (which run length-wise). The rails are electrically connected along their lines, so you can connect components without actually soldering them together. Here is how to make our simple circuit:

1. Run a wire from the digital GND port of the Arduino to the negative outer rail of the breadboard, as shown in image 1.
2. Place your three resistors (I used 1kilo-ohm resistors for this example, which matched my LEDs well enough) in an evenly-spaced fashion on the inner rails of the breadboard. Note that on a breadboard, a line runs length-wise, halfway down the middle. This separates the left inner rails from the right inner rails, so you can connect more components. Position the resistors so that they bridge the gap between the inner rails, as shown in image 2.
3. Place the LEDs right next to the right side of the resistors, with the anode (the longer end) in the slot right next to the resistor, and the cathode (the shorter end) into the slot that is offset to the resistors by 1 slot, as shown in image 3 (see image 4 to see the difference between an anode and a cathode).
4. Run wires from the cathode rail of the LEDs to the ground outer rail, as shown in image 5.
5. Run wires from the anode side of the resistors to digital pins 12, 8, and 2 on the Arduino to complete the circuit, as shown in image 6 and 7.

Now that we have our circuit made, it's on to the programming.

STEP 3: Program Your Arduino

Programming the Arduino is pretty simple. Through a couple of small programs, your simple LED circuit will be dancing to the beat in no time.

1. Connect the Arduino to your computer with your A to B USB cable (your computer should install its driver automatically) - see image 1.
2. Download the Arduino Software Environment from here and extract it to a folder (no installation required, just keep the folder in a safe place).
3. Download the Processing Software Environment from here and extract it to a folder near your Arduino SE folder.
4. Download the Arduino library for Processing from here, and copy it into your Processing/libraries folder.
5. Run the arduino.exe program in your Arduino SE folder.
6. Go to File -> Examples -> Firmata -> StandardFirmata, and press the Upload button on the Arduino SE (see image 2).
7. Wait a few seconds, until your RX/TX LEDs on your Arduino stop blinking (which means the Firmata firmware is installed).
8. Download and unzip the attached folder of code below (BeatWrite.zip).
9. Copy an .mp3 file into the "data" folder of the unzipped BeatWrite folder.
10. Open Processing (processing.exe), then open the BeatWrite.pde file in the BeatWrite folder.
11. Edit the line of code "song = minim.loadFile("freebird.mp3", 2048);". Instead of using "freebird.mp3", use the filename of your mp3.
12. Hit the Run button on the top-left (see image 3).

Sit back, and enjoy the light show!

STEP 4: How It Works / Customizing the Script

The reason this program works is due to the minim JavaSound library. This fancy library comes with Processing, and can do plenty of cool things with analyzing audio signals. In our case, it uses the fast fourier transform of the audio signal from the mp3 to analyze certain frequencies, and lights an LED if certain ranges of frequencies go above the allotted "sensitivity" value. In order to change this sensitivity value, all you need to do is change the "beat.setSensitivity(100);" line of code in the BeatWrite program. Also, as I said before, you can definitely extend this concept to many LEDs on a large scale. This is just a small-scale example of the potential for this great library. Also, if you know more about your mp3 than just generic hi-hat, snare, and bass drum hits, you can use the isRange(int low, int high, int threshold) method to beat the circuit to other patterns in your music. The sky is the limit in terms of audio processing with Minim and BeatDetect.

Many thanks to the Minim team for providing a BeatDetect manual with plenty of examples. This LED tutorial is a hardware extension of this software example.

STEP 5: How It Should Look

Here is a video of when I first made the BeatWrite circuit (it looks a little different than the one we're building, but the concept is the same).


Have fun with your new Arduino BeatDetect circuit!

275 Comments

Hi, I am attempting to use this program with more LEDS. But for some reason, as soon as I upload the firmata example, the LED that goes to pin 1 turns on and then nothing else works. Any thoughts? Thank you.
Here are some tips that i would have needed:
-maybe Processing has not automatically installed the minim library, then you have to install it manually by going to sketch->import library->add Library and searching for minim
-if you get an error with "ArrayIndexOutOfBounds" or maybe "NullPointerException" maybe write 0 instead of 1 in the bracket in line 35 (worked for me)
Bonjour,
Je cherche un projet Arduino permettant de déclencher des éclairs sur un enregistrement d'orage réalisé et stocké sur le MP3 DF Player mini.
Code kinda works (it only displays changes in kick, hat, snare for a few seconds), arduino wired corretcly, but lights dont flash. Any ideas?
I'm getting this error:

==== JavaSound Minim Error ====
==== Unsupported Audio File: not a MPEG stream:null
java.lang.RuntimeException: java.lang.NullPointerException
at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:206)
at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
at javax.media.opengl.Threading.invoke(Threading.java:191)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
at processing.opengl.PJOGL.requestDraw(PJOGL.java:688)
at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1651)
at processing.core.PApplet.run(PApplet.java:2256)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at ddf.minim.javasound.JSMinim.getAudioRecordingStream(Unknown Source)
at ddf.minim.Minim.loadFile(Unknown Source)
at BeatWrite.setup(BeatWrite.java:58)
at processing.core.PApplet.handleDraw(PApplet.java:2361)
at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:665)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:649)
at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1289)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1119)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:994)
at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1300)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:302)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I'm thinking that it wants a MPEG file, but it also gives an error with that, so could anyone help?
Hey, how do I download Minim?


Can I attach servo instead of LEDs here. If so how do I do that ?
I have a little problem that my led doesn't light up even though I changed between arduino uno and mega adk and it still doesn't work and Processing's Console says:


==== JavaSound Minim Error ====

==== Don't know the ID3 code TXXX



==== JavaSound Minim Error ====

==== Don't know the ID3 code TXXX



==== JavaSound Minim Error ====

==== Don't know the ID3 code TXXX



==== JavaSound Minim Error ====

==== Don't know the ID3 code TSSE



==== JavaSound Minim Error ====

==== Don't know the ID3 code TXXX

And I firmly believe that my LED light is working properly
This is my 3rd try. Error in reference to line 26 song.play();
COM6

==== JavaSound Minim Error ====
==== java.io.FileNotFoundException: 04 Time

==== Minim Error ====
==== Couldn't load the file 04 Time //(mysong)
NulPointerException
Running Win8.1 on intel CORE i7
import <processing.serial>.*;
^~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
processing.serial: No such file or directory


Where is the processing.serial file and/or how do I get it???
The code doesnt work now
it gives a fuckiin long error
Help pls
java.lang.RuntimeException: java.lang.IllegalAccessError: tried to access class processing.core.PApplet$RegisteredMethods from class cc.arduino.Arduino$SerialProxy
at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
at
What I did to make mine work for certain errors:
- Getting some kind of function thing "not found": Processing > Sketch > Import Library > Search up and Get 'Minim' (I didn't have to put anything into Processing/libraries)
- If no LEDs are flashing but it's running: run println(Arduino.list()); on one of the lines in the BeatWrite.pde file to list the ports available. Then find your Arduino, mine was something like usbmodem and was the 6th entry. So I changed arduino = new Arduino(this, Arduino.list()[1], 57600); to arduino = new Arduino(this, Arduino.list()[5], 57600); because mine was the 6th entry, and *it's 0-indexed*
- To figure out which port your Arduino is on go to the Arduino IDE > Tools > Port, to find which one it is
Mine works too !!! :) I want to advance this now.any idea how I can make this computer independant device?(without the need to press play on processing sketch from computer to play song); just add an external battery and the leds blink according to music maybe upload the song on arduino somehow and play through speakers...???
Mine works too..thanks.. I want to advance this now.any idea how I can make this computer independant device?(without the need to press play on processing sketch from computer to play song); just add an external battery and the leds blink according to music maybe upload the song on arduino somehow and play through speakers...???
Mine works too.. I want to advance this now.any idea how I can make this computer independant device?(without the need to press play on processing sketch from computer to play song); just add an external battery and the leds blink according to music maybe upload the song on arduino somehow and play through speakers...???
Hey Guys,
I got this project working correctly without wiring any resistors in.
It would not work when I had them wired in.
Once you have you're SW stored in the correct folders this will work perfectly for you.
This code works with any MP3 file also.

Hey, I'd rly like to know how many LEDs you can connect to it? Like if I want like 10 LEDs blink just on bass-made sound, is that possible to make it ? :D

Btw, you did such a nice job there :)
You can connect as many LED's as inputs you have.

I made it!! Super awesome project. Was wondering how this could work to trigger relay signals. I have made few projects with relays and understand how they work but it seems this output signal is not the same as a HIGH/LOW that that relays need. Is it possible to do with this project?

More Comments