SparkFun will be closed on Tuesday, November 5th to support our team in being able to go out and vote! Orders placed after 2 pm MT Monday, November 4th will ship on Wednesday, November 6th. Thanks for your patience and understanding.
Alright, so I'm in the middle of a bit of a larger project at the moment; I've been revamping my Fiber Optic Light Suit --- incorporating better controls, redesigning 3D-printed parts and now adding audio input! The video below gives a little demo and explanation of the basics of this neat little feature.
What I have is an MSGEQ7 Audio Spectrum Analyzer taking its input from SparkFun's MEMS Microphone Breakout. I wanted something that could do quite a bit of math for my application and still have enough clock cycles left over to run my LEDs at a particularly high frame rate, so I'm using a Teensy 3.6 to read the input from the equalizer. However, the pins on the Teensy run at 3.3V logic and are not 5V tolerant, so I'm also using a logic level converter to get all of my logic levels where they should be. All of this is pretty standard if you're making a multichannel audio equalizer.
One thing I've always wanted to implement was software to map the frequency of sound in the environment around me to a hue of color. To accomplish this, I ditched the standard RGB (Red, Green, Blue) scheme of color data and went instead to HSV (Hue, Saturation, Value). Hue, which is the color value (e.g., red or aqua) was mapped on a scale from 0 to 255. I divided this into seven equally spaced steps in hue around the color wheel (Numerical values for ROYGBIV) and assigned the value (or brightness) for each different hue to each channel from the audio spectrum analyzer. The channels are then averaged and written to the LEDs. In order to get started, use the Arduino library manager to download the libraries for the MSGEQ7 as well as FastLED, which I find to be a great tool for controlling strips of LEDs using as few clock cycles as possible.
Once you have those libraries downloaded, the below example code should get you started mapping frequencies to hues of color.
#include "FastLED.h"
#include "MSGEQ7.h"
//LED Setup
#define NUM_LEDS 32
#define DATA_LEFT_ARM 11 //yellow
#define CLOCK_LEFT_ARM 13 //blue
CRGB leftArm[NUM_LEDS];
//Equalizer Setup
#define pinAnalog A1
#define pinReset 16
#define pinStrobe 17
#define MSGEQ7_INTERVAL ReadsPerSecond(5000)
#define MSGEQ7_SMOOTH true
CMSGEQ7<MSGEQ7_SMOOTH, pinReset, pinStrobe, pinAnalog> MSGEQ7;
void setup() {
FastLED.addLeds<APA102, DATA_LEFT_ARM, CLOCK_LEFT_ARM, RBG, DATA_RATE_MHZ(24)>(leftArm, NUM_LEDS);
MSGEQ7.begin();
}
void loop() {
bool newReading = MSGEQ7.read(MSGEQ7_INTERVAL);
if (newReading) {
visualizeAllChannels();
}
}
void visualizeAllChannels() {
CRGB spectrumHue= (0, 0, 0);
uint8_t colorValue[7];
double colorNormalize[7];
uint8_t hueMixer[7] = {10, 15, 225, 30, 80, 180, 150};
for (uint8_t channel = 0; channel < 7; channel++) {
//hueMixer[channel] = 36 * channel;
colorValue[channel] = MSGEQ7.get(channel);
colorNormalize[channel] = colorValue[channel] / 255.0;
spectrumHue = spectrumHue + (CHSV(hueMixer[channel], 255, colorValue[channel]) / 7);
}
for (int LED = 0; LED < 32; LED++) {
leftArm[LED] = spectrumHue;
}
FastLED.show();
}
This means that a bass note will show up as red, mid range as green, and treble as blue. In the video, mostly mixes of greens and blues are visible as the audio input is coming from my tiny, high-frequency cell phone speaker. The speaker in my office hits red in all of the places that I would expect and gives a much fuller range of colors.
I wonder what it will look like at a live show with decent audio quality. For now, all I can do is wonder. Please let me know what you think in the comments below.
See our LED page for everything you need to know to start using these components in your project.
Great project! I have used the MSGEQ7 on a few projects and it is a great chip to offload a lot of math. I like the idea of mapping color to frequencies.
Very cool! I already have the MSGEQ7 and the MEMS Microphone Breakout for a sound-reactive lamp I was planning on making but never got around to starting. Your code approach was a lot simpler than anything I was going to do, so I'm definitely saving this link for when I ever get around to building my lamp :)
That sounds awesome! I'm happy I could help. Check out some of the minimum brightness functions of the FastLED library, thatll let you keep the lamp on to light up the room but still allow for music visualization on top of that