Sound Sensor with Arduino Mega, LED including Buzzer

Sound Sensor with Arduino Mega, LED including Buzzer

I will show you how to implement a Sound sensor with an Arduino, LED and Buzzer which 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.

The materials I used were readily available from the Store. Below are the materials I used and they are very few:

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

knocksensorFritz-255x300 - steinacoz

 

This is what the Serial monitor should look like:

knocksensorSerial - steinacoz
knocksensorSerial – steinacoz

 

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: ");
    Serial.print(sensorReading);
    Serial.print("\n");
    Serial.print("\n");
    
    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
}

 

Feel free to add your comments below about this post.

Leave a Reply

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