Interface Sound Sensor with Arduino and Control Devices With a Clap

Ready to add an ear to your next project?

These sound sensors are inexpensive, easy to interface with and are able to detect sounds of voice, claps or door knocks.

You can use them for a variety of sound reactive projects, for example, making your lights clap-activated or keeping an “ear” on your pets while you’re away.

Do you know how Electret Microphones work?

Inside the microphone is the thin diaphragm, which is actually one plate of a capacitor. The second plate is the backplate, which is close to and parallel to the diaphragm.

electret microphone working.gif

When you speak into the microphone, sound waves created by your voice strike the diaphragm, causing it to vibrate.

When the diaphragm vibrates in response to sound, the capacitance changes as the plates get closer together or farther apart.

As the capacitance changes, the voltage across the plates changes, which by measuring we can determine the amplitude of the sound.

Hardware Overview

The sound sensor is a small board that combines a microphone (50Hz-10kHz) and some processing circuitry to convert sound waves into electrical signals.

This electrical signal is fed to on-board LM393 High Precision Comparator to digitize it and is made available at OUT pin.

sound sensor sensitivity adjustment and comparator

The module has a built-in potentiometer for sensitivity adjustment of the OUT signal.

You can set a threshold by using a potentiometer; So that when the amplitude of the sound exceeds the threshold value, the module will output LOW otherwise HIGH.

This setup is very useful when you want to trigger an action when certain threshold is reached. For example, when the amplitude of the sound crosses a threshold (when a knock is detected), you can activate a relay to control the light. You got the idea!

Tip: Rotate the knob counterclockwise to increase sensitivity and clockwise to decrease it.

sound sensor power and status leds

Apart from this, the module has two LEDs. The Power LED will light up when the module is powered. The Status LED will light up when the digital output goes LOW.

Sound Sensor Pinout

The sound sensor only has three pins:

sound sensor module pinout

VCC pin supplies power for the sensor. It is recommended to power the sensor with between 3.3V – 5V.

GND is a ground connection.

OUT pin outputs HIGH when conditions are quiet and goes LOW when sound is detected. You can connect it to any digital pin on an Arduino or directly to a 5V relay or similar device.

Wiring Sound Sensor with Arduino

Let’s hook the sound sensor up to the Arduino.

Connections are fairly simple. Start by connecting VCC pin on the module to 5V on the Arduino and GND pin to ground.

Now connect the OUT pin to the digital pin #7 on your Arduino. That’s it!

The following illustration shows the wiring.

wiring sound sensor with arduino

Calibrating Sound Sensor

To get accurate readings out of your sound sensor, it is recommended that you first calibrate it.

The module has a built-in potentiometer for calibrating the digital output (OUT).

By turning the knob of the potentiometer, you can set a threshold. So that when the sound level exceeds the threshold value, the Status LED will light up and the digital output (OUT) will output LOW.

Now to calibrate the sensor, start clapping near the microphone and adjust the potentiometer until you see the Status LED on the module blink in response to your claps.

That’s it your sensor is now calibrated and ready for use.

Detecting Sound – Basic Example

Now that you have your sound sensor hooked up you’ll need a sketch to make it all work.

The following example detects claps or snaps and prints message on the serial monitor. Go ahead, try the sketch out; and then we will dissect it in some detail.

#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();
	}
}

If everything is fine, you should see below output on serial monitor when the clap is detected.

sound sensor output

Explanation:

The sketch begins with the declaration of the Arduino pin to which the sensor’s OUT pin is connected.

#define sensorPin 7

Next, we define a variable called lastEvent that stores the time since the clap detected. It will help us eliminate spurious sounds.

unsigned long lastEvent = 0;

In the Setup section, we declare the signal pin of the sensor as input. We also setup the serial monitor.

pinMode(sensorPin, INPUT);
Serial.begin(9600);

In the loop section, we first read the digital output from the sensor.

int sensorData = digitalRead(sensorPin);

When the sensor detects any sound loud enough to cross the threshold value, the output goes LOW. But we have to make sure that the sound is due to clapping and not due to the spurious background noise. So, we wait for 25 milliseconds. If output remains LOW for more than 25 milliseconds, we declare that the clap is detected.

if (sensorData == LOW) {
	if (millis() - lastEvent > 25) {
		Serial.println("Clap detected!");
	}
	lastEvent = millis();
}

Control Devices With a Clap

For our next project we will use the sound sensor as a “Clapper” that turns on AC powered devices with the clap of your hands.

This project uses One Channel Relay Module to control AC powered devices. If you are not familiar with the relay module, consider reading (at least skimming) below tutorial.

SUGGESTED READING

tutorial for controlling ac devices with one channel relay module and arduino
Interface One Channel Relay Module with Arduino
Sometimes you want your Arduino to control AC powered devices like lamps, fans or other household devices. But because the Arduino operates at 5 volts,...

Wiring

The wiring for this project is very simple.

Warning:
This board interacts with HIGH AC voltage. Incorrect or improper use could result in serious injury or death. So, it is intended for people experienced around, and knowledgeable about HIGH AC voltage.

First you need to supply power to the sensor and the relay module. Connect their VCC pins to the 5V pin on the Arduino and GND to ground.

Next connect the output pin (OUT) on the sound sensor to the digital pin #7 on your Arduino, and control pin (IN) on the relay module to the digital pin #8.

You’ll also need to place the relay module in line with the AC powered device you’re attempting to control. You’ll have to cut your live AC line and connect one end of the cut wire (coming from the wall) to COM and the other to NO.

The following illustration shows the wiring.

wiring sound sensor and relay with arduino

Arduino Code

Here’s the sketch to control devices with a clap.

#define sensorPin 7
#define relayPin 8

// Variable to store the time when last event happened
unsigned long lastEvent = 0;
boolean relayState = false;    // Variable to store the state of relay

void setup() {
	pinMode(relayPin, OUTPUT);  // Set relay pin as an OUTPUT pin
	pinMode(sensorPin, INPUT);  // Set sensor pin as an INPUT
}

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) {
		//toggle relay and set the output
		relayState = !relayState;
		digitalWrite(relayPin, relayState ? HIGH : LOW);
	}

	// Remember when last event happened
	lastEvent = millis();
	}
}

Once you have loaded and run the program with your hardware hooked up, the sensor should turn on or turn off the device every time you clap.

Explanation:

If you compare this sketch with our previous one you’ll notice many similarities, except few things.

At the start we declare the Arduino pin to which the relay’s control pin (IN) is connected. We have also defined a new variable relayState to store the state of relay.

#define relayPin 7

boolean relayState = false;

In the Setup, we define the relayPin as being output.

pinMode(relayPin, OUTPUT);

Now when we detect the sound of the clap, instead of printing the message on the serial monitor, we just toggle the state of the relay.

relayState = !relayState;
digitalWrite(relayPin, relayState ? HIGH : LOW);

Troubleshooting

If the Sound Sensor is misbehaving, try the following steps.

  1. Double check that the power supply is clean. Because the sound sensor is an analog circuit, it’s more sensitive to noise on the power supply.
  2. The electret microphone on the sound sensor is also sensitive to mechanical vibration and wind noise. Mounting it with a resilient material can help absorb vibration.
  3. The sensing range of this sound sensor is very small, probably 10 inches, so you have to make a noise much closer to get a good response.