Arduino Nano ESP32 - Sound Sensor

The sound sensor has the capability to detect the presence of sound in its surroundings. It can be utilized to create trendy sound-responsive projects like lights that activate with a clap or a pet feeder that responds to sound.

In this tutorial, we will learn how to use the Arduino Nano ESP32 and a sound sensor to detect sound. We will delve into the following:

Arduino Nano ESP32 sound sensor

Following that, you have the flexibility to modify the code to trigger an LED or a light (using a relay) upon sound detection, or even enable a servo motor to rotate.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Sound Sensor

The sound sensor can be used to detect 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 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 ESP32's input pin.
Sound Sensor Pinout
image source: diyables.io

The sound sensor has a handy built-in potentiometer that lets you easily adjust its sensitivity. 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.

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 Nano ESP32'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 Nano ESP32's analog input pin.
analog sound sensor Pinout
image source: diyables.io

How It Works

The module includes a convenient built-in potentiometer that allows you to adjust the sound sensitivity. Here's how the output pin of the sensor behaves:

  • When sound is detected, the output pin is set to LOW.
  • When sound is not detected, the output pin is set to HIGH.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Sound Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For Sound Sensor

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

Arduino Nano ESP32 Code - Detecting the sound

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sound-sensor */ #define SENSOR_PIN D3 // The Arduino Nano ESP32 pin D3 connected to the OUT pin of the sound sensor int prev_sound_state = HIGH; // The previous state from the input pin int sound_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP32's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the ESP32's input pin sound_state = digitalRead(SENSOR_PIN); if (prev_sound_state == HIGH && sound_state == LOW) Serial.println("The sound has been detected"); else if (prev_sound_state == LOW && sound_state == HIGH) Serial.println("The sound has disappeared"); // save the the last state prev_sound_state = sound_state; }

Detailed Instructions

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
  • Clap your hand in front of the sound sensor
  • Check out the result on the 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 constantly on or off, even when there is sound present, you may need to adjust the potentiometer to fine-tune the sound sensitivity of the sensor.

Now, we have the freedom to personalize the code and make it trigger an LED or a light when sound is detected. We can even make a servo motor rotate according to the sound input. For more detailed guidance and step-by-step instructions, you can refer to the tutorials provided at the end of this tutorial.

Troubleshooting

If you encounter any issues with the sound sensor's functionality, please consider the following troubleshooting steps:

  • Reduce vibrations: Mechanical vibrations and wind noise can affect the sound sensor's performance. To minimize these disturbances, try mounting the sound sensor on a stable surface.
  • Consider the sensing range: Keep in mind that this particular sound sensor has a limited sensing range of approximately 10 inches. For accurate readings, try producing sound closer to the sensor.
  • Check the power supply: Ensure that the power supply is clean and free from noise, as the sound sensor is sensitive to power supply interference due to its analog nature.

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!