ESP8266 - Sound Sensor

The sound sensor has the capability to detect the presence of sound in its surroundings. It can be employed to create projects that respond to sound, like lights that activate with a clap or a pet feeder that responds to sound cues.

This tutorial instructs you how to use the ESP8266 and a sound sensor to detect sound. We will explore:

ESP8266 NodeMCU NodeMCU sound sensor

Subsequently, you have the option to modify the code to trigger the activation of an LED or a light (using a relay) upon sound detection, or even make a servo motor rotate.

Hardware Preparation

1×ESP8266 NodeMCU
1×USB Cable Type-C
1×Digital Sound Sensor
1×Analog Sound Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP8266
1×(Recommended) Power Splitter For ESP8266 Type-C

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Sound Sensor

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

The sound sensor comes with a handy potentiometer that allows you to easily adjust its sensitivity. Additionally, it features two LED indicators:

  • One LED indicates the power status
  • Another LED indicates the sound state: it turns 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 ESP8266'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 ESP8266'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 sensor behaves:

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

Wiring Diagram

  • The wiring diagram between ESP8266 and sound sensor when powering via USB port
The wiring diagram between ESP8266 NodeMCU and Sound Sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between ESP8266 and sound sensor when powering via Vin
The wiring diagram between ESP8266 NodeMCU and Sound Sensor Vin

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For Sound Sensor

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

ESP8266 Code - Detecting the sound

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-sound-sensor */ #define SENSOR_PIN D7 // The ESP8266 pin D7 connected to the OUT pin of the sound sensor int prev_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 ESP8266's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the ESP8266's input pin sound_state = digitalRead(SENSOR_PIN); if (prev_state == HIGH && sound_state == LOW) Serial.println("The sound has been detected"); else if (prev_state == LOW && sound_state == HIGH) Serial.println("The sound has disappeared"); // save the the last state prev_state = sound_state; }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • 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 constantly turned on or off, regardless of the presence of sound, you can adjust the sensitivity of the sound sensor by adjusting the potentiometer.

Now, with the code customized, we can make it activate an LED or a light when sound is detected. We can even make a servo motor rotate. For detailed instructions and additional information, please refer to the tutorials provided at the end of this guide.

Troubleshooting

If you encounter issues with the sound sensor not functioning correctly, try the following troubleshooting steps:

  • Reduce vibrations: Mechanical vibrations and wind noise can affect the performance of the sound sensor. Mounting it on a stable surface can help minimize these disturbances.
  • Consider the sensing range: Keep in mind that this particular sound sensor has a limited sensing range of approximately 10 inches. To obtain accurate readings, try generating 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 disturbances 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!