Go Down

Topic: Trying to print a value to serial port for a vu meter using arduino uno (Read 1 time) previous topic - next topic

xcrunner

I constructed a simple vu meter circuit using a mic, 8 LED's, and an arduino uno. Mic output is connected to A0 and the LED's are connected to digital pins 2-9. From my understanding if it prints to the serial port I should see values from 0-1024 depending on what the output of the mic is and that should be from 0-5 volts. When I run this I get values from -1 to about 40 depending on the volume of the sound.  How can I change the code to get it to print values I can translate to voltage?
Any help would be greatly appreciated. Thanks.
Here is the code so far.
Code: [Select]

#define NUMREADINGS 5 // Number of readings (increase for a slow curve)
#define MINSOUND 25   // Min sound intensity
#define MAXSOUND 125  // Max sound intensity

const int firstLED = 2;
const int lastLED = 9;
int leds;
int readings[NUMREADINGS];
int index = 0;
int total = 0;
int average = 0;
int sound = 0;

int x = 0;
int y = 0;

void setup() {
    Serial.begin(9600);
  for (int r = 0; r < NUMREADINGS; r++) {
        readings[r] = 0;
    }
    for (int p = firstLED; p <= lastLED; p++) {
        pinMode(p, OUTPUT);
    }
    leds = (lastLED - firstLED) + 1;
}

void loop() {
    total -= readings[index];
    readings[index] = analogRead(0);
    total += readings[index];
    index++;

    if (index >= NUMREADINGS) {
       index = 0;
    }

    average = abs((total / NUMREADINGS) - 338); // ((1024 * 300) / 1000) = ~338
    sound = map(average, MINSOUND, MAXSOUND, 0, leds);
    for (int ledON = firstLED; ledON < (firstLED + sound); ledON++) {
        digitalWrite(ledON, HIGH);
    }

    for (int ledOFF = (firstLED + sound); ledOFF <= lastLED; ledOFF++) {
        digitalWrite(ledOFF, LOW);
    }

    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    Serial.println(sound);
}

Grumpy_Mike

#1
May 04, 2012, 01:24 am Last Edit: May 04, 2012, 01:25 am by Grumpy_Mike Reason: 1
Quote
From my understanding if it prints to the serial port I should see values from 0-1024 depending on what the output of the mic is and that should be from 0-5 volts.

Yes so that means you are not getting 5V on the analogue input.

Forget about the fancy averaging, get the raw output and print it and see if it the full range. Taking an average of an audio waveform should give you a value of 512. So it suggests your audio signal is not biased correctly on the arduino input. Can you post a schematic of what you have?

Averaging an audio waveform will not give you an vu reading because the positive peaks of the audio will cancel the negative ones.

xcrunner

I attached the schematic I drew with word.  The LED's go connected to pins 2-9 and then common ground on arduino, the mic is a ADMP401 MEMS microphone and has a ground that goes connected to the ground on the arduino, the vcc goes to the 3.3V pin on the arduino, and the AUD is connected to A0 on the arduino board.  The circuit is very simple its getting the code to do what I want that is complicated. I borrowed most of the code from someone elses project.
Thanks a lot.

Do you have any idea of what the microphone output is? and at what SPL? What range of SPL's do you expect to measure and are they linear or logarithmic ? Sound Pressure Level would in this case refer to a 'standard transducer' (speaker, defined carefully) placed at a specific point in front of the test microphone, driven at a specific level... the point is that there are too many variables in your question to give any kind of one size fits all answer. Your A/D measurement... What are you trying to measure and is it within the frequency response of the A/D input... too slow would return a varying rate and at various multiples of the sampling rate various things can become distorted where the first one woulds be a sample that had a component that was an even multiple of the sampling rate or very close to one.
The project must start with a clear objective and from that generate an accurate set of specifications, from there (a good definition) the sketch can be outlined, modules defined and tested them the complete sketch assembled and tested.

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

I did not mention that the response of a VU (Volume Unit) meter is ALWAYS logarithmic and that there is one more characteristic called the damping factor, it refers to the speed at which it responds, peak or average.

Doc
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Grumpy_Mike

#5
May 04, 2012, 09:52 am Last Edit: May 04, 2012, 12:34 pm by Grumpy_Mike Reason: 1
Quote
The circuit is very simple

True .......... and is is very wrong and could damage your arduino.
You are exposing the analogue input to a negative voltages that does the damage.
It needs a capacitor to couple the audio signal in and then a potential divider to bias the analogue input to 2.5V.
Like the front end in this circuit.
http://interface.khm.de/index.php/labor/experimente/arduino-realtime-audio-processing/
You also need an amplifier to get the signal from the microphone into the range.

Then your software needs to track the input and record the maximum value it sees, that maximum value is a measure of the volume.

Alternatively you need an envelope follower on the front end of your analogue input.

You can light more LEDs with a shorter code. And also, here you can see how to view the value with the serial monitor. Try it!
http://telecoreference.wordpress.com

Go Up