How a KY-038 Sound Sensor works and how to Interface it with ESP32?

Published  November 25, 2022   1
Interface KY-038 Sound Sensor with ESP32

Want to give your project the sense of sound! Then follow along because in this project we are interfacing a sound sensor to build some interesting projects with it. The KY-038 sound sensor we are using uses a condenser-type microphone to detect sound waves which give us a perfect balance of stability and reliability. So in this article, we decided to interface a KY-038 sensor with ESP32 and build a simple decibel meter out of it. In the process, we will let you know how a sound sensor works, how to work with it, and the pros and cons of it, so without further ado let's get right into it. With few tweaks, this circuit can also be converted into Noise pollution detector, burglar alarm etc.

Sound Sensor Pinout

The KY-038 sound sensor module has 4 pins VCC, GND, Digital Out, and Analog Out. We can either use the AO pin as an output for analog reading or the DO pin as an output for digital readout. The Sound sensor pinout is as follows:

Sound Sensor Pinout

VCC is the power supply pin of the Sound Sensor that can be connected to 3.3V or 5V of the supply. But note that the analog output will vary depending upon the provided supply voltage.

GND is the ground pin of the Sound Sensor module and it should be connected to the ground pin of the Arduino.

DOUT is the Digital output pin of the board, low output indicates that no sound is detected by the sensor, and high indicates that the sensor has detected sound.

AOUT is the Analog output pin of the board that will give us an analog reading directly from the Sound sensor.

How Does a Sound Sensor Module Work?

The working of the sounds sensor module is very simple, the main component in this module is a condenser microphone. The microphone gives out only analog signals when a sound wave hits the diaphragm of the sensor. This analog signal gets processed by the op-amp and we get the digital output.

The main component of a sound sensor is a microphone. There are many different types of microphones, like Carbon Microphones, Fiber Optic Microphones, Ribbon Microphones, and Laser Microphones, but the sound sensor module we are using has a condenser microphone. An image of the sound sensor module is shown below.

Sound Sensor Functional Diagram

As you can see from the image, a condenser microphone consists of two charged metal plates. The first plate is called the diaphragm and the second plate is the backplate of the microphone. These two plates together form a capacitor. When a sound wave hits the diaphragm of the microphone the diaphragm starts to vibrate, and the distance between the two plates changes. The movement of the diaphragm and the change in spacing produces the electrical signal that corresponds to the sound picked up by the microphone and this signal then gets processed by the onboard op-amp. This module also has two built-in onboard LEDs, one of which lights up when power is applied to the board and the other one lights up when the incoming audio signal exceeds the threshold value set by the potentiometer.

We previously used sound sensor with Arduino to build Arduino Whistle Detector, Arduino based musical fountain and noise lever meter.

Sound Sensor Module – Parts

For many different projects, this can be used to build sound-reactive switches or to build a sound-reactive LED visualizer. This is why this sensor is popular among beginners as these are low power, low cost, rugged, and feature a wide sensing range that can be trimmed down to adjust the sensitivity.

"Sound sensor module parts"

This sensor has three pins, two of which are power pins leveled VCC and GND and the other two are analog and digital pins shown in the diagram above. It has an onboard power LED and a signal LED. The power LED turns on when power is applied to the board and the signal LED turns on when the circuit is triggered. This board also has a comparator Op-amp that is responsible for converting the incoming analog signal to a digital signal. We also have a sensitivity adjustment potentiometer; with that, we can adjust the sensitivity of the device. Last, we have the condenser microphone that is used to detect sound. All these together make the total Sound Sensor Module.

Commonly Asked Questions about Sound Sensor Module

What are the types of sound sensors?

There are different types of microphones that are known as sound sensors like dynamic, condenser, ribbon, carbon, etc.

What are the advantages of a sound sensor?

Sound sensors can be used for security systems, often it works with speech recognition software where sound or speech is converted to text. This is a faster approach compared to typing using a keyboard.

What is the range of the sound sensor?

This sensor is capable of determining noise levels within 100 dB or decibels at 3 kHz, and 6 kHz frequencies range, approximately.

Can Arduino detect sound?

You'll learn how to use the KY-038 sound detection sensor with Arduino. You can measure changes in the intensity of sound in an environment with the ADC of the Arduino.

 

Sound Sensor Module Schematic Diagram

The schematic diagram for the Sound Sensor module is shown below. The schematic itself is very simple and needs a handful of generic components to build. If you don't have a prebuilt module on hand but still want to test your project, the schematic below will come in handy.

 

Sound Sensor Module Schematic Diagram

In the schematic, we have an LM393 op-amp that is a low-power, low-cost, low-offset voltage op-amp that can be powered from a 3.3V or 5V supply. Please note that the analog output voltage of the device will depend on the supply voltage of the device. The main job of this op-amp is to convert the incoming analog signal from the sensor probe to a digital signal. There is also this 10K potentiometer that is used to set a reference voltage for the op-amp, also this potentiometer is used to generate the reference voltage for the analog out function of the module.

If the input voltage of the sensor goes below the threshold voltage set by the potentiometer, the output of the op-map goes low. Other than that we have two LEDs. The first one is a power LED and the other one is the trigger LED. The power LED turns on when power is applied to the board and the trigger LED turns on when a certain set threshold is reached. This is how this basic circuit works.

 

Circuit Diagram for Decibel Meter with Sound Sensor and ESP32

Now that we have a good understanding of how the sound sensor works, we can connect all the required wires to the Arduino as per the schematic shown below.

Circuit Diagram for Decibel Meter with Sound

Connecting the sound sensor to the microcontroller is simple, we just need to power the module with 3.3V power and connect the analog out pin to the ESP32. Now we can process the signal with the ADC of the ESP32. We have also connected three LEDs to show the intensity of the device and we are also showing the recorded decibel on the OLED display. The LEDs are connected to the GPIO pins of the ESP32 and the OLED module is connected to the I2C pins of the ESP32 device. Learn more about interfacing OLED with ESP32 here.

Decibel Meter Circuit

Arduino Code for Decibel Meter

The code for the Arduino-based decibel meter is very simple and easy to understand. In the code, we will just measure the audio noise coming from the microphone and convert it to a digital signal with an ADC sample and figure out the decibel noise.

We start our code including all the required libraries; those are Wire.h, Adafruit_GFX.h, and Adafruit_SSD1306.h library. Next will define the screen width and screen height.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define  SCREEN_WIDTH 128 // OLED display width, in pixels
#define  SCREEN_HEIGHT 64 // OLED display height, in pixels
#define  OLED_RESET   -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Next, we have defined the instance for the Adafruit_SSD1306 library which is displayed.

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

Next, we have two variables to hold sampled data from the ADC.

const int sampleWindow = 50;
unsigned int sample;

We have defined all the required input and output pins for the LEDs and the sound sensor.

#define SENSOR_PIN 35
#define PIN_QUIET 33
#define PIN_MODERATE 25
#define PIN_LOUD 26

Next, we have our setup() function, in the setup function, we set all the required pins as input and output. And we also set the LED pins to low and initialize the serial. Finally, in the setup function, we initialize the display and set it to low.

void setup(){
  pinMode(SENSOR_PIN, INPUT); // Set the signal pin as input
  pinMode(PIN_QUIET, OUTPUT);
  pinMode(PIN_MODERATE, OUTPUT);
  pinMode(PIN_LOUD, OUTPUT);
  digitalWrite(PIN_QUIET, LOW);
  digitalWrite(PIN_MODERATE, LOW);
  digitalWrite(PIN_LOUD, LOW);
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.display();
  digitalWrite(PIN_LOUD, LOW);
}

Then, we have our loop function, in the loop function, we read the data from the ADC and sample it to store it within the min and max variable.

unsigned long startMillis = millis();                  // Start of sample window
  float peakToPeak = 0;                                  // peak-to-peak level
  unsigned int signalMax = 0;                            //minimum value
  unsigned int signalMin = 1024;                         //maximum value
  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow) {
    sample = analogRead(SENSOR_PIN);
    if (sample < 1024) {
      if (sample > signalMax) {
        signalMax = sample;                       
      }
      else if (sample < signalMin)  {
        signalMin = sample;                         
      }
    }
  }

Now to figure out the peak-to-peak signal ratio we subtract the min value from the max value and map this value to a 49dB to 90dB signal.

  peakToPeak = signalMax - signalMin;                 
  int db = map(peakToPeak, 0, 900, 49, 90);        
  Serial.print("\t");
  Serial.println(db);
  display.setCursor(0, 0);
  display.print("Loudness: ");
  display.print(db);
  display.print("dB");
  digitalWrite(PIN_LOUD, LOW);

Now with the help of if statements we check the values and according to the values, we light up a LED to show the sound intensity and finally print it on the display.  

   else if (db > 60 && db < 85) {
    display.clearDisplay();
    display.setCursor(0, 1);
    display.print("Level:Moderate");
    display.display();
    digitalWrite(PIN_QUIET, LOW);
    digitalWrite(PIN_MODERATE, HIGH);
    digitalWrite(PIN_LOUD, LOW);
  }
  else if (db >= 85 && db <= 90) {
    display.clearDisplay();
    display.setCursor(0, 1);
    display.print("Level:High");
    display.display();
    digitalWrite(PIN_QUIET, LOW);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD, HIGH);
  }
  else {
    digitalWrite(PIN_QUIET, LOW);
    digitalWrite(PIN_MODERATE, LOW);
    digitalWrite(PIN_LOUD, LOW);
  }
  delay(200);
}

Projects using Arduino and Sound Sensor Module

Previously we have used this sound sensor module to build many interesting projects. If you want to know more about those topics, the links are given below.

Arduino Controlled Musical Fountain

If you are a DIY! enthusiast and you are planning to build something decorative for your home then this project could be for you because in this project we have used an Arduino and a sound sensor to build a musical fountain that will generate aesthetic design with water.

Clap Switch using Arduino

If you are a beginner and want to build something easy and interesting then this project could be for you, because in this project we have used a condenser microphone and an Arduino to build a simple clap sensitive switch that can turn on and off your appliance with just a clap.

Arduino Whistle Detector Switch

If you want to turn on or off your home appliances with sound then you can build yourself a simple whistle-detecting circuit with Arduino and sound sensor. This whistle detecting system reduces the drawbacks of a clap switch and improves reliability of the circuit.

Measure Sound/Noise Level in dB with Arduino

If you're looking for an interesting project on the internet and having a hard time finding one then this project could be for you, because in this project we have used our favorite microcontroller Arduino and a sound sensor to build a decibel meter that can measure sound and show its intensity on the serial monitor window.

Code

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define  SCREEN_WIDTH 128 // OLED display width, in pixels

#define  SCREEN_HEIGHT 64 // OLED display height, in pixels

#define  OLED_RESET   -1 // Reset pin # (or -1 if sharing Arduino reset pin)

 

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

 

const int sampleWindow = 50;

unsigned int sample;

 

#define SENSOR_PIN 35

#define PIN_QUIET 33

#define PIN_MODERATE 25

#define PIN_LOUD 26

 

void setup(){

  pinMode(SENSOR_PIN, INPUT);

  pinMode(PIN_QUIET, OUTPUT);

  pinMode(PIN_MODERATE, OUTPUT);

  pinMode(PIN_LOUD, OUTPUT);

 

  digitalWrite(PIN_QUIET, LOW);

  digitalWrite(PIN_MODERATE, LOW);

  digitalWrite(PIN_LOUD, LOW);

 

  Serial.begin(115200);

 

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

    Serial.println(F("SSD1306 allocation failed"));

    for (;;);

  }

 

  display.clearDisplay();

  display.setTextSize(2);

  display.setTextColor(WHITE);

  display.display();

  digitalWrite(PIN_LOUD, LOW);

}

 

void loop()

{

  unsigned long startMillis = millis();                 

  float peakToPeak = 0;                                 

 

  unsigned int signalMax = 0;                           

  unsigned int signalMin = 1024;                         

 

  // collect data for 50 mS

  while (millis() - startMillis < sampleWindow)

  {

    sample = analogRead(SENSOR_PIN);

 

    if (sample < 1024)                                 

    {

      if (sample > signalMax)

      {

        signalMax = sample;                         

      }

      else if (sample < signalMin)

      {

        signalMin = sample;                          

      }

    }

  }

  peakToPeak = signalMax - signalMin;                   

  int db = map(peakToPeak, 0, 900, 49, 90);        

  Serial.print("\t");

  Serial.println(db);

  display.setCursor(0, 0);

  display.print("Loudness: ");

  display.print(db);

  display.print("dB");

  digitalWrite(PIN_LOUD, LOW);

  if (db <= 55)

  {

 

    display.clearDisplay();

    display.setCursor(0, 1);

    display.print("Level:Quite");

    display.display();

    digitalWrite(PIN_QUIET, HIGH);

    digitalWrite(PIN_MODERATE, LOW);

    digitalWrite(PIN_LOUD, LOW);

    //  delay(3000);

  }

  else if (db > 60 && db < 85)

  {

    display.clearDisplay();

    display.setCursor(0, 1);

    display.print("Level:Moderate");

    display.display();

    digitalWrite(PIN_QUIET, LOW);

    digitalWrite(PIN_MODERATE, HIGH);

    digitalWrite(PIN_LOUD, LOW);

  }

  else if (db >= 85 && db <= 90)

  {

    display.clearDisplay();

    display.setCursor(0, 1);

    display.print("Level:High");

    display.display();

    digitalWrite(PIN_QUIET, LOW);

    digitalWrite(PIN_MODERATE, LOW);

    digitalWrite(PIN_LOUD, HIGH);

  }

  else

  {

    digitalWrite(PIN_QUIET, LOW);

    digitalWrite(PIN_MODERATE, LOW);

    digitalWrite(PIN_LOUD, LOW);

  }

 

  delay(200);

}

Have any question realated to this Article?

Ask Our Community Members

Comments

Hello! I saw your project, it's awesome! but I have some questions. 

1. Can esp32 change to esp32-s3? if yes, the pin of the  circuit are the same? 

2. Can OLED use TFT LCD Touch Panel? if yes, what is the circuit (which pin to which pin)?