# Arduino Forum

## Using Arduino => Programming Questions => Topic started by: louisbourdon on Jan 04, 2014, 05:28 pm

Title: Convert voltage to dB
Post by: louisbourdon on Jan 04, 2014, 05:28 pm
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?
Title: Re: Convert voltage to dB
Post by: robtillaart on Jan 04, 2014, 07:25 pm
yep except that Arduino goes from 0..1023 :)
Title: Re: Convert voltage to dB
Post by: Magician on Jan 04, 2014, 08:04 pm
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/ (http://coolarduino.wordpress.com/2013/01/09/audio-vu-meter/)

Title: Re: Convert voltage to dB
Post by: louisbourdon on Jan 04, 2014, 11:57 pm
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);                     }`
Title: Re: Convert voltage to dB
Post by: Magician on Jan 05, 2014, 12:25 am
Try:
Code: [Select]
`double db =  20.0  * log10 (sensorValue  +1.);`
Title: Re: Convert voltage to dB
Post by: MarkT on Jan 05, 2014, 03:34 am

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/ (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.
Title: Re: Convert voltage to dB
Post by: Magician on Jan 05, 2014, 04:34 am
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
Title: Re: Convert voltage to dB
Post by: louisbourdon on Jan 19, 2014, 03:42 pm
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!
Title: Re: Convert voltage to dB
Post by: Magician on Jan 20, 2014, 02:01 am
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.
Title: Re: Convert voltage to dB
Post by: juanernesto40 on Jul 08, 2014, 06:29 pm

maybe can try with this:
double db = 20. * (log10 (soundSensor));
Title: Re: Convert voltage to dB
Post by: KeithRB on Jul 08, 2014, 06:35 pm
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.
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 03, 2014, 04:49 pm
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?
Title: Re: Convert voltage to dB
Post by: KeithRB on Oct 03, 2014, 04:53 pm
The *second* hit if you google "ln" tells you it is the natural logarithm. (i.e., base e, not base 10)
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 03, 2014, 05:23 pm
Sure thing, but how this is implemented in code? Cause this is not working...

double db = 16.801 * log (sensorValue/1023) + 9.872;
Title: Re: Convert voltage to dB
Post by: KeithRB on Oct 03, 2014, 06:08 pm
You probably need to cast sensor value to a float so that the division is done in floating point.
Title: Re: Convert voltage to dB
Post by: PaulS on Oct 03, 2014, 06:13 pm

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.
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 03, 2014, 06:14 pm
Thanks for the point, but I have tried it already.
Here is my code so far...

Code: [Select]
`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);                     }`
Title: Re: Convert voltage to dB
Post by: PaulS on Oct 03, 2014, 06:15 pm
Quote
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?
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 03, 2014, 06:27 pm
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...
Title: Re: Convert voltage to dB
Post by: robtillaart on Oct 03, 2014, 06:51 pm
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..
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 03, 2014, 07:48 pm
Quote
maybe the formula should be

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

Thanks for the suggestions, but not even the minus sign didn't work. The values I get are almost static, they barely change.
Im not sure if this formula could work, although on the website I found it says it is compatible with Arduino Uno.

:|
Title: Re: Convert voltage to dB
Post by: KeithRB on Oct 03, 2014, 07:50 pm
The formula is fine. Maybe you should print some of the sensor values and work things out by hand.

Title: Re: Convert voltage to dB
Post by: BulldogLowell on Oct 04, 2014, 02:08 am

Quote
maybe the formula should be

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

Thanks for the suggestions, but not even the minus sign didn't work. The values I get are almost static, they barely change.
Im not sure if this formula could work, although on the website I found it says it is compatible with Arduino Uno.

:|

formula produces values from just under 10dB to about 60dB...
Title: Re: Convert voltage to dB
Post by: louisbourdon on Oct 04, 2014, 03:59 am
Thank you all so much about your replies and effort to help me sort this problem out.

It was after all a hardware issue. I had to change 2 sound modules as they were not giving the expected results - I suspect a problem in their variable resistors.

Now I have some results that sort of simulate a dB scale. It is not accurate for sure, but closer to my needs.

Cheers!
Title: Convert voltage to dB
Post by: esalagran on Jun 12, 2015, 05:29 pm
Hi,

I'm using this program and the sound sensor doesn't read correctly. It only detects three values of decibelius.

double db = (20. * log(10)) * (sensorValue / 5.); I use this formula but it doesn't work.

What's the problem?

Thanks
Title: Re: Convert voltage to dB
Post by: vaj4088 on Jun 13, 2015, 01:22 am

The log() function provides the natural logarithm, not base 10.
Thus, log(10) is the natural logarithm of 10, which is a constant.
The log10() function provides logarithms to the base 10.

Perhaps you meant

Code: [Select]
`double db = 20.0 * log10(sensorValue / 5.0) ;`

But of course I know nothing about your sensor so I can only guess.
I like to put a zero after the decimal point just because it makes it more obvious that a decimal point is there.

Title: Re: Convert voltage to dB
Post by: PaulS on Jun 15, 2015, 09:21 pm
Quote
What's the problem?
The problem is that you have unrealistic expectations. You post a snippet of code, using mixed mode arithmetic, and you want us to guess what the problem is.
Title: Re: Convert voltage to dB
Post by: knut_ny on Jun 16, 2015, 03:00 pm
Use op-amps to rectify and then envelope your AC signal.
This shall result in a DV voltage 0..5V for ADC.
Title: Re: Convert voltage to dB
Post by: esalagran on Aug 24, 2015, 11:33 am
I'm using a sound detection circuit http://letsmakerobots.com/files/sound_circuit.jpg. This circuit send a signal to my Arduino Leonardo. It convert Voltage to dB and if the number is over 68dB, it will open a light. I'm using thi program and it has a mistake but I don't know what it is.

int sensorPin = A0;  // analogic output
int relayPin = 10;   //digital output
int sensorValue = 0;
int db=0
void setup() {

pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
Serial.begin(9600);
}

void loop() {

double db = (20. * log(10)) * (sensorValue / 5.);

Serial.print("db = ");
Serial.println(db);

delay(500);
if db > 68 {digitalWrite (relayPin, HIGH};
delay (5000);
}

void loop (){
double db = (20. * log(10) * (sensor value / 5.);
Serial.print ("db =");
Serial.println(db);
delay (500);
if db < 55 {digitalWrite (relayPin, LOW};
delay (2000) ;
}

thanks
Title: Re: Convert voltage to dB
Post by: wahyu21 on Nov 03, 2015, 05:15 pm
help me for make decibel meter with arduino,i dont know how to convert ADC to dB in arduino.....i thought logarithm doesn't exsist in arduino...help please. :smiley-confuse:
Title: Re: Convert voltage to dB
Post by: aarg on Nov 03, 2015, 05:20 pm
Title: Re: Convert voltage to dB
Post by: wahyu21 on Nov 03, 2015, 05:30 pm
can you help me for creating the completly program of this...

this my proggram

#include <LiquidCrystal.h>             // 0

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // 1
int val;                               // 2
int dB;                                // 3
const int sensorPin = A0;              // 4

char in;                               // 5
int sensorVal = 0;                   // 6
int smoothedVal = 200;               // 7
int samples = 4;                     // 8
void setup() {                         // 9

pinMode(6, OUTPUT);                  // 10
pinMode(7, OUTPUT);                  // 11
pinMode(8, OUTPUT);                  // 12
pinMode(9, OUTPUT);                  // 13

lcd.begin(16, 2);                    // 14
lcd.setCursor(1,0);                  // 15
lcd.print("DESIBEL METER");          // 16
lcd.setCursor(5,1);                  // 17
lcd.print("W-Tech");                 // 18
delay(300);                          // 29
lcd.clear();                         // 20
// Print a message to the LCD.
Serial.begin(9600);                  // 21

}

void loop() {                          // 22
//int val;
lcd.clear();                         // 23

smoothedVal = smoothedVal + ((val - smoothedVal)/samples); //25
val = smoothedVal*0.6779+32.755;      // 26
dB = val*0.5228 + 35.271;             // 27
Serial.println(dB);                  // 28

lcd.setCursor(0,0);                  // 30
lcd.print(" DESIBEL METER");         // 31
lcd.setCursor(0,1);                  // 32
lcd.print("press red button");}      // 33
else{                                // 34

lcd.setCursor(1,0);                  // 35
lcd.print("DESIBEL METER");          // 36
lcd.setCursor(0,1);                  // 37
lcd.print(dB+2);                       // 38
lcd.setCursor(5,1);                  // 39
lcd.print("dB");                     // 40
lcd.setCursor(8,1);                  // 41
lcd.print("W-Tech");}                // 42

delay(250);                          // 43

if (val>=45){ digitalWrite(6,1);}   // 44
else {digitalWrite(6,0);}           // 45
if (val>=55) {digitalWrite(7,1);}   // 46
else  {digitalWrite(7,0);}         // 47
if (val>=60) {digitalWrite(8,1);}  // 48
else {digitalWrite(8,0);}          // 49
if (val>=70) {digitalWrite(9,1);}  // 50
else {digitalWrite(9,0);}          // 51

}

i use linearity to convert my ADC......
help me for changes to 20log10....26 and 27 is my conversion of my ADC
Title: Re: Convert voltage to dB
Post by: aarg on Nov 03, 2015, 05:31 pm
Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags which make the code look
Code: [Select]
`like this`when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.
Title: Re: Convert voltage to dB
Post by: wahyu21 on Nov 03, 2015, 05:48 pm
i don't know what to do
Title: Re: Convert voltage to dB
Post by: PaulMurrayCbr on Nov 04, 2015, 02:32 am
Quote
(i.e. 0 would then equal approximately 20-30 dB).
Ok, that's weird. I would have thought that a reading of 0 comes to negative infinity db.

To make your calculation, we need two points on your scale - assuming that voltage corresponds linearly to SPL. And we can't use point 0 (no sound at all) because decibels are logarithmic. We'll do everything using floating point.

As always, I suffix everything with the unit of measure.

Code: [Select]
`// When the analog in is 100, then the reading should be 40dbconst double V0 = 100;const double V0_dB = 40;// When the analog in is 1020, then the reading should be 80dbconst double V1 = 1020;const double V1_dB = 80;// pre-calculate the scaling constants// convert units to bels rather than decibelsconst double inV0_Bel = log10(V0);const double inV1_Bel = log10(V1);const double outV0_Bel = V0_dB / 10;const double outV1_Bel = V1_dB / 10;const double scalingConstant = (outV1_Bel-outV0_Bel) / (inV1_Bel-inV0_Bel);float analog2db(int V) {  double inV_Bel = log10(V);    // scale to correct range  double outV_Bel = (inV_Bel-inV0_Bel) * scalingConstant + outV0_Bel;    return outV_Bel * 10; // deci-bels}`

Title: Re: Convert voltage to dB
Post by: wahyu21 on Nov 05, 2015, 11:14 am
I still don't get it...can you create the program from begining until the end?i really need this...i still dont get it how to change the analog voltage to decibel....
Title: Re: Convert voltage to dB
Post by: MiFi on Nov 09, 2015, 12:51 am
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!
And you are aware that  60 dB represents a dynamic range of one to one million?
You shoud not even try doing that in software.

You need analog hardware upfront with a logarithmic amplifier.
Title: Re: Convert voltage to dB
Post by: MiFi on Nov 09, 2015, 01:12 am
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?
If you want to to "make it work as closely as possible to as a dB meter", you have got a LOT to do!

You will need extreme low noise amplifiers, an (A) curve or a (C) curve filter, decoupling amplifiers again and finally a logarithmic demodulating amplifier. Not to mention a reliable microphone that has at least 60dB dynamic...
Good luck!

The phidget sound sensor does not appear to match these requirements.
Halas, since I am still seeking for a shield that does exactly that.

Title: Re: Convert voltage to dB
Post by: Anggasiregar on Nov 06, 2017, 09:47 am