×

Hello there friend! SparkFun will be closed on 8/17/2018 @2pm for a company event. We will not be offering our normal Friday tour or Local Pickups during this time. We will resume normal operations on Monday 8/20/2018. Any orders placed after 2pm MT on 8/17/2018 will be processed when we resume normal operations. Have a great day!

Enginursday: Audio Reactive Wearable Fiber Optic

Andy is working on an audio control subsystem for his new fiber optic rave suit...let's check it out.

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.

Hardware

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.

Software

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_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() {
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.