ESP8266 NodeMCU – KY-038 Sound Sensor

In this tutorial, we will learn how to use our capacitor sound sensor to measure sound intensity around the room.

As a result, we will be able to plot the surrounding sound values and turn the lights on when you clap your ends. Let´s start.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

KY-038 Sound Sensor

Figure 1

The objective of this sensor is to measure the ambience sound intensity around the area. Understanding the basic working principles of the sound sensor module and its usefulness, we can now begin to apply them in our projects.

For that to happen, an electric capacitor microphone is responsible to absorb the sound waves and produce the respective signal. There are two main functionalities for this sensor. One of them is the ability to turn on a digital output when sound is detected above a certain threshold. The threshold can be adjusted by a small potentiometer present on the board, which will modify the state of the digital pin DO. A good example is shown in figure 2.

Figure 2

The second functionality is the ability to directly connect the output signal of the microphone to an ADC (analog to digital converter), to obtain the raw data from the sensor. Doing so, a series of values can be printed and even make a graph with them to analyze the sound behaviour over a period of time. Figure 3 show two random examples of what can be done related to the data.

Figure 3

So, by using the values obtained by the method above, the wanted value for the activation of DO can be found while we observe the graph at the same time we clap our hands. This step will be explained in detail in the code section. Don’t worry!!!

Also, the specs and pinout can be seen in figure 4. If you want to have a better knowledge about the sensor, you may want to visit the datasheet.

Figure 4

Sensor Specs:
• Source voltage: 3-6V DC;
• Sound threshold potentiometer;
• Digital and analog Output;
• Power LED;
• LED for Digital output on. 


Figure 5

Consider the figure above. We must connect the pin A0 from the sensor to the pin A0 of the NodeMcu. The pin D0 from our sensor goes to the pin D8 of the NodeMcu. Finally, the pin D7 connects to the LED in series with a 220-ohm resistor and to the Gnd. The power supply connections for the sound sensor, Vcc and Gnd, must come from the NodeMcu board accordingly to the figure above.

Installing Libraries For Timer interrupt

To get our setup to recognize our claps, we first need to install a library to use a timer. But first, to obtain the library, a download is required from an open-source GitHub.

Figure 6

Paste the downloaded zip file in your library Arduino folder: Documents > Arduino > libraries. Then, in the Arduino IDE go to Sketch > Include Library > Add .ZIP Library > select the downloaded file > Open like in figure 7.

Figure 7


Having in mind what was explained in the previews chapter, a simple code will be uploaded first just to print a graph with the raw data to find the value for our clap.
The first step is to upload this simple code.

//For now, everything related to the digital output of the sound //sensor is going to be commented, because it is not relevant for the 
//first step
int led_pin = D7;
//int D0_Value = D8;
int raw_Value = A0;

void setup(){
  //pinMode(D0_Value, OUTPUT); // Set pin D8 as digital output
  pinMode(raw_Value, INPUT); //Set pin A0 as an input

void loop(){
  //int val_digital = digitalRead(D0_Value);
  int val_analog = analogRead(raw_Value );



The second step is running the code and opening the serial monitor. As we are looking at the monitor, a lot of values are being printed corresponding to the surrounding sound. To make the value extraction easy, be sure that your room is silent, like so, a constant row of values will be printed. Then clap your hands. It is important to clap firmly, several times periodically and with the same intensity per clap because the higher values (not silence) means a clap and will be used in the next step. In my case, the mean result for five claps was: 120. Be aware, that some sensors

The third step is to use the chosen value and insert it into the code. Be aware, that the LED will only turn on when you clap your hands 2 times in a short period of time, to distinguish from other sounds. Basically, if the code detect 2 sounds above our setpoint during 2 seconds, is an indication to open the light. Just upload the code and have fun!!!

#include "Timer.h"

Timer t;
int led_pin = D7;
int D0_Value = D8;
int raw_Value = A0;

int LED_State = 0;
int clap_counter = 0;
int Set_point = 120; //This is the value obtained from step 2

void setup(){
  pinMode(D0_Value, OUTPUT); // Set pin D8 as digital output
  pinMode(raw_Value, INPUT); //Set pin A0 as an input

  t.every(2000, Reset_Clap_counter);
 //Programed to call the function to reset clap counter every 2 seconds


void loop(){
  int val_digital = digitalRead(D0_Value);
  int val_analog = analogRead(raw_Value );

//This is optional. No need to show the values again

  digitalWrite (led_pin, LED_State); //Variable LED_State commands the state of the LED

//Remember that the counter is running. After 2 second it will reset the Clap_counter
//So the claps must be done rapidly

  if(val_analog > Set_point){
    clap_counter ++;

      if (clap_counter == 2){
        LED_State = !LED_State;
//This setpoint came from the adjustment of the potenciometer.
//Can be interpreted like a sound limiar. Ths is optional and it is 
//just a way for us to use this functionality

  if (val_digital){
    Serial.print("The room is very noisy");

//Function to reset counter every 2 second
void Reset_Clap_counter()
  clap_counter = 0;



2 thoughts on “ESP8266 NodeMCU – KY-038 Sound Sensor

  1. Was the construction: if(val_analog > Set_point)
    meant to be: if(val_analog > Set_point)
    Did something get lost in “translation” (i.e. transcription to web)?

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock