Audio Sensor Development Part 1: Microphone Math

Making LEDs blink and change colors is lots of fun, and pre-programmed sequences can look amazing. But wouldn’t LEDs be even more fun if they could react to their environment? The colors and patterns suddenly mean something. Depending on the sensors used, LEDs could react to temperature, movement, position, sound, and more.

We’ve been experimenting with music-reactive projects for quite a while, starting with the Shifty VU Shield in 2009. The Shifty VU Shield accepted a stereo audio input and allowed an Arduino to display sound-reactive patterns on a chain of ShiftBrite LEDs or OctoBrites.

In 2011, Garrett made a one-off project for a friend: The LED Viking Mohawk. This project was based on an Arduino Pro Mini and MSGEQ7 chip, and didn’t require a line-in audio connection; it used a microphone to analyze nearby sound and display reactive patterns.

There have been several other audio-reactive projects, but let’s circle back to the point of this article. The RGB Shades are the ultimate wearable LED accessory, and if they reacted to music would be even more fun. We did a quick prototype of the concept in 2014 using a prototype RGB Shades:

After the successful delivery of Kickstarter rewards and producing a few more batches of the RGB Shades, we decided it was time to revisit audio sensing. The RGB Shades (and LED Matrix Shades) have some expansion headers on them, so we decided to make a small add-on board.

The previous projects were mostly tossed together from available parts, and any problems with sensitivity or noise were either ignored, adjusted out, or hacked away in software (you can do a lot with digital smoothing!). But for this design, we want it to be simple, stable, and not require a lot of tweaking by everyone who wants to use it.

* Input audio with typical music frequencies over typical volume ranges
* Output several frequency levels scaled for microcontroller ADC
* Attach to RGB Shades and LED Matrix Shades

* Electret microphone (20Hz - 20kHz)
* Pre-amplifier
* MSGEQ7 spectrum analyzer chip

So let’s start with the microphone. Any problems here will be magnified through the rest of the system!

The microphone was selected (as usual) on price versus performance. The CUI CMA-4544PF-W is pretty inexpensive and claims response from 20Hz to 20kHz. It’s a standard electret microphone element with typical sensitivity.

Here are the specifications:

From the above specs, we can see this is a very average electret microphone as used in countless devices; it’ll probably work fine with any common electret mic application circuits. But how do you use these specifications to design for a desired range of audio environments? Most people seem to use rule-of-thumb designs and expect to tweak the gain later, perhaps jumping from 10x to 100x gain if it seems about right, but even then not knowing what the theoretical range of allowable sound pressures will be. Tweaking a design in response to real-world testing is expected, but blind modification may hurt either the top or bottom end of the scale. We’re hoping that this design can be used from about 70 dB SPL to 100+ dB SPL, so a little math is in order.

We’re initially trying to design a pre-amplifier circuit, so we need to know the peak voltage at various sound levels. The typical specification of this microphone is -44 dB with an input frequency of 1 kilohertz, at a sound pressure level of 1 Pa (Pascal), where the 0 dB point is 1 volt / 1 Pa. A Pascal is a measure of pressure that can be applied to air pressure (and therefore sound levels). Most importantly, it’s a direct measure of pressure rather than the logarithmic decibel scale. Since the microphone datasheet goes to the trouble of pointing out their sensitivity is specified against Pascals, they’re saying that the microphone output will scale with Pascals (more or less) linearly. This also means it will NOT scale linearly with decibels, in which a 6 dB change relates to a doubling or halving of the total output.

The first instinct is to start converting everything to the same units…convert your desired sound level in dB SPL to Pascals, calculate a conversion factor from the microphone sensitivity spec, and so on. In fact, datasheets used to directly give the sensitivity in millivolts per Pascal. But we don’t need to do that! We don’t ever need to look at Pascals directly, because decibels are a ratio…you can easily compare two ratios, and only have to anti-log one number.

We do need a place to anchor our ratio, and that’s already done for us. Environmental sound levels are often measured in dB SPL, which is referenced to the lowest threshold of human hearing (this is defined, for the purposes of SPL, as 0 dB). If you measure this pressure, it’s about 20 uPa. Converting to decibels:

Our microphone spec says it’ll be -44 dB compared to 1 volt, with the 0 dB point set at an input of 1 Pascal (or 94 dB SPL). To get the output voltage for a given dB SPL, we can start by simply calculating dB offsets using the sensitivity figure. We’ll get back to 0 dB by subtracting 94 dB from our input dB SPL, and then add the sensitivity (negative in this case). Then we can convert the resulting decibel figure to a gain figure, and from there to a voltage:

Well…almost! Another small gotcha is that when volts are presented in decibels, they’re assumed to be volts RMS. So to get our actual peak voltage, we need to convert from Vrms to Vpeak. Here’s the final formula:

For convenience, we made a simple Javascript calculator. You can plug in the microphone sensitivity and target SPL, and get expected RMS and peak voltages at 1kHz.

Sensitivity: dB (0 dB = 1V/1Pa @ 1kHz)
Input SPL: dB SPL
VoltsRMS: V
VoltsPEAK: V

Expanding on the above calculations let us create an even more useful tool for estimating how the circuit will perform. Most engineering is done in spreadsheets, so here’s one of ours. It calculates the microphone peak voltage over a range of SPL measurements, applies a preamp gain (in this case selected based on resistors we had in stock), applies another gain from the MSGEQ7 datasheet, and then calculates the typical number that an ADC would read from the MSGEQ7. Using this spreadsheet, we were able to play with different gain values and see where the usable range of ADC counts would be. We can also type in the minimum and maximum microphone sensitivity ratings from the datasheet, and see how that affects the output.

If you’d like to play with the numbers, here’s a link to a live Google Sheet: Electret Mic + MSGEQ7 Calculator

Will this information completely remove the need for tinkering with a prototype? Doubtful! There are still a lot of variables; the sensitivity of the microphone can vary 2 dB in either direction, and so can the MSGEQ7’s gain. We’ve made some assumptions about what sound levels will work best, and the role that peak voltage plays in the final result (perhaps occasional clipping against the supply rails is more usable than missing some of the quieter parts of the incoming audio). However, we can definitely see that a preamp gain of 100 (a typical rule of thumb figure) is probably way too much and we should start at 10.

The physical implementation of the circuit will also reveal some real-world problems to solve. We’ll start with the simplest-possible circuit and work up from there as needed.

It’s time to build and test this!

Click to continue >> Audio Sensor Development Part 2: EDA, PCB, SPL, PSRR

Submitted by Garrett on Thu, 12/10/2015 - 17:03.