Interface Sound Sensor with Arduino and Control Devices With a Clap

Ever wanted to control your electronics with just a clap or snap? With a sound sensor module, your Arduino can start “listening” to the world around it!

These inexpensive and easy-to-use sensors allow you to build sound-reactive projects—like clap-activated lights, noise-sensitive alarms, or even fun interactive gadgets.

In this tutorial, we’ll walk you through how to hook up a sound sensor to an Arduino, understand how it works, and use it to trigger actions based on sound. Let’s get started and give your project an ear!

Do you know how Electret Microphones work?

An electret microphone consists of a thin, flexible diaphragm and a stationary backplate. These two parts sit very close to each other, forming a capacitor.

electret microphone working

When sound waves travel through the air and strike the diaphragm, they make it vibrate back and forth. As the diaphragm moves closer to or farther from the backplate, the capacitance between them changes.

This changing capacitance generates an electrical signal that corresponds to the sound waves. However, this signal is very weak—too weak to be useful on its own. That’s why electret microphones contain a Field-Effect Transistor (FET). This transistor amplifies the weak signal, making it strong enough for further processing in audio applications.

Hardware Overview

The sound sensor module is a small circuit board that includes a microphone, an LM393 High Precision Comparator chip, and some additional electronic components that help detect the sound signals.

Sound Sensor Sensitivity Adjustment and Comparator

The microphone picks up sound waves from the environment, typically within a frequency range between 50Hz and 10kHz. It then converts these sound waves into analog voltage signals.

On the module, there’s a potentiometer (a small knob) that allows you to adjust how sensitive the sensor is. In simpler terms, this lets you set a threshold or limit for detecting sound. When you turn the knob counterclockwise, the sensor becomes more sensitive and will detect quieter sounds. If you turn it clockwise, it becomes less sensitive and will only detect louder sounds.

The LM393 comparator chip plays an important role. It compares the analog signals from the microphone to the threshold level you have set with the potentiometer. If the incoming sound is louder than your threshold, the sensor’s output will switch to LOW. If the incoming sound is quieter than the threshold, the output stays HIGH.

This feature allows you to configure the sensor to respond only to sounds that exceed a certain loudness. This capability is particularly useful in projects where you want specific actions triggered by sound. For example, you can set the sensor to activate a relay and turn on a light whenever someone claps loudly.

Sound Sensor Power and Status LEDs

Additionally, the module has two small LEDs:

  • The Power LED turns on whenever the module is powered up.
  • The Status LED lights up whenever the detected sound exceeds your set threshold.

Sound Sensor Pinout

The sound sensor module has only three pins:

Sound Sensor Module Pinout

OUT is the output pin. When the sound level is louder than the threshold you set using the potentiometer on the board, the OUT pin sends a LOW signal. When sound is quieter than the threshold, it sends a HIGH signal. You can connect this pin to any digital pin on an Arduino or directly to a 5V relay module to control other devices.

GND is the ground pin.

VCC is used to supply power to the module. You should connect it to a voltage source between 3.3V and 5V.

Wiring a Sound Sensor to an Arduino

Let’s connect the sound sensor to an Arduino. The wiring is quite simple and doesn’t take much time.

First, connect the VCC pin on the sound sensor to the 5V pin on the Arduino to supply power. Next, connect the GND pin on the sensor to one of the Arduino’s ground (GND) pins. Finally, connect the OUT pin from the sensor to digital pin #8 on the Arduino.

The following table lists the pin connections:

Sound SensorArduino
VCC5V
GNDGND
OUT8

The wiring is shown in the image below.

Wiring Sound Sensor with Arduino

Setting the Threshold

As you may know, the sound sensor has a built-in potentiometer that lets you set the sound level threshold. When the sound gets louder than this set level, the sensor’s Status LED turns on and the OUT pin sends a LOW signal.

Digital Output of Sound Sensor

To set the threshold, try snapping your fingers or clapping your hands near the microphone. While doing this, gently turn the potentiometer until you see the Status LED blink in response to your sound.

That’s all there is to it! Your sound sensor is now ready to use in your projects.

Example 1 – Basic Sound Detection

In this first example, we’ll create a simple project that uses the sound sensor to detect claps or snaps and then display a message on the serial monitor. Give it a try; we’ll go over how the code works in a moment.

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

Once you upload the code to your Arduino and open the serial monitor, you should see a message saying “Clap detected!” each time the sensor picks up a loud sound like a clap or finger snap.

sound sensor output

Code Explanation:

The program begins by telling the Arduino which pin connects to the OUT pin of the sound sensor. In our case, we’re using digital pin 8.

#define sensorPin 8

Next, we create a variable called lastEvent. This variable stores the time when the last sound was detected.

unsigned long lastEvent = 0;

In the setup() section, we tell the Arduino to configure the sound sensor pin as an input. We also start serial communication, which lets you see messages printed out in the Serial Monitor.

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

Now, in the loop() section, the Arduino constantly checks the signal from the sound sensor. When the signal is LOW, it means the sensor has detected a sound that is louder than the threshold you’ve set using the potentiometer.

However, we don’t react immediately when we detect a LOW signal. Instead, we check whether at least 25 milliseconds have passed since the last detected sound. If that much time has passed and the signal is still LOW, then we consider this a genuine clap or snap. This timing check helps ignore accidental or random noises that might trigger the sensor by mistake. You don’t want the sensor to react to every little sound—just clear claps or snaps.

When these conditions are met, the Arduino displays “Clap detected!” on the Serial Monitor.

int sensorData = digitalRead(sensorPin);

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

Example 2 – Controlling Devices With a Clap

In this next project, we’re going to take the sound sensor a step further and build a fun and practical device—a “Clapper” that can turn AC-powered appliances on or off with just a handclap.

We’ll be using the relay module to control devices that run on high-voltage AC power. If you’re not yet familiar with how a relay works or how to use it, it’s a good idea to check out the tutorial linked below before continuing.

Wiring

Wiring is fairly simple, but there’s one very important thing to keep in mind:

Warning:

This project involves working with HIGH AC voltage (mains electricity). Improper handling can lead to serious injury or death.

This project is intended only for individuals who thoroughly understand and have experience with high-voltage AC circuits. If you are not comfortable or experienced with mains voltage, do not attempt this project. Safety should always be your top priority when working with electricity, especially at household voltage levels.

To get started, first connect the VCC pins of both the sound sensor and the relay module to the 5V pin on the Arduino, and connect their GND pins to the Arduino’s ground. Next, connect the OUT pin of the sound sensor to digital pin 7 on the Arduino. Then connect the control pin (IN) of the relay module to digital pin 8.

Now comes the part where you connect the relay module to your AC-powered device, like a lamp. Before you start, make absolutely sure the lamp is unplugged and the power source is switched off! This is extremely important for your safety.

Carefully cut the live wire of the lamp’s power cord. Take the end of the wire coming from the wall outlet and connect it to the COM (Common) terminal on the relay. Then connect the other end of the cut wire (the one going to the lamp) to the NO (Normally Open) terminal. When the relay switches on, it will close the circuit and allow electricity to flow to your lamp.

The following tables list the pin connections:

Sound SensorArduino
VCC5V
GNDGND
OUT7
Relay ModuleArduino
VCC5V
GNDGND
IN8

The image below will help you build the circuit correctly.

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

Once the code is uploaded to your Arduino, clapping your hands near the sensor will toggle the connected device on or off.

Code Explanation:

Now, let’s go over how the code works.

If you’ve already tried the first example, you’ll notice that this sketch is very similar. However, there are a couple of key differences.

First, we declare which Arduino pin is connected to the relay’s control pin. Then, we introduce a new variable called relayState. This variable keeps track of whether the relay is currently on or off, so we can switch it back and forth each time a clap is detected.

#define relayPin 7

boolean relayState = false;

In the setup section, we set the relay pin as an output, which means the Arduino will be sending signals to it.

pinMode(relayPin, OUTPUT);

When a clap is detected, instead of printing a message on the serial monitor like in the last example, we simply flip the relayState to its opposite value. If the relay was off, it turns on. If it was on, it turns off.

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