In this article, I will show you how to implement a Sound sensor with an Arduino, LED and Buzzer.  This can be helpful in home automation, alarm system etc. The ideal here is just to take a reading (usually a knock on the door or just pick sound of certain frequency) from a sensor. Then flash and sound a buzzer or alarm for notifications.

arduino sound sensor

I decided to refresh my electronics abilities in preparation for an upcoming project. The materials I used were readily available or lifted from my school research lab since they were idle and rusting away.

Below are the materials I used and they are very few:

  • Arduino Mega ADK (you can use any other Arduino for yours)
  • Passive Buzzer
  • Sound Sensor
  • Arduino IDE

This is how I did the connections (demonstrated with Fritzing):



Bread board diagram showing connections

This is what the Serial monitor should look like:

Serial Monitor

Copy and paste this code to your IDE editor, compile and upload to your Arduino board:


/* Knock Sensor

   This sketch reads a piezo element to detect a knocking sound.
   It reads an analog pin and compares the result to a set threshold.
   If the result is greater than the threshold, it writes
   "knock" to the serial port, and toggles the LED on pin 13.

   The circuit:
	* A0 pin on the sensor attached to arduino analog pin 0
	* + pin on the sensor attached to arduino 5v pin
	* GND pin on the sensor attached to arduino GND pin
* Buzzer + pin attached to arduino digital pin 7
* Buzzer + pin attached to arduino digital pin 7
* Buzzer - pin attached to arduino GND pin
We will be using the default LED on the Arduino  pin 13


// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light
int buzzerPin = 7; 	//variable to store buzzer pin

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
  pinMode(buzzerPin, OUTPUT); // declare the buzzerPin as OUTPUT
  Serial.begin(9600);       // use the serial port

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);

  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    // toggle the status of the ledPin:
    ledState = !ledState;
    // update the LED pin itself:
    digitalWrite(ledPin, ledState);
    // send the string "Someone is knocking" back to the computer, followed by newline
    Serial.println("Someone is knocking");
    Serial.print("Sensor value: ");
    digitalWrite(buzzerPin, HIGH); //if the threshold is passed, sound the buzzer
    delay(1000); // allow the buzzer to sound for a second and then turn it off
    digitalWrite(buzzerPin, LOW);
  delay(100);  // delay to avoid overloading the serial port buffer


Please enter your comment!
Please enter your name here