I recently used a pair of MSGEQ7 seven-band audio spectrum analyzer chips to construct a rather cool display. In this first incarnation, we end up driving 14 LEDs -- seven each for the left and right audio channels.
To set the scene before we leap into the fray, let's take a look at this short video to get an idea of where we will end up.
Since I think this will be of interest to beginners, as well as practicing engineers, I will go slowly and take the time to explain some basic concepts here and there. In these cases, I'll kindly ask anyone who already knows this stuff to bear with me.
Let's start with the fact that my music source is my iPad. Since I want to keep things as simple as possible, I intend to drive everything from the iPad's headphone socket. The problem is that plugging anything into the headphone socket disables the internal speakers. Similarly, if you are using the iPad to drive a Bluetooth boom box, for example, plugging something into the iPad's headphone socket will terminate the Bluetooth audio feed. Thus, my first step was to purchase a stereo splitter cable on Amazon. This allows me to use the output from my headphone socket to drive both an external speaker system and my spectrum analyzer.
At the same time, I ordered two stereo audio cables and a cheap and cheerful amplifier and speaker system.
Choosing a microcontroller
We can construct this audio spectrum analyzer project using pretty much any microcontroller that supports the required set of features. Assuming we wish to display the left and right audio channels separately, we need at least two analog inputs, two regular digital outputs, and 14 pulse-width modulated (PWM) outputs. You could get by with just one analog input and seven PWM outputs if you merged the two channels (this will be discussed later).
If you wish, you could process the audio data using an eight-bit Arduino Mega microcontroller development platform with 256 Kbytes of Flash and 8 Kbytes of SRAM running at 16 MHz. You could also use an Arduino Uno (which supports only six PWM outputs) if you would be happy combining the left and right channels and losing one of the frequency bands. I decided to use a chipKIT MAX32 platform to process the audio data stream. This little beauty boasts a 32-bit processor running at 80 MHz, along with 512 Kbytes of Flash and 128 Kbytes of RAM.
The chipKIT MAX32 has the same physical footprint and the same input/output (I/O) pins as the Arduino Mega. Also, the integrated development environments (IDEs) for these two platforms are almost identical, which makes our lives a lot easier. The main thing to note is that the chipKIT MAX32 employs a 3.3V supply, while an Arduino Mega uses a 5V supply. This will affect some of the component values, but this will be noted at the appropriate points in the following discussions.
Introducing the MSGEQ7
The MSGEQ7 is a seven-band graphic equalizer chip you can buy for $4.95 from SparkFun. It will work with a 5V supply (like the Arduino Mega) or a 3.3V supply (like the chipKIT MAX32).
Inside the MSGEQ7 are seven band-pass filters tuned to 63 Hz, 160 Hz, 400 Hz, 1,000 Hz, 2,500 Hz, 6,250 Hz, and 16,000 Hz. Each filter has an associated peak detector, as illustrated below. The clever thing is that the outputs from the seven peak detectors are multiplexed together, which explains how everything fits into a teenie-weenie eight-pin package.
Everything is controlled by two digital signals called RESET and STROBE. As shown in the waveform diagram below, a positive-going pulse on the RESET signal kicks everything off. Though the data sheet doesn't say so, my impression is that this pulse takes a copy of the current peak detector outputs and stores (latches) the values. We then apply seven negative-going pulses to the STROBE input. Every time the STROBE input goes low, we can read the value of one of the bands on the DATA_OUT signal, starting with 63 Hz and working out to 16,000 Hz.
The DATA_OUT is an analog value whose magnitude reflects the value from the corresponding peak detector. This value can be read using one of your microcontroller's analog inputs.
Actually, I recreated the timing diagram shown above from the original datasheet. If you read the comments at the bottom of this column, you will see a question by David Ashton. David points out that, as the minimum strobe pulse width of 18µs is less than the minimum output settling time of 36µs, this implies that you can read the data even after the STROBE signal has returned to its HIGH state. Based on this, David notes that you could actually read the data during the "purple times" in the above diagram.
If this were to prove to be correct, then -- based on the timing specifications in the datasheet -- a better representation of the timing relationships and waveforms would be as illustrated below.
Since it's not possible to make a definitive decision based on the existing datasheet, I talked to John Ambrose at Mixed Signal Integration -- the company that makes the MSGEQ7 (along with many other interesting products). John confirmed that applying a positive-going pulse to the RESET signal does indeed latch the current frequency values.
John went on to explain that the DATA_OUT signal is clamped to 0V when the STROBE signal is HIGH. This has several implications, including the fact that you cannot read the data when the STROBE signal is in its HIGH state. Also, this means that the minimum strobe pulse width ("ts") really isn't 18µs; instead, it's equal to the output settling time ("to") plus however long it takes for you to actually read the sample (let's call this "tsr" for "sample read time"). Based on this, the definitive timing diagram is actually as shown below.
With all that behind us, the following illustration reflects the additional components we need to make things work. The resistors are all 1/4 watt, and the capacitors are 50V ceramics. (These components are of the lead-through-hole variety.) In the original data sheet, the value of C2 is shown at 0.01µF, but I ran across an application note somewhere that said it was better to use 0.1µF, so that's what I did.
I decided to use two MSGEQ7s -- one for each audio channel (left and right). If you wish to use a single device for both channels, you can employ the circuit variation that's shown at the bottom of the above illustration.
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 2 >