Skip to main content
ESPBoards

ESP32 KY-038 Microphone Sound Sensor Module

The KY-038 is a microphone sound sensor module equipped with an electret condenser microphone and an LM393 comparator. It offers both analog and digital outputs, making it suitable for various sound detection applications, including voice-activated systems and sound level monitoring.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-038 Microphone Sound Sensor Module Datasheet ButtonKY-038 Microphone Sound Sensor Module Specs ButtonKY-038 Microphone Sound Sensor Module Specs ButtonKY-038 Microphone Sound Sensor Module Specs Button

🛒 KY-038 Price

KY-038 Microphone Sound Sensor Module
Normally, the KY-038 Microphone Sound Sensor Module costs around 2$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ About KY-038 Microphone Sound Sensor Module

The KY-038 Microphone Sound Sensor Module is designed to detect sound levels in the environment. It features an electret condenser microphone that captures sound signals and an LM393 comparator to process these signals. The module provides both analog and digital outputs: the analog output delivers a voltage proportional to the sound intensity, while the digital output goes high when the sound level exceeds a user-defined threshold, adjustable via an onboard potentiometer. This sensor is ideal for applications such as voice-activated projects, sound level monitoring, and interactive sound installations.

⚙️ KY-038 Sensor Technical Specifications

Below you can see the KY-038 Microphone Sound Sensor Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Analog/Digital
  • Operating Voltage: 3.3V to 5V
  • Microphone Sensitivity: -42 ±3 dB
  • Frequency Range: 50Hz to 10kHz
  • Dimensions: 35mm x 15mm x 14mm

🔌 KY-038 Sensor Pinout

Below you can see the pinout for the KY-038 Microphone Sound Sensor Module. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

  • +V: Connects to the power supply, typically 3.3V or 5V.
  • GND: Connects to the ground of the circuit.
  • D0 (Digital Output): Outputs a high signal when the detected sound exceeds the set threshold.
  • A0 (Analog Output): Provides an analog voltage proportional to the sound intensity.

🛠️ KY-038 Microphone Sound Sensor Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ No Response from Sensor

Issue: The sensor does not respond to sound.

Solutions:

  • Ensure the module is properly powered with the correct voltage (3.3V or 5V).
  • Verify all connections are secure and correctly wired.
  • Adjust the sensitivity using the onboard potentiometer.
  • Check if the microphone is functioning correctly and not obstructed.

⚠️ False Triggering

Issue: The sensor triggers without any sound input.

Solutions:

  • Reduce the sensitivity using the potentiometer to prevent false triggers.
  • Ensure the environment is free from electrical noise or interference.
  • Check for any loose connections or faulty components on the module.

💻 Code Examples

Below you can find code examples of KY-038 Microphone Sound Sensor Module with ESP32 in several frameworks:

If you encounter issues while using the KY-038 Microphone Sound Sensor Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-038 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-038 Microphone Sound Sensor Module:

int analogPin = A0;   // Analog output of the sensor
int digitalPin = 3; // Digital output of the sensor

void setup() {
pinMode(analogPin, INPUT);
pinMode(digitalPin, INPUT);
Serial.begin(9600);
Serial.println("KY-038 Sound Detection Module Test");
}

void loop() {
float analogValue = analogRead(analogPin) * (5.0 / 1023.0);
int digitalValue = digitalRead(digitalPin);

Serial.print("Analog Voltage: ");
Serial.print(analogValue, 4);
Serial.print(" V, Threshold: ");
Serial.println(digitalValue == HIGH ? "Reached" : "Not Reached");
Serial.println("----------------------------------------------------------------");
delay(1000);
}

This Arduino code initializes the analog and digital pins connected to the KY-038 sensor. It reads the analog voltage to monitor sound intensity and checks the digital output to determine if the sound level has exceeded the set threshold. The results are printed to the serial monitor every second.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-038 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-038 Microphone Sound Sensor Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#include "driver/gpio.h"

#define ANALOG_PIN ADC1_CHANNEL_6 // GPIO34
#define DIGITAL_PIN GPIO_NUM_15

void app_main(void) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ANALOG_PIN, ADC_ATTEN_DB_11);
gpio_set_direction(DIGITAL_PIN, GPIO_MODE_INPUT);

printf("KY-038 Sound Detection Module Test\n");
while (1) {
int raw = adc1_get_raw(ANALOG_PIN);
float voltage = raw * (3.3 / 4095.0);
int digital_state = gpio_get_level(DIGITAL_PIN);

printf("Analog Voltage: %.2f V, Sound Detected: %s\n", voltage, digital_state ? "Yes" : "No");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

This ESP-IDF code configures GPIO34 as an analog input and GPIO15 as a digital input for the KY-038 sound sensor. It reads the analog voltage and digital state, printing the voltage and whether a sound is detected to the console every second.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-038 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-038 Microphone Sound Sensor Module

sensor:
- platform: adc
pin: GPIO34
name: "KY-038 Sound Sensor Analog"
update_interval: 1s
filters:
- multiply: 3.3
- lambda: |-
return x * 1000; // Convert to millivolts

binary_sensor:
- platform: gpio
pin:
number: GPIO15
mode: INPUT_PULLUP
name: "KY-038 Sound Sensor Digital"

This ESPHome configuration sets up the KY-038 sensor with both an analog and digital sensor. The analog value from GPIO34 is converted to millivolts, while the digital sensor on GPIO15 acts as a binary sensor to indicate sound detection.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-038 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-038 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-038 Microphone Sound Sensor Module:

#include <Arduino.h>

#define ANALOG_PIN 34
#define DIGITAL_PIN 15

void setup() {
pinMode(DIGITAL_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-038 Sound Sensor Test");
}

void loop() {
int raw_value = analogRead(ANALOG_PIN);
float voltage = raw_value * (3.3 / 4095.0) * 1000; // Convert to millivolts
int digital_value = digitalRead(DIGITAL_PIN);
Serial.printf("Analog Voltage: %.2f mV, Digital State: %s\n", voltage, digital_value ? "Sound Detected" : "No Sound");
delay(1000);
}

This PlatformIO code sets up GPIO34 as an analog input and GPIO15 as a digital input for the KY-038 sensor. It reads the analog voltage and digital state, logging the voltage and sound detection status to the serial monitor every second.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-038 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-038 Microphone Sound Sensor Module with your ESP32.

import machine
import time

ANALOG_PIN = machine.ADC(machine.Pin(34))
ANALOG_PIN.atten(machine.ADC.ATTN_11DB)
DIGITAL_PIN = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)

while True:
analog_value = ANALOG_PIN.read()
voltage = (analog_value / 4095) * 3300 # Convert to millivolts
digital_value = DIGITAL_PIN.value()
print("Analog Voltage:", voltage, "mV", "| Digital State:", "Sound Detected" if digital_value == 1 else "No Sound")
time.sleep(1)

This MicroPython script configures GPIO34 as an ADC input and GPIO15 as a digital input for the KY-038 sensor. It continuously reads the sensor values and prints the voltage and sound detection state every second.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-038 Microphone Sound Sensor Module, its pinout, connection with ESP32 and KY-038 Microphone Sound Sensor Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.