Go Down

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

#### PaulS

#15
##### 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.

#### louisbourdon

#16
##### 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);                     }`

#### PaulS

#17
##### 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?

#### louisbourdon

#18
##### 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...

#### robtillaart

#19
##### 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..
Rob Tillaart

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

#### louisbourdon

#20
##### 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.

:|

#### KeithRB

#21
##### 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.

#### BulldogLowell

#22
##### 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...

#### louisbourdon

#23
##### 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!

#### esalagran

#24
##### 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

#### vaj4088

#25
##### 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.

#### PaulS

#26
##### 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.

#### knut_ny

#27
##### 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.
Ny

#### esalagran

#28
##### 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

#### wahyu21

#29
##### 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.

Go Up