Convert voltage to dB

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?

yep except that Arduino goes from 0..1023 :slight_smile:

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/

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

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);                     
}

Try:

double db =  20.0  * log10 (sensorValue  +1.);

Magician:

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.

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

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!

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.

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

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.

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 :roll_eyes:

Maybe someone could help?

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

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

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

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

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

Or divide by 1023.0 for the same effect.

Thanks for the point, but I have tried it already.
Here is my code so far...

const int analogInPin = A0;
double sensorValue = 0.;

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

void loop() {
  sensorValue = analogRead(analogInPin);            
  double db = 16.801 * log(sensorValue/1023.) + 9.872;
  Serial.println(db);
  delay(500);                     
}

Here is my code so far...

Which does something. What, pray tell, does it do?

Which you expect to do something. What, pray tell, do you expect it to do?

I thought it would convert from a voltage range of values to a logarithmic range of decibel values from 50 to 100.
I get negative values (i.e. -13.7) in silence, and inf values with noise...

sensorValue/1023. <= 1.0

==>
log(sensorValue/1023.) <= 0.0

==>
16.801 * log(sensorValue/1023.) <= 0.0

==>
16.801 * log(sensorValue/1023.) + 9.872 <= 9.872

so yes -13.xxx is a reasonable value

maybe the formula should be

dB = -16.801 * log(sensorValue/1023.) + 9.872; // note the additional minus..