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:

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
1×Sound Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Optional) Screw Terminal Block Shield for Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are affiliate links. If you buy the components through these links, We may 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. You can adjust the detection threshold (sensitivity) using a built-in potentiometer.

Pinout

The 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

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.

Function References

The Best Arduino Starter Kit

※ OUR MESSAGES