Arduino - Sound Sensor

The sound sensor is capable of detecting the presence of sound in the surrounding environment. It can be used to create sound-reactive projects, such as clap-activated lights or a sound-activated pet feeder.

In this tutorial, we are going to learn how to use Arduino and sound sensor to detect the sound. In detail, we will learn:

Arduino sound sensor

Afterward, you can tweak the code to make it activate an LED or a light (via a relay) when it detects sound, or even make a servo motor rotate.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Sound Sensor

The sound sensor can be utilized to detect the presence of sound in the surrounding environment. There are two types of sound sensor module:

  • Digital sound sensor module: outputs the digital signal value (ON/OFF)
  • Analog sound sensor module: outputs both analog and digital signal value

The sensitivity of digital output can be addjusted by using a built-in potentiometer.

The Digital Sound Sensor Pinout

The digital sound sensor includes three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • OUT pin: is an output pin: HIGH if quiet and LOW if sound is detected. This pin needs to be connected to Arduino's input pin.
Sound Sensor Pinout
image source: diyables.io

The sound sensor also has a built-in potentiometer, which can be used to addjust the sensitivity of the sensor. Further more, it has two LED indicators:

  • One LED indicator for power
  • One LED indicator for the sound state: on when sound is present, off when quiet

The Analog Sound Sensor Pinout

The analog sound sensor includes four pins:

  • + pin: needs to be connected to 5V
  • G pin: needs to be connected to GND (0V)
  • DO pin: is a digital output pin: HIGH if quiet and LOW if sound is detected. This pin needs to be connected to Arduino's digital input pin.
  • AO pin: is an analog output pin: outputs the analog value indicated sound level. This pin needs to be connected to Arduino's analog input pin.
analog sound sensor Pinout
image source: diyables.io

The analog sound sensor has a handy built-in potentiometer that lets you easily adjust its sensitivity for the digital output. Additionally, it comes with two LED indicators:

  • One LED indicator shows the power status.
  • Another LED indicator indicates the sound state, turning on when sound is detected and off when it's quiet.

How It Works

The module has a built-in potentiometer for setting the sound sensitivity.

  • When the sound is detected, the output pin of the sensor is LOW
  • When the sound is NOT detected, the output pin of the sensor is HIGH

Wiring Diagram

Arduino Sound Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

The real wiring:

Arduino Sound Sensor connection

How To Program For Sound Sensor

  • Initializes the Arduino pin to the digital input mode by using pinMode() function. For example, pin 5
pinMode(5, INPUT);
  • Reads the state of the Arduino pin by using digitalRead() function.
int soundState = digitalRead(5);

Arduino Code - Detecting the sound

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-sound-sensor */ // Arduino's pin connected to OUT pin of the sound sensor #define SENSOR_PIN 5 int lastState = HIGH; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The sound has been detected"); else if (lastState == LOW && currentState == HIGH) Serial.println("The sound has disappeared"); // save the the last state lastState = currentState; }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Clap your hand in front of the sound sensor
  • See the result on Serial Monitor.
COM6
Send
The sound has been detected The sound has disappeared The sound has been detected The sound has disappeared
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Please keep in mind that if you notice the LED status remaining on constantly or off even when there is sound, you can adjust the potentiometer to fine-tune the sound sensitivity of the sensor.

Now we can customize the code to activate an LED or a light when sound is detected, or even make a servo motor rotate. You can find more information and step-by-step instructions in the tutorials provided at the end of this tutorial.

Troubleshooting

If the sound sensor is not working properly, try the following steps:

  • Reduce vibrations: The sound sensor is also sensitive to mechanical vibrations and wind noise. Mounting the sound sensor on a solid surface can help minimize these vibrations.
  • Consider the sensing range: Keep in mind that this sound sensor has a short sensing range, around 10 inches. To obtain accurate readings, you will need to make a noise closer to the sound sensor.
  • Check the power supply: Ensure that the power supply is clean, as the sound sensor is sensitive to power supply noise due to being an analog circuit.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES