Tuesday, May 21, 2013

AVR Atmega audio input root mean square (RMS)

audiogetavarage is a simple library you can use to interface with an audio input.

It reads data from an ADC pin and returns the RMS value of the input simply using rms avarage.
The root mean square (abbreviated RMS or rms), also known as the quadratic mean, is a statistical measure of the magnitude of a varying quantity.
Here is simply implemented by getting samples, for each of them making the root, then the mean for all the collected samples, and finally square the result.

Atmega ADC can detect signal between 0 to 5v, when setup to 10bit, ADC has and accurancy of 1024 points.
The audio source is an AC input, when biased at is middle it has also negative voltage, thing about a sine wave with amplitude between 1 and -1.
The input should be biased such as the signal has it's middle between the ADC max and mix, more or less at 2.5v, with a amplitude of 2.5v (max 5v, min 0v).
The most simple thing you can do to bias the input is putting a resistor in series with a DC decoupling capacitor in series with two bias resistor, one pull up, and one pull down. If you have a scope, you can check what's the consequence of components above to the signal.

The above schematics (rev 02), provided as sample, is a simple microphone preamplifier. It is based on WM-61A panasonic capsule and TLC27L2 opamp.
This circuit is a derivation from John Conover WM-61A preamp. The reference is the schematic-6 found here: Using the Panasonic WM61A as a Measurement Microphone (John Conover).
An alternative and schematics is provided (rev 01), it implements some hardware low and high pass filter you may need in noise power environment.

Software side, to force a sample timing a TIMER it is used, otherwise grabbed samples have equal but "not know" interval, actually depending on the atmega frequancy and compiler. The number of sample to grab can be modified.
This library also provide a "dynamic bias" function, this function tries to re-bias the signal in the middle ADC range (512, if 10bit ADC is used).
A smooth filter it is used to process the signal, the filter factor can be setup.

A processing script is provided to view the input grabbed and the calculated output.

Setup parameters are stored in file audioget.h

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 8MHz.


  • read risk disclaimer
  • excuse my bad english


  1. great i will tray...

    1. good, let me know ;)
      In the next weeks i will release EMA filtering audio RMS input and Radix-4 FFT audio input. And then an experimental SPL meter.