Sunday, May 15, 2011

Reading Sound Level


I have included a very simple circuit below along with the required code to read in line level audio. No special considerations were given to the sampling rate or anti-aliasing filters; these things are not required for this project as was found experimentally.
For this circuit a simple voltage divider is formed with a blocking capacitor (using something other than 100uF is fine - feel free to experiment) to maintain that DC bias at analog pin A0. This is to ensure that the negative portion of the line level audio does not clip. A DC offset in code is used to compensate for any differences in the voltage divider resistor values due to inherent tolerances, and the signal is then mapped to the -256:256 range (convenient for the algorithm, as you will see). Values are sent to the serial monitor to allow you to make sure that things are working.

Regarding the 3.5mm jacks, the diagram shows only one. Typically there is one pin that is GND, and two other pins that are the left and right channels of stereo audio. For our purposes you can simply tie the left and right channel together. For a line out signal, connect the corresponding pins of the second 3.5mm jack to the first 3.5mm jack. It's just that simple!

Note that you will need to ensure that the volume is high enough at the input to get some real values. Don't expect to see any clipping. Experiment! Every input source will be a little bit different.

//Simple read of line-level audio, write to serial monitor
//Uses simple voltage divider input circuit with blocking capacitor
//Basic approach doesn't consider using anti-aliasing filter

//or sampling rate

const int audioPin = A0;
int x=0;
int sensorValue = 0;
int offset = 0; //choose offset based on the reading you get
//in the serial monitor when there is no input signal
//this value may be nonzero if resistors aren't exactly
//equivalent in voltage divider circuit; simpler software

//hack than to use a potentiometer

void setup() {

Serial.begin(9600);

}

void loop() {

sensorValue = analogRead(audioPin);
x = (sensorValue - 512)/2; //maps 0:1024 to -256:256
x = x - offset; //subtract offset so we see zero for no input

Serial.println(x);
delay(1);

}

No comments:

Post a Comment