Go Down

Topic: Problem regarding my arduino dB meter using a sparkfun electret microphone. (Read 331 times) previous topic - next topic

orange1785

Hello, I'm trying to build a dB meter as a personal project using a sparkfun electret microphone and a arduino uno. (https://www.sparkfun.com/products/12758).

I've plugged the GND pin directly into the arduino's GND, the VCC pin directly to the arduino's 3.3v and the AUD pin directly to the arduino's A3.

here is my code :

int ledpin = 13;
int soundpin = A3;

void setup() {
 Serial.begin(9600);
pinMode(ledpin, OUTPUT); // Initialize pin 13 as an output
pinMode(soundpin, INPUT);
}

void loop() {

  int adc = analogRead(soundpin);
  double dB = (adc - 223.4) / 5.930; (linear regression using my samples from LoggerPro);
  Serial.print(adc);
  Serial.print("\n");

  }

Here are my samples :

dB  |  adc

12   336
15   338
20   339
22   392
32   349
42   452
44   411
45   471
47   450
49   474
57   647
60   660

Currently I do not have resistor or capacitors. The output my programs gives is stable during silence but whenever I add sound it stays the same value (namely around 17-18 dB). However, when I approach my phone, from which I play music for testing, to the microphone, The outputs become very erratic and sometimes give a series of zeros. This is my first project I've done and it would be appreciated if anyone could give their input. Thank you.

 





Grumpy_Mike

Quote
I've plugged the GND pin directly into the arduino's GND, the VCC pin directly to the arduino's 3.3v and the AUD pin directly to the arduino's A3.
This is wrong.

Quote
Currently I do not have resistor or capacitors.
That is your problem.

You need an amplifier to boost your signal. You need to make an envelope follower to get just the peaks of your audio signal.

https://en.m.wikipedia.org/wiki/Envelope_detector

orange1785

Pardon, do you mind going into more detail of why I cannot connect it directly to the arduino and why I must use resistors and capacitors. I'm just starting to use arduinos and I want to learn more on how to use them.

Thank you

Grumpy_Mike

That is like asking for more details about exactly why you can't use orange juice to run your car.

I told you what you need to do.

It is nothing to do with the Arduino it is to do what you want to do. You seem not to understand the basics of an audio waveform. In order to detect the loudness of a sound you need to measure the peak amplitude.  Not the waveform, and your setup will not even do that.

In order to measure the waveform you need to bias the input to 2.5V with two resistors and a capacitor because the waveform goes positive and negitave and an Arduino can only measure positive voltages.

But you need to measure the envelope peak. Did you not read that link? If you did an don't understand some aspects of it then ask specifically about those aspects.

It seems you ignored the how to use this forum sticky post which we hide at the top of every forum section. That will tell you how to ask a question and how to react to the answer you receive.

orange1785


DVDdoug

The SparkFun microphone board is supposed to be biased at half the supply voltage already.

With a 5V power supply silence should read about 512 (half of the 1023 range).    With a 3.3V power supply (input biased at 1.65V) you should be reading about 338.   Of course, 5V will give you greater range.

It's virtually impossible to get complete silence and the electronics may generate some noise and there will probably be some inaccuracy in the bias.   So, don't expect to get stable readings, but you should be getting readings around 512.   You may need to estimate or calculate the average to find the actual bias reading.

The first thing you need to do is subtract-out the bias.

With sound you should be getting "random looking" positive and negative readings (after subtracting-out the bias).     With quiet sounds you should get readings near zero, and with louder sounds you'll read larger positive and negative values, but you are reading a waveform that passes-through zero twice per cycle.   The Audacity Website has a little tutorial about how digital audio works.   (That will explain why your readings "look random".)  

And of course, voice and music has constantly-changing volume/loudness.

Speaking of Audacity...  Audacity can generate a test-tone file so you can play a constant-loudness pure tone for testing & calibration purposes.    (Of course it's still a wave and it's constantly changing...   But peak and average are stable.)

Quote
However, when I approach my phone, from which I play music for testing, to the microphone
To get "meaningful readings" you'll probably need something louder than a little cell-phone speaker.

Quote
double dB = (adc - 223.4) / 5.930; (linear regression using my samples from LoggerPro);
I don't know what you're taking about, but decibels are NOT linear!   

dB = 20log(ADC/ADCRef)


------------------------------------------------------------------------------------------
Of course, you can't use the raw  "random" ADC readings.  You'll need to find the peaks or average of  the positive readings (or average the absolute values) or you can calculate RMS.

Next, you need a reference.   To find a reference you generally need a known good SPL meter.     

For example, let's say your real SPL meter reads 80dB and the Arduino ADC reads 100 (peak or average or whatever you choose).    That's your reference.    We can calculate dB relative to that reference .    Now, lets say the Arduino reads 200, we can calculate dB relative  to that reference: 20log(200/100) = 6dB.    That's 6dB higher than the reference so we have 86dB SPL.

In theory, you could calculate a reference from the microphone specs and preamp the gain but that tends to be inaccurate.   Real SPL meters are always calibrated with an actual known sound level.  (If you attempt that, note the mic specs will give you RMS voltage.)

Real SPL meters also have Weigting and averaging.   That means, if you calibrate your meter with a 1kHz test-tone, the readings of your homemade SPL meter won't match the real SPL meter with real-world sounds.    You may be able to get "close enough", but it's difficult to build a good-calibrated SPL meter.

orange1785

Thank you soo much ! This could really solve the issue. I'll test it out .

Go Up