Go Down

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

PaulS


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

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

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

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

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

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

The formula is fine. Maybe you should print some of the sensor values and work things out by hand.


BulldogLowell


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

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

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

Your formula is incorrect.

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

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

Use op-amps to rectify and then envelope your AC signal.
This shall result in a DV voltage 0..5V for ADC.
At last 20*log10(analogread(A?))
Ny

esalagran

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() {

  sensorValue = analogRead(sensorPin);   

const int analogInPin = A0;

  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 (){
  sensorValue =analogRead (sensorPin);
  const int analogInPin=A0 :
  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

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:

Go Up