Add Sound Detector to Your Arduino Project

Last Updated on March 16, 2024

sound detector arduino

Knock? Knock! did you hear that sound, now you can add sound detection to your Arduino project, here I have used sparkfun Electret microphone Breakout board which is constructed with small microphone (100 Hz – 10KHz) and preamplifier. It detects and amplify the sounds of door knocks, claps, voice or any other sounds loud enough to picked up by the microphone.




Microphone Breakout

mic breakout

Here the Electret Mic capsule capturing sound wave between two conducting plates (one a movable and the other fixed) and converting sound waves into electrical wave.

Microphone Breakout Schematic

mic schematic

The Electrical signals are amplified and appears in “AUD” pin of breakout board. It can be picked up by the micro controller’s ADC.

The output at AUD pin orgin at half the supply voltage hence the ADC will ideally read 1/2 the full scale or in case 512 on a 10 bit ADC.

Arduino Mic Breakout Schematic

Arduino Hookup

arduino hookup sound detector

Connect Vcc pin and Ground pin of breakout board to Arduino power supply pins (+5  and Gnd), and then connect AUD (sound output) pin of breakout board to Arduino’s Analog pin A0.

Breakout — Arduino

Vcc –> +5V

Gnd –> Gnd

AUD –> A0

Here i have used the Arduino’s on board LED at digital pin 13, you can use any other pins as per your need.

Arduino Code for Sound Detection

Simple Code

int led = 13;
int threshold = 600; //Change This
int volume;

void setup() {                
  Serial.begin(9600); // Serial port begin
  pinMode(led, OUTPUT);     
}

void loop() {
  
  volume = analogRead(A0); // Reads the value from the Analog PIN A0
 
    //Serial print level
    
    Serial.println(volume);
    delay(100);

  if(volume>=threshold){
    digitalWrite(led, HIGH); //Turn ON Led
  }  
  else{
    digitalWrite(led, LOW); // Turn OFF Led
  }

}

Try clapping, Snapping, Door slamming. blowing, knocking etc.., and find the changes of level in serial monitor and Adjust the if statement according to the level printed in serial monitor.

You Can try this Arduino code also

/* 
* This code has been adapted from the 
* Example Sound Level Sketch for the 
* Adafruit Microphone Amplifier   
*/

const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz)
unsigned int knock;
int ledPin = 13;

void setup() 
{
   Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
 unsigned long start= millis();  // Start of sample window
 unsigned int peakToPeak = 0;   // peak-to-peak level

 unsigned int signalMax = 0;
 unsigned int signalMin = 1024;

 // collect data for 250 miliseconds
 while (millis() - start < sampleWindow)
 {
   knock = analogRead(0);
      if (knock < 1024)  //This is the max of the 10-bit ADC so this loop will include all readings
      {
         if (knock > signalMax)
         {
           signalMax = knock;  // save just the max levels
         }
      else if (knock < signalMin)
        {
         signalMin = knock;  // save just the min levels
         }
     }
 }
 peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
 double volts = (peakToPeak * 3.3) / 1024;  // convert to volts


Serial.println(volts);
 if (volts >=1.0)
 {
  //turn on LED
  digitalWrite(ledPin, HIGH);
   delay(500);
  Serial.println("Knock Knock");
 }
 else
 {
 //turn LED off
 digitalWrite(ledPin, LOW);
 }             
}





Leave a Reply

Your email address will not be published. Required fields are marked *