Go Down

Topic: Convert voltage to dB (Read 33194 times) previous topic - next topic

louisbourdon

Hi
I'm looking at this link (http://www.sengpielaudio.com/calculator-gainloss.htm) to get the formula for converting voltage to dB. I use a sound sensor connected to Arduino (0-1024) and I want to convert this to acoustic energy (dB) (for display, etc).

As I understand this, 1024 values (or 0 to 5V) would equal to 60 decibel units, so if I use the formula -> (20*log10)*V/V0, I will get from 0 to 60 - and then I can offset by an amount if I want to match proper settings (i.e. 0 would then equal approximately 20-30 dB).

Does this sound alright?

robtillaart

yep except that Arduino goes from 0..1023 :)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Magician

Quote
As I understand this, 1024 values (or 0 to 5V) would equal to 60 decibel units, so if I use the formula -> (20*log10)*V/V0, I will get from 0 to 60
No. Voltage is RMS value in the formula, your range 0-1023 is peak-to-peak. You have to divide by 2 in the first, and than by sqrt(2). 1024 / 2 * sqrt(2) = 362.  Roughly, you can get about 53-54 dB at the best - perfectly centered steady sine wave.  Less than 40 dB for normal audio content.
  Options: use ADC with more bits, or averaging.
http://coolarduino.wordpress.com/2013/01/09/audio-vu-meter/
 

louisbourdon

I try this code, but it doesn't give correct results. Someone can help to fix the formula?

Code: [Select]
const int analogInPin = A0;

int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);           

  Serial.print("sensor = " );                       
  Serial.println(sensorValue);     

  double db = (20. * log(10)) * (sensorValue / 5.);
 
  Serial.print("db = ");
  Serial.println(db);
   
  delay(500);                     
}

Magician

Try:
Code: [Select]
double db =  20.0  * log10 (sensorValue  +1.);

MarkT


Quote
As I understand this, 1024 values (or 0 to 5V) would equal to 60 decibel units, so if I use the formula -> (20*log10)*V/V0, I will get from 0 to 60
No. Voltage is RMS value in the formula, your range 0-1023 is peak-to-peak. You have to divide by 2 in the first, and than by sqrt(2). 1024 / 2 * sqrt(2) = 362.  Roughly, you can get about 53-54 dB at the best - perfectly centered steady sine wave.  Less than 40 dB for normal audio content.
 Options: use ADC with more bits, or averaging.
http://coolarduino.wordpress.com/2013/01/09/audio-vu-meter/
 


But the smallest signal you can see is 1 count peak-to-peak, not 1 count RMS,
so its 1023 times smaller than the max, ie 60.2dB less.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Magician

Quote
1 count peak-to-peak, not 1 count RMS,
so its 1023 times smaller than the max, ie 60.2dB less.
I never heard someone would measure DC voltage in dB. For AC there is an offset,  and full span is only 512 , it gets to 6.02 * 9 + 1.76 = 55.94
I agree, that 512 is higher than 362, which I counted in integer math term, and basically isn't always the case

louisbourdon

So, is there any formula that works to convert the voltage in dB SPL? I have tried many different ways, but I always get false results. There has to be something out there - or if could someone direct me to resources?

Again, I want to convert the voltage in db SPL (sound pressure level), with a minimum value of around 30dB, and a maximum at around 90dB.
Thanks!

Magician

Quote
So, is there any formula that works to convert the voltage in dB SPL?
No, otherwise it has to include mic sensitivity , amplification coef. and arduino reference voltage.  You have to calibrate with a standard device, in special soundproof chamber.

juanernesto40


maybe can try with this:
double db = 20. * (log10 (soundSensor));

KeithRB

Is your sensor just a microphone? If so, you cannot just convert any single value to dB, you need to know the amplitude of your signal, and possibly filter it for A-weighting if this is for an SPL meter.

louisbourdon

After some time I returned to finish this project. So far, still, no answer to make it work as closely as possible to as a dB meter.

I'm reading this article (http://www.inmotion.pt/store/phidgets-sound-sensor) saying that there is a formula that can be used to get the dB conversion (for 1kHz sound for example).

SPL at 1kHz tone (dB) = 16.801 x ln(sensorValue/1023) + 9.872


I tried the formula but I got nothing. I don't know what the ln stands for  :smiley-roll:

Maybe someone could help?

KeithRB

The *second* hit if you google "ln" tells you it is the natural logarithm. (i.e., base e, not base 10)

louisbourdon

Sure thing, but how this is implemented in code? Cause this is not working...

double db = 16.801 * log (sensorValue/1023) + 9.872;

KeithRB

You probably need to cast sensor value to a float so that the division is done in floating point.

Go Up