Ready to give your next project an ear?

These sound sensors are inexpensive, simple to use, and capable of detecting voice, claps, or door knocks.

You can use them for a variety of sound-reactive projects, such as making your lights clap-activated or monitoring your pets while you’re away.

Do you know how Electret Microphones work?

Inside a microphone is a thin diaphragm and a backplate. Collectively, they function as a capacitor.

electret microphone working.gif

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

When the diaphragm vibrates in response to sound, the plates move closer or farther apart, causing the capacitance to change. As a result, a voltage is generated across the plates, which we can measure to determine the amplitude of the sound.

Hardware Overview

The sound sensor is a small board that incorporates a microphone (50Hz-10kHz) and some processing circuitry to convert the sound wave into an electrical signal.

This electrical signal is fed to the on-board LM393 High Precision Comparator, which digitizes it and makes it available at the OUT pin.

sound sensor sensitivity adjustment and comparator

The module includes a potentiometer for adjusting the sensitivity of the OUT signal.

You can use it to set a threshold, so that when the amplitude of the sound exceeds the threshold, the module outputs LOW, otherwise HIGH.

This setup is very useful for triggering an action when a certain threshold is reached. For example, when the amplitude of the sound exceeds a threshold (a knock is detected), you can activate a relay to control the light.

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

sound sensor power and status leds

The module also includes two LEDs. The Power LED illuminates when the module is turned on, and the Status LED illuminates when the sound level exceeds the threshold value.

Sound Sensor Pinout

The sound sensor only has three pins:

sound sensor module pinout

VCC supplies power to the sensor. It is recommended that the sensor be powered from 3.3V to 5V.

GND is the ground pin.

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

Wiring a Sound Sensor to an Arduino

Let’s hook the sound sensor to the Arduino.

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

Finally, connect the OUT pin to Arduino digital pin #8. That’s it!

The wiring is shown in the image below.

wiring sound sensor with arduino

Setting the Threshold

The module has a built-in potentiometer for setting the sound level threshold above which the module outputs LOW and the status LED lights up.

digital output of sound sensor

Now, to set the threshold, click your finger close to the microphone and adjust the potentiometer until the module’s Status LED blinks in response to your clicks.

That’s all there is to it; your module is now ready to use.

Example 1 – Basic Sound Detection

The following simple example detects claps or snaps and displays a message on the serial monitor. Go ahead and try out the sketch; we’ll go over it in detail later.

#define sensorPin 8

// 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 working properly, you should see the following output on the serial monitor when the clap is detected.

sound sensor output

Code Explanation:

The sketch starts by declaring the Arduino pin to which the sensor’s OUT pin is connected.

#define sensorPin 8

Next, we define a variable called lastEvent that stores the time when a clap was previously detected. It will help us reduce accidental sound detection.

unsigned long lastEvent = 0;

In the Setup section, we configure the sensor’s OUT pin to behave as an input and establish serial communication.

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

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

int sensorData = digitalRead(sensorPin);

When the sensor detects a sound loud enough to exceed the threshold value, the output goes LOW. However, we must ensure that the sound is caused by clapping and not by background noise. Therefore, we wait 25 milliseconds after the output goes low. If the output remains LOW for more than 25 milliseconds, the message “Clap detected” is printed on the serial monitor.

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

Example 2 – Controlling Devices With a Clap

For our next project, we will use the sound sensor to create a “Clapper” that activates AC-powered devices with a handclap.

This project controls AC-powered devices using One Channel Relay Module. If you are unfamiliar with the relay module, consider reading the tutorial below.

Wiring

The wiring for this project is straightforward.

Warning:
This board interacts with HIGH AC voltage. Improper or incorrect use could result in serious injury or death. Therefore, it is intended for people who are familiar with and knowledgeable about HIGH AC voltage.

Let’s begin by providing power to the sensor and relay module. Connect their VCC pins to the Arduino’s 5V pin and GND to ground.

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

You’ll also need to connect the relay module to the AC-powered device you are attempting to control. You’ll need to cut your live alternating current line and connect one end of the cut wire (coming from the wall) to COM and the other to NO.

The wiring is shown in the diagram below.

wiring sound sensor and relay with arduino

Arduino Code

Here’s the code for controlling 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();
	}
}

When you’re done, the sensor should turn the device on or off every time you clap.

Code Explanation:

If you compare this sketch to our previous one, you’ll notice many similarities, with a few differences.

At the start, we declare the Arduino pin to which the relay’s control pin (IN) is connected. In addition, we have defined a new variable relayState to keep track of the relay’s status.

#define relayPin 7

boolean relayState = false;

In the Setup, we configure the relayPin as an output.

pinMode(relayPin, OUTPUT);

When we detect the sound of the clap, instead of printing the message on the serial monitor, we simply toggle the state of the relay.

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

Troubleshooting

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

  • Make sure that the power supply is clean. Because the sound sensor is an analog circuit, it is more sensitive to power supply noise.
  • The sensor is also sensitive to mechanical vibration and wind noise. Mounting the sensor to a solid substrate can reduce some of these vibrations.
  • This sound sensor has a very short sensing range, perhaps only 10 inches, so you’ll need to make a noise much closer to it in order to get a reliable reading.


Login
ADS CODE