Introduction: 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!
Attachments
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
Have fun with your new Arduino BeatDetect circuit!
17 People Made This Project!
- ahowardmsu05 made it!
- colleen.lanz made it!
- shanjng made it!
- shanjng made it!
- tuica made it!
- benebi8 made it!
- nonon4 made it!
- DungM2 made it!
- LakshyaS10 made it!
See 8 More
267 Comments
Question 5 days ago
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
Question 3 months ago on Step 3
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???
Question 4 months ago on Step 3
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
Tip 8 months ago
- 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*
Reply 6 months ago
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...???
Question 6 months ago
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...???
9 months ago
Heres mine!
https://youtu.be/GbyW9H1DhXY
Reply 6 months ago
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...???
Question 1 year ago
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.
Question 1 year ago on Introduction
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 :)
Answer 1 year ago
You can connect as many LED's as inputs you have.
Question 2 years ago
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?
2 years ago
Hi I am new to Arduino and just trying to learn the code, I know a little Bash scripting.
My question - is it possible to modify the code to create a loop to play through a series
Of mp3 files ( create a sort of play list).
Question 2 years ago
I fixed the first problem but now it is saying that cc doesnt exist. Any help to fix this probelm will be appreciated.
Answer 2 years ago
Download processing-arduino zip from >this< link, and extract the 'arduino' folder into Processing sketchbook folder (you can find its location on File>Preferences).
Question 2 years ago
When I run the other program and press go, it says that ddf is missing and that a libray might be missing. How would I solve this problem?
Tip 2 years ago
1. For missing libraries go to Processing IDE > Sketch > Import library > Add Library and install Minim
2. In "arduino = new Arduino(this, Arduino.list()[1], 57600);" replace 1 with 0
3. If LEDs still don't blink, reverse/flip the LEDs around. Worked for me!
Question 2 years ago
Hi, ive been trying to upload the standard firmata, but ive already run into a problem, here is the error code:
c:\program files (x86)\arduino\hardware\tools\avr\bin\../lib/gcc/avr/4.9.2/../../../../avr/bin/ar.exe: unable to rename 'core\core.a'; reason: File exists
exit status 1
Error compiling for board Arduino/Genuino Uno.
How can I fix this?
Tip 2 years ago
I have successfully made this project, check out https://github.com/Pwenzel99/Flash-LEDs-To-Beat-of-Music-Arduino/tree/master for my software.
Tip 2 years ago
On beat write make sure you have the correct pin numbers for the arduino and you have to change the arduino = new Arduino(this, Arduino.list()[1], 57600) to arduino = new Arduino(this, "The Port You Are Using", 57600). You find the port you are using in the arduino SE by clicking Tools->Get Port Info. I don't know if this is what everyone needs to do but I had to change the digital.write in BeatWrite to analog.write.