Logo

ESP32 with KY-038/KY-037 Sound Sensor

Introduction to Sound Sensor

The KY-038 and KY-037 are sound detection sensors that use a microphone and op-amp to detect sound levels in the environment. While both sensors function similarly, the KY-037 is more sensitive due to its internal design. These modules are widely used in security alarms, noise detection systems, and sound-activated projects. In this project, we’ll connect a KY-038 to an ESP32 to detect sound events and print output on the Serial Monitor.

Components Required

We need a KY-038 or KY-037 sound sensor, an ESP32 development board, and jumper wires. The sensor’s digital output will be read by a GPIO pin on the ESP32 to determine if sound is detected.

  • Connect KY-038 VCC to ESP32 3.3V or 5V.
  • Connect KY-038 GND to ESP32 GND.
  • Connect KY-038 Digital OUT to ESP32 GPIO 15 (or any digital GPIO).
  • Open the Arduino IDE on your computer.
  • Go to Tools > Board > Boards Manager.
  • Search for 'ESP32' and install the ESP32 package.
  • Select 'ESP32 Dev Module' from the Board menu.
  • Connect ESP32 to your computer via USB cable.
  • Go to Tools > Port and select the correct COM port.
  • Go to Sketch > Include Library > Manage Libraries.
  • Search for 'Adafruit Unified Sensor' and install it.
  • Paste the provided Arduino code into a new sketch.
  • Click Upload (right arrow icon) to upload code to ESP32.
  • Wait for the code to compile and upload.
  • Open Serial Monitor (Ctrl + Shift + M) and set baud rate to 115200.
  • Observe sound detection status on Serial Monitor.

Formula: Sound Detected = digitalRead(SOUND_SENSOR_PIN)

Components List

  • 1 × ESP32 Development Board
  • 1 × KY-038 or KY-037 Sound Sensor Module
  • 1 × Breadboard
  • 1 × Set of Jumper Wires
  • 1 × USB Cable for ESP32
  • 1 × Computer with Arduino IDE installed

Pin Configuration

  • VCC: Connect to ESP32 3.3V or 5V pin
  • GND: Connect to ESP32 GND pin
  • Digital OUT: Connect to ESP32 GPIO 15

Use only the digital output pin of the sound sensor to detect sound (on/off) events. The analog output can be used for sound intensity measurement but is not covered in this example.

Wiring and Connections

  • VCC -> 3.3V or 5V
  • GND -> GND
  • Digital OUT -> GPIO 15

Code for ESP32 and Sound Sensor

1#define SOUND_SENSOR_PIN 15
2
3void setup() {
4  Serial.begin(115200);
5  pinMode(SOUND_SENSOR_PIN, INPUT);
6}
7
8void loop() {
9  int soundState = digitalRead(SOUND_SENSOR_PIN);
10  if (soundState == HIGH) {
11    Serial.println("Sound Detected!");
12  } else {
13    Serial.println("No Sound");
14  }
15  delay(500);
16}

Code Explanation

  • #define SOUND_SENSOR_PIN 15: Defines the GPIO pin used to read the digital signal from the sound sensor.
  • pinMode(SOUND_SENSOR_PIN, INPUT);: Sets the GPIO pin as an input to read sound detection signal.
  • digitalRead(SOUND_SENSOR_PIN): Reads the HIGH/LOW signal from the sound sensor.
  • Serial.println("Sound Detected!");: Prints a message when a sound is detected.

Applications

  • Sound-activated lighting systems
  • Intruder detection in smart security systems
  • Noise level monitoring in environments
  • Voice-triggered automation projects