This works like an ear in your project. These sensors are cheap and easy to use. These sensor detects the sounds like clap, door knocks etc. you can automate your appliances on your voice commands.
Working of electric microphone?
There is the small layer of diaphragm, it is actually the layer of capacitor. The plate which is situated on back is sound plate which is close and proportional to diaphragm.
When we speak the sound strikes to the diaphragm, causing it to vibrate. When diaphragm vibrates the capacitance changes because the plates are closer to each other.
When the capacitance changes the voltage on the plates changes, by measuring capacitance we can find the amplitude of the sound.
Hardware Overview
There is a small board on which a microphone and some extra components that convert sound waves in electrical signals.
There is built in potentiometer from which we can set the sensitivity of the out signal. You can set the threshold (pre-defined sound like someone enters) means when the amplitude of sound reach the threshold value it will turn on otherwise it will remain off.
Rotate potentiometer clockwise to increase sensitivity and rotate anti-clockwise to decrease the sensitivity.
There are two LED’s on the module the upper led shows the sensor is powered on and the other shows the status of the module.
Sound Sensor Pinout
There are three pin of the sound sensor.
VCC goes to the 5V or Arduino.
GND is a ground connection.
OUT goes to any digital pin of Arduino. By default it is HIGH but when it detects sound it will turn LOW.
Wiring Sound Sensor with Arduino
Calibrating Sound Sensor
Before using the sensor we suggest you to calibrate the sensor first. To calibrate the sound there is the potentiometer. You can set threshold by rotating knob. So when the sound reached the threshold value, the status led will ON. And digital output will goes LOW.
Now clap near to the sensor and adjust sensitivity through potentiometer and see when the status led will glow according to your clap. Now the sensor is read to use.
Detecting Sound – Basic Example
Now after hooking up sensor with Arduino you need a sketch to get fully functional. This sketch detects your claps or snaps and print out results on the screen. Let’s start our sketch gets our project interesting.
#define sensorPin 7
// Variable to store the time when last event happened
unsigned long lastEvent = 0;
void setup() {
pinMode(sensorPin, INPUT); // Set sensor pin as an INPUT
Serial.begin(9600);
}
void loop() {
// Read Sound sensor
int sensorData = digitalRead(sensorPin);
// If pin goes LOW, sound is detected
if (sensorData == LOW) {
// If 25ms have passed since last LOW state, it means that
// the clap is detected and not due to any spurious sounds
if (millis() - lastEvent > 25) {
Serial.println("Clap detected!");
}
// Remember when last event happened
lastEvent = millis();
}
}
When everything works properly you should see the result as clap detected.
Explanation:
First of all we have to initialize the pin that we are using for our senor. Now initialize a variable with zero that stores the time since the clap is detected. The variable named lastEvent.
In setup section, we have to tell the pin that we are using as input. Then begin serial monitor with baud rate of 9600.
In loop section read the value of digital pin (sensor) first.
If sensor detects that the voice is higher than the threshold value then the output goes low. But it has to make sure that the voice in a result of clapping rather than another sound. So for this we wait for 25 milliseconds. If output remain low for more than 25 milliseconds so we declare it as clap.