Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

new to Ardunio but trying to compile

Status
Not open for further replies.
here is the db meter code that looks promising. connected leds and after my wife wakes up I can turn on some movie or music to see how well it really works.
Code:
const int MIC = 0; //the microphone amplifier output is connected to pin A0
int adc;
int dB, PdB; //the variable that will hold the value read from the microphone each time
int greenLed=2;
int yellowLed=3;
int redLed=4;
void setup() {
Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the microphone is obtaining on the Serial Monitor
  pinMode(3, OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(2,OUTPUT);
}

void loop(){

  PdB = dB; //Store the previous of dB here
 
adc= analogRead(MIC); //Read the ADC value from amplifer
//Serial.println (adc);//Print ADC for initial calculation
dB = (adc+83.2073) / 11.003; //Convert ADC value to dB using Regression values

if (PdB!=dB)
Serial.println (dB);

if (dB>60)
{
  digitalWrite(4, HIGH);   // turn the REDLED on (HIGH is the voltage level)
  delay(2000);                       // wait for a second
  digitalWrite(4, LOW);
}
if (dB>40)
{
  digitalWrite(3,HIGH);
  delay(2000);
  digitalWrite(3,LOW);
}
if (dB>20)
{
  digitalWrite(2,HIGH);
  delay(2000);
  digitalWrite(2,LOW);
}
}
 
I looked at the decibel calculation in this program and it didn't make sense to me. Decibel is logarithmic function, and what's in the program is a very poor approximation. The red line show the dB approximation from the program. The blue line shows the real dB curve. It's not a straight line as it's a log function. Based on the program calculations, you'd get extreme changes at loud levels and little change at low levels.

dB - real vs program.jpg


The graph below shows the true dB curve along with perceived loudness. A change in sound pressure level of 10dB is perceived as a doubling or halving of the loudness. If the level increases by 10dB, it sounds twice as loud. If the level falls by 10dB, it sounds half as loud. A change in level of 3dB is about the minimum that can be heard.

dB.jpg


A 10-bit ADC provides a total range of 60 dB. But look at the left end of the plot. A small change in ADC count results in a large change in dB level. The useful range is more like 40 - 50 dB. To get this range however, the microphone level must be matched to the full scale range of the ADC. You want the loudest (practical) level to equal the full scale range of the ADC. From that point, the lowest sound that can accurately be measured is about 2 x 2 x 2 = 8 times less. So you'll probably need an amplifier stage with adjustable gain to set the mic level to match the ADC.

Decibel Calculation

dB = 20 log (V/Vref) where log is log base 10 and Vref is the reference value for the dB. In this case, it doesn't matter since the numbers aren't calibrated to anything. Making Vref = 0.1 will keep all the values positive to make it easier to handle.

Note that decibels are a relative number, equal to a multiple. A 10dB change is a 10dB change whether it's from 10dB to 20dB or from 100dB to 110dB. In this case, the change in decibels is all that matters, since the dB levels aren't calibrated to any standard.

Yeah, I know this is wasted on MrDEB, but still I try.
 
true in that correlating ADC to a dB is not precise.
the mic has am amp.
One example I ran across is to implement a high band pass filter and a low pass filter.
basically just a vu meter would work but the dB issue gets in the way for a linier dB measurement.
 
Not precise? Ya think? 8x too high at high levels. 4x too low at low levels. You might say no relation to dB at all.

The vertical scale is in dB. A 10dB change is a doubling / halving of perceived level, so a 20dB change is 4x and a 40dB change 8x.
 
Ahhhh. No. This guy uses a VOM to measure "dB" at the quietest level and loudest level....and then linearly interpolates between the two. No. Decibels are a logarithmic scaling of data. Anything else will not work well for your application.
 
Must be some way to do this?
going to keep pluging away and maybe stumble on a method to get something to work?
 
Decibel Calculation

dB = 20 log (V/Vref) where log is log base 10 and Vref is the reference value for the dB. In this case, it doesn't matter since the numbers aren't calibrated to anything. Making Vref = 0.1 will keep all the values positive to make it easier to handle.

At the risk of being pedantic and offending Musicmanager *again*, I did explain how to calculate decibels properly. This isn't magic, or unknown. It's straight forward math and can easily be implemented on an Arduino.

From the Arduino documentation,

The log10() function returns the logarithm of argument __x to base 10.

dB = 20 * log10(ADC/0.1) <-- this equation is all that's needed to properly calculate decibels.

You must include the math.h file in the Arduino sketch, The 0.1 reference value is arbitrary, to get a nice range on the dB scale.

dB Computed.jpg
 
curious how did you offend musicmanager?
got to thinking about this project in the sense that I just need to compare the ongoing sound volume to a preset level that is preset by the user.. preset level for highest volume level is only one needed. need to consider low volume?
will try out the math suggestion. thanks
 
lets let the cat out of the bag
want to control the volume on the tv to a set desired level.
using the remote, train the Arduino using the remote, (volume up/down) then the user sets their desired volum level and the seven segment displays the level from 0-9 or 0 - ?
if a loud commercial comes on, the Arduino detects a louder than preset level then lowers the volume. If the pre set volume level is to low then the Arduino raises the volume (volume up) up to the pre set level.
The Arduino communicates via Infra red, same as using the tv remote by itself.


got to thinking about this project in the sense that I just need to compare the ongoing sound volume to a preset level that is preset by the user.. preset level for highest volume level is only one needed. need to consider low volume?

Maybe you need to think this through and settle on what you want.
 
Well this looks close but need to tweek the values.
I turned on a Netflix show and the dB values look about right but lots of negative numbers unless I speak into the mic ten the numbers go up but I turn the volume down and output goes lower etc. I made additions as you suggested.
Code:
[code]
#include <math.h>;
int num_Measure = 128 ; // Set the number of measurements   
int pinSignal = A0; // pin connected to pin O module sound sensor 
int redLed =5 ;
int blueLed=2;
int clearLed=3;
long Sound_signal;    // Store the value read Sound Sensor   
long sum = 0 ; // Store the total value of n measurements   
long level = 0 ; // Store the average value   
int soundlow = 40;
int soundmedium = 500;
 
void setup () 
{   
  pinMode (pinSignal, INPUT); // Set the signal pin as input   
  Serial.begin (9600);
//  lcd.begin(16,2);
} 
  
void loop () 
{ 
  // Performs 128 signal readings   
  for ( int i = 0 ; i <num_Measure; i ++) 
  {
Sound_signal=20*log10(analogRead(pinSignal));

    
 //  Sound_signal = analogRead (pinSignal); 
    sum =sum + Sound_signal; 
  } 
 
  level = sum / num_Measure; // Calculate the average value   
  Serial.println("Sound Level: ");
 // lcd.print("Sound Level= ");
  Serial.println (level-33); 
//  lcd.print(level-33);
  if(level-33<soundlow)
  {
  //  lcd.setCursor(0,2);
  //  lcd.print("Intensity= Low");
     digitalWrite(clearLed,HIGH);
     delay(500);
     digitalWrite(clearLed,LOW);
  }
  if(level-33>soundlow && level-33<soundmedium)
  {
  //  lcd.setCursor(0,2);
  //  lcd.print("Intensity=Medium");
     digitalWrite(blueLed,HIGH);
     delay(500);
digitalWrite(blueLed,LOW);
  }
  if(level-33>soundmedium)
  {
  //  lcd.setCursor(0,2);
  //  lcd.print("Intensity= High");   
    digitalWrite(redLed,HIGH);
  }
  sum = 0 ; // Reset the sum of the measurement values 
  delay(200);
 // lcd.clear();

}
[/CODE]
 
Did you add the line to calculate decibels?

Code:
{
Sound_signal=20*log10(analogRead(pinSignal));

    
 //  Sound_signal = analogRead (pinSignal); 
    sum =sum + Sound_signal; 
  }

That's a start, but you missed all the other stuff you need to change. As you've added that line (ignoring my suggestion of dividing by 0.1 to shift the range), your decibel readings will range from 0 to 60 dB.* No need for all the "level - 33" scaling lines after that. Also, the high/medium/low level settings need to be adjusted. 0 is the quietest level it will hear. 60 is the maximum level it will hear. Adjust the microphone level to read 58 - 60 loudest level you expect.

60 = loudest

50 = 1/2 the perceived volume of the maximum.

40 = 1/2 * 1/2 = 1/4 the perceived volume of the maximum.

30 = 1/2 * 1/2 * 1/2 = 1/8 the perceived volume of the maximum

20 = 1/2 * 1/2 * 1/2 * 1/2 = 1/16 the perceived volume of the maximum

10 = 1/2 * 1/2 * 1/2 * 1/2 * 1/2 = 1/32 the perceived volume of the maximum.

< 10 dB - probably out of gas....

You're making some progress.

* What is the dB level for an ADC reading of 1000? Punch it out on a calculator.

What is the dB level for an ADC reading of 100? Punch it out on a calculator.

What is the difference in dB levels for the above two calculations?



What is the dB level for an ADC reading of 1000 if you divide it by 0.1 as I suggested? Punch it out on a calculator.

What is the dB level for an ADC reading of 100 if you divide it by 0.1 as I suggested? Punch it out on a calculator.

What is the difference in dB levels for these two calculations?

Go ahead. I can wait until you have answers. You might learn something.
 
Also, it's not strictly proper to average dB levels since they represent ratios rather than linear numbers*. Probably best to average the ADC readings and then convert to dB. Where are the numbers being averaged in the code you posted. Figure it out and explain.

*People who should know better have done this in commercial software.

You can put more effort in YOUR project than I have.
 
I had the divide by 0.1 but inadvertently put in a different sketch.
As for punching it out, I perhaps the worst when it comes to math but will make changes as suggested.
 
My god Jon... You ooze empathy..

I guess this is another insult for today. I truly appreciate them

I dare say I have put more effort into explaining the whys and wherefores of decibels than MrDEB has put into this entire project. I have attempted to get him to think and understand how things work, rather than put superficial thought into things and abandoning the effort as soon as a misplaced comma upsets the works. Is suggesting he actually make some effort to understand some of the stuff he is copying and pasting wrong?

He actually managed to insert the line to correctly calculate decibels into his latest code, so there's a glimmer of hope. This seems to be something most Arduino programmers don't understand from the posted code for VU meters and "sound" meters.

After a dozen years of programming[/u], the question in this thread of "how do I find typos?" suggests not a whole lot has been learned over time.
 
My god Jon... You ooze empathy..
Empathy doesn't pay the bills, hard results do that. Back in the day, you learned, or starved. Welfare states encourage laziness..

There are givers and takers in this world.

Givers might be wise to learn some limits, because the takers generally do not have them...

A 'Thanks', and perhaps a nod to where a helper post made a difference would be nice to see, but these DEB threads kinda fly in the face of any help....
Help is right there on a plate, from some of our most-experienced members, just begging to be explored, but it gets waved aside like a mosquito or something equally irritating, instead favouring " going to keep plugging away and maybe stumble on a method to get something to work "
 
Thanks Mickster.

As you say, sometimes (most of the time)
merely leading MrDEB to the knowledge isn't enough; a more active approach can be helpful and even that may be futile.
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top