Here's a step-by-step guide to constructing a simple 14-band (2 x 7) audio spectrum analyzer using two MSGEQ7s and a chipKIT or Arduino microcontroller development platform.
Creating the first-pass software
Before we add the LEDs to the breadboard, this is a good point to pause and make sure that everything is working as planned. The software for the spectrum analyzer is identical for the chipKIT MAX32 and the Arduino Mega.
Let's start with a simple program that reads the values coming out of the spectrum analyzer and displays them on a serial I/O window on your host computer. You can access the full source code (including copious comments) by clicking here.
We declare our RESET and STROBE signals (I called these "ctrlReset" and "ctrlStrobe") and assign them to digital pins 23 and 22 on the microcontroller, respectively. Also, we declare our left and right channel data pins and assign them to analog pins 0 and 1, respectively.
As part of this, we declare two integer arrays, "spectrumLeft" and "spectrumRight," each of which contains seven elements. This is where we will store the spectrum values from the MSGEQ7s.
In the setup() function, we initialize the serial communications and select a data rate of 9,600 baud using the Serial.begin() function. Next, we define our "ctrlReset" and "ctrlStrobe" signals as digital outputs. Finally, we set the "ctrlReset" and "ctrlStrobe" signals to their inactive values of LOW and HIGH, respectively.
Next, we have the main loop() function as shown below. We call a readMSGEQ7() function, which actually reads the spectrum data values out of the MSGEQ7 chips and stores these values in our "spectrumLeft" and "spectrumRight" arrays.
As soon as we return from the readMSGEQ7() function, we display the values we've read out of the MSGEQ7s on to our host computer's screen. As you can see, most of the above code is used for formatting to make sure everything lines up nicely.
The readMSGEQ7() function is shown below. We start by generating a positive pulse on the "ctrlReset" signal. This causes the MSGEQ7s to latch (store) their current spectrum data values, and it also resets the multiplexers in the MSGEQ7s. We follow this pulse with a delay of 75µs in order to meet (actually, slightly exceed) the minimum reset-to-strobe time of 72µs. If the truth be told, a lot of example code I've seen omits this delay, and my own system works without it, but I'd "rather be safe than sorry," as they say, so I'm keeping this delay in my code.
Now we enter a loop, which we execute seven times. The first iteration will return the 63Hz data values, the second iteration will return the 160Hz values, and so on. We start each cycle by pulling the "ctrlStrobe" signal LOW. Then we wait for 40µs to allow the outputs to settle. Next, we read the spectrum values out of the MSGEQ7s and store them in the appropriate elements of our "spectrumLeft" and "spectrumRight" arrays.
Why do we divide these values by 4 before storing them? The analog-to-digital converters (ADCs) on the microcontroller's analog pins return values from 0 to 1,023 (these correspond to 0V to 3.3V on the chipKIT MAX32 and 0V to 5V on the Arduino Mega). In the fullness of time, when we come to drive our LEDs, we will require only values in the range of 0 to 255, and the easiest way to convert a value of 0 to 1,023 into a value of 0 to 255 is to divide it by 4.
The last thing we do in our loop is to drive the "ctrlStrobe" signal HIGH and wait for 40µs. A lot of example code I've seen omits this 40µs delay, which -- when added to the earlier 40µs delay -- is used to meet (actually, slightly exceed) the minimum strobe-to-strobe time of 72µs. My own system does work without this delay, but I think you're on shaky ground if you do decide to leave it out. I'll talk about this more in my next column, which will describe the test procedures I used and the interesting things I discovered along the way.
For the moment, just feast your orbs on the following screenshot, which shows the data from the MSGEQ7s being streamed to the serial I/O window on my notepad PC.
In case you were wondering, the tune being played is one of the tracks from Babel by Mumford & Sons. You can see all this in action on this video on YouTube.
Page 1: Introducing the MSGEQ7
Page 2: Creating the first-pass hardware
Page 3: Creating the first-pass software
Page 4: Modifying the hardware to add the LEDs
Page 5: Modifying the software to drive the LEDs
To Page 4 >