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.

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!!!



Leave a Reply

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