VU Meter Question

Hey guys, I have made what I thought was a workable VU Meter but it doesn't work. The LED and the Mic are connected into the right pin, but it still doesn't work. I've attached the files and the program. Please help! Also it's on an Arduino UNO

int ledPin = 13;
int leftChannel = 0;
int left, i;

void setup()
{

  pinMode(ledPin, OUTPUT);
  pinMode(leftChannel, INPUT);

}

void loop()
{
left = analogRead(leftChannel); 
left = left / 50;   

  if (left == 0)  
     {
     digitalWrite(ledPin, LOW);
  
  }
  
  else
  {
   for (i = 0; i < left; i++)
    {
     digitalWrite(ledPin, HIGH);
    }
   for(i = i; i < 10; i++) 
     {
      digitalWrite(ledPin, LOW);
     }
  }
}

it still doesn't work.

What does it do ?
What should it do ?

Why turn on the LED several times then turn it off 10 times ? Once it's on it's on and once it's off it's off. In any case those for loops are going to execute in microseconds so you will see little or no LED illumination.

UKHeliBob:
What does it do ?
What should it do ?

I want it to light up when the mic senses sound. What it is doing is nothing.

When the input is not zero turn on the LED (just once), delay() for 200ms then turn it off. This is very crude and will not work as a VU meter but will prove that you can detect sound. Then you can set about refining the program to react differently to louder sounds than quieter ones.

UKHeliBob:
This is very crude and will not work as a VU meter but will prove that you can detect sound.

What do you mean? I want it to work as a VU Meter

Take small steps.
First prove that you can detect sound then improve the program.

How will it act as a VU meter ?
What will set the threshold at which the LED will light ?
Could you use several LEDs and light more for louder sounds ?
Should the output have a linear relationship to the input or is it more complicated than that ?

Nothing is possible until you can detect sound.

UKHeliBob:
First prove that you can detect sound then improve the program.

Could you tell me how to do this? I just want one led to light when the mic detects sound. I just need to know how to do this.

Note: VU meter is a vary particular type of sound meter with a specified frequency characteristic, as well as specified attack and decay characteristics.

You are taking a single measurement of an AC waveform at some random time. That gives you no information about the actual signal, other than that it is there.

mattmanmar:

UKHeliBob:
First prove that you can detect sound then improve the program.

Could you tell me how to do this? I just want one led to light when the mic detects sound. I just need to know how to do this.

Sensing of just sound or no sound could best be done by first rectifying and filtering the audio signal before wiring to the analog input pin. Then in your sketch you will be reading a average audio power level and you can select/define the minimum the threshold value returned by the analogRead() to determine if it's a loud enough sound or not.

Lefty

You seem to have an electret microphone going directly to
an analog input?

That won't work as microphones put out a few millivolts, whereas the ADC
minimum step is 5mV. You need a microphone+preamp module to interface
to an Arduino.

Actually I'm not totally certain about this as the image has way too many pixels
to fit onto my screen - please post images at a sensible resolution, its really not
helpful to post 12 megapixel images.

Don't take this as an insult, but you're probably in over your head. You need to scale the project goal way back and get through the basics first.

I'm going to tear your project to bits here. Don't be offended, you just need to know what you're doing wrong. OK? Here goes:

  1. Your pictures are WAY too big. Before posting, resize them to no more than 1000 pixels wide. Viewing them in the forum requires a ton of scrolling just to follow your wires.

  2. You didn't put a current-limiting resistor between the output pin and your LED. Consequently, you may have burned up your LED, or your output pin, or both. If they're both OK, they may not be for long. Stick a 220R to 1K resistor in between.

  3. You didn't post a schematic or a datasheet for the mic, but from what I can see, you're using an electret mic capsule connected to an analog input with no pull-up resistor. The mic is not powered and will not produce a usable signal this way.

  4. You're reading the analog value and dividing by 50. Why?

  5. You have a conditional that checks if the analog value is less than 0. In the real world, your chances of having an analog reading stay at one value is very small due to noise and natural fluctuations. You need to be checking for a small range, or within a threshold (e.g., <10). Tune as needed.

  6. Checking for 0 = silence is not correct for an audio signal anyway. Audio is AC, and as such, needs to swing above and below a reference voltage. In typical single-rail (one voltage) audio circuits, that means you have to create a 1/2 * Vcc rail (e.g., 2.5v) so your signal can swing between 0 and 5, with 2.5 being the middle -- i.e., silence. This is tricky though, because you also have to know what analog value corresponds to no signal, and then get the difference between this "zero value" (ideally 511, which is 1023/2) and the reading from your mic. Then, you take the absolute value of the result to get your signal level. If the electret mic is the type that has a Gnd and Vcc/Out pin driven by a resistor, then it has an internal divider to allow AC swing around a set point, however, you won't have a good way to read that reference point to calculate the audio signal offset, so you might need to run it through a DC-blocking cap, and provide your own Vcc/2 rail for comparison.

#5 is the part where most electronics newbies throw in the towel, but there are a lot of guides out there with basic circuits to do what you want to do. If you don't understand how it works, just copy one that already does. :slight_smile:

  1. I'm not really sure what you were hoping to accomplish with this:
   for (i = 0; i < left; i++)
    {
     digitalWrite(ledPin, HIGH);
    }

... I assume it's an attempt to vary the LED's brightness based on signal level. That isn't what it does though. It just turns the LED on (value of left) times. This does nothing different than just turning it on once, aside from taking a tiny amount more time to complete the loop. If you were hoping to vary the brightness, you'll want to look into PWM instead.

  1. Same goes for this part:
   for(i = i; i < 10; i++) 
     {
      digitalWrite(ledPin, LOW);
     }

... except you've arbitrarily chosen a value of 10 to stop for some reason. The first time through the loop, the LED turns off. And it doesn't turn any offer the next however many times it runs. Since this loop exists right after the on loop, you're not likely to see the LED ever light up at all. Depending on how fast the entire loop() function runs, you may get a very dim light that kinda varies with the signal level, but it probably won't work nearly as well as you're hoping for.

So, definitely start with something simple. Find and copy a known-good schematic. Then just turn the LED on and delay(100) or something before turning it off. Once you get that working, well... take that bridge when you come to it.

SirNickity:
3) You're reading the analog value and dividing by 50. Why?
If you don't understand how it works, just copy one that already does. :slight_smile:

I got this one off the internet, I only modified it a little. Do you have any you could suggest?

This one came up pretty close to the top in my search results:

The question itself doesn't apply to you (although I'm only guessing you're using an electret capsule -- probably a safe bet though), but the schematic looks like a good start. Read some of the other posts on that page, they're helpful too. Particularly the post below the question, where someone shows how to create a Vcc/2 divider. You'll want to do that. Lots of good info in that one.

Handling audio is kinda tough relative to most beginner projects. If you're determined to figure this out, you've got a lot of concepts to wrap your head around. Be patient, read, and ask questions. You'll get there.