Question about calibrating the microphone module

So I'm using the KY038 microphone module to change the color of an RGB strip based on noise in a room. I've calibrated the module such that the second LED lights up if I'm clapping from a couple meters away, however there's no change in the analog value being picked up by arduino when it does pick up the sound. This is a snippet of the code I'm using to monitor values:

int soundSensorPin = A0; 
int analogValue = 0;

void setup() {
  Serial.begin(9600);
}


void loop(){
  analogValue = analogRead(soundSensorPin);
  Serial.print("Analog Value: ");
  Serial.print(analogValue);
  Serial.println();
  delay(400);
}

Additionally, there are times when I move the module around a bit, bring it back to where it was before, and it outputs noticeably different values. Any suggestions? This problem is holding back the entire project.

BMohiuddin:
delay(400);

How long does your typical clap take?

Pieter

Your code only samples the sound about 2.5 times per second (the delay(400)) and the sound signal is time varying so an individual sample may read any value between the most negative and most positive excursions of the signal.

  1. A quick and dirty approach might be to take some number of samples in rapid succession (say 100 samples in an inner "for loop") and take the maximum as the value used.

  2. Alternatively one could get something proportional to the average sound power over an interval by taking some number of samples, subtracting the mean to get rid of the DC offset (nominally 512), and summing the square of each sample.

Right... A hand-clap probably lasts about 1 millisecond (1/1000th of a second) so if you're only reading the microphone about 2 times per second, the odds are you're going to miss it.

What microphone module do you have? ...There are a couple of different kinds. Most put-out an amplified & biased audio signal, but some put-out a digital-1 when the sound is above a preset level, and some put-out a DC voltage proportional to the loudness.

Even in a "fast loop" you may miss a hand-clap and if you print every time through the loop that will slow-down the loop. (It's often helpful to print-out the results while testing even if you want the final "real program" to run faster.)

[u]Here[/u] is a little tutorial about how analog signals are sampled/digitized.

I make sound-activated lighting effects and I use a [u]peak detector[/u] (AKA "envelope follower"). It puts-out a varying DC signal proportional to the peak amplitude/loudness so you can read it about 10 times per second instead of reading the audio waveform thousands of times per second (and it will "catch" a hand-clap). But, you only get amplitude from a peak detector so you can't use it if you want the frequency information.

Just to get started, take a look at my [u]World's Simplest Lighting Effect[/u]. It turns and LED on when the signal is above average and off when the signal is below average so you get lots of "LED action" with any regular audio.

It doesn't use a peak detector and it will work with a regular microphone board. I haven't tried it with a hand-clap but it runs a fast-loop so it will probably work. It does break-out of the loop every 50ms to "analyze" the signal and optionally print, so there is still a chance of missing a clap.