The sound sensor does not detect an increase of sound unless air is blown directly into the microphone, I set the potentiometer at 40 dB from my code reaching upwards to 140 dB where it abruptly drops back to 40 dB.
How do I fix the range to detect noises in my room?
Its purpose is to ring a sound once the decibel level has reached a high enough level set in the code
int num_Measure = 78 ; // Set the number of measurements
int pinSignal = A0; // pin connected to pin O module sound sensor
long Sound_signal; // Store the value read Sound Sensor
long sum = 0 ; // Store the total value of n measurements
long level = 0 ; // Store the average value
int soundlow = 40;
int soundmedium = 60;
void setup ()
{
pinMode (pinSignal, INPUT); // Set the signal pin as input
Serial.begin (9600);
}
void loop ()
{
// Performs 128 signal readings
for ( int i = 0 ; i <num_Measure; i ++)
{
Sound_signal = analogRead (pinSignal);
sum =sum + Sound_signal;
}
level = sum / num_Measure; // Calculate the average value
Serial.print("Sound Level: ");
Serial.println (level-33);
if(level-33<soundlow)
{
}
if(level-33>soundlow && level-33<soundmedium)
{
}
if(level-33>soundmedium)
{
tone(9, 2000, 500);
}
sum = 0 ; // Reset the sum of the measurement values
delay(200);
}
@mynamesjohn12
The potentiometer only sets the threshold for the digital output (DO).
It has no affect on the analog output (AO)
When you do an analogRead, you will get values between 0 and 1023.
0 is no sound, 1023 is loud sound
Thanks. Those sensors are not calibrated, and don't output dB, which is a logarithmic measure of volume.
The analog output is proportional to instantaneous sound pressure level, so what analog readings do you typically get when "listening" to room noises?
Note that with this extremely low baud rate it takes about 10 ms to print a single reading, so do a bunch of readings, as fast as you can, and print out minimum, maximum and average.
The closest I can get locally is a voice recognition module, would that work for outputting dB?
What would be good sensor alternatives?
I've tried increasing the baud rate its minimum and average is still stuck at 40.
Thank you.
Are you sure? I couldn't find the actual specs... If it puts-out analog audio it should be biased at half of the ADC range and silence should read ~512 (assuming an Arduino with a 10-bit ADC).
If it's a peak detector (envelope follower) you are correct.
If I'm right, quiet sounds vary slightly around 512 and loud sounds will deviate up & down more. Since you are sampling a waveform, the readings will look random. You can subtract-out the bias to get positive & negative numbers and with louder sounds you'll get higher positive & negative peaks.
for ( int i = 0 ; i <num_Measure; i ++)
{
Sound_signal = analogRead (pinSignal);
sum =sum + Sound_signal;
}
That WILL work with a peak detector where a positive reading equates to loudness.
It WILL NOT work with a waveform because the the (unbiased) waveform is positive half of the time and negative half the time with an average of zero. With bias the positive & negatives will cancel-out and you'll just be essentially summing the bias multiple times.
You can sum or average the positive values (ignoring the negative values) or you can sum or average the absolute values.
After much research I have come to the best way to convert analog reading to DB. For the KY-038 with a sound of 94 DB SPLref, measured with an iPhone, the level was around 70 PeakRef for the analog reading of peak to peak. Some protection in the code are necessary to avoid infinite value.
float MesurePeakSonore() {
startMillis = millis(); // Start of sample window
peakToPeak = 0; // peak-to-peak level
dBSPLPeak = 0.0; // Peak dB SPL reading.
signalMax = 0;
signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow) {
sample = analogRead(SENSOR_PIN);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax) {
signalMax = sample; // save just the max levels
} else if (sample < signalMin) {
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
//transformation en décibels
dBSPLPeak = SPLref + 20 * log10((float)abs(peakToPeak) / PeakRef);
if (isinf(dBSPLPeak) == 0) {
return dBSPLPeak;
} else {
return 49.9;
}
}