Skip to content

Circuit-Digest/KY-038-Sound-Sensor-works-and-Interfacing-it-with-ESP32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Sound-Activated Control System using ESP32 and KY-038 Sensor

KY-038 Sound Sensor with ESP32

πŸ“‹ Project Overview

This project demonstrates how to interface the KY-038 sound sensor module with an ESP32 microcontroller to create a sound-activated control system. The KY-038 is a microphone sound sensor that can detect sound intensity and trigger actions based on ambient noise levels, making it ideal for voice-activated switches, clap switches, and sound-responsive automation systems.

πŸ”— Project Reference

Full tutorial and detailed explanation: Interface KY-038 Sound Sensor with ESP32 - Circuit Digest

🎯 Features

  • Real-time Sound Detection: Monitors ambient sound levels continuously
  • Dual Output Modes: Digital (DO) and Analog (AO) outputs for flexible implementation
  • Adjustable Sensitivity: Built-in potentiometer for threshold adjustment
  • LED Indicator: Visual feedback when sound is detected
  • Low Power Consumption: Efficient operation suitable for battery-powered projects
  • Easy Integration: Simple interfacing with ESP32 GPIO pins

πŸ› οΈ Hardware Requirements

Components Needed:

  • ESP32 Development Board (ESP32-DevKitC or similar)
  • KY-038 Sound Sensor Module
  • LED (optional, for visual indication)
  • 220Ξ© Resistor (for LED)
  • Breadboard
  • Jumper Wires
  • USB Cable (for programming and power)

KY-038 Module Specifications:

  • Operating Voltage: 3.3V - 5V
  • Output: Digital (DO) and Analog (AO)
  • Detection Range: Adjustable via potentiometer
  • Microphone Type: Electret condenser microphone
  • PCB Size: 32mm x 17mm

πŸ“ Circuit Diagram

Pin Connections:

KY-038 Pin ESP32 Pin Description
VCC 3.3V Power Supply
GND GND Ground
DO GPIO 4 Digital Output
AO GPIO 34 Analog Output (ADC1_CH6)

Additional Connections (Optional LED):

Component ESP32 Pin
LED (+) via 220Ξ© GPIO 2
LED (-) GND

πŸ’» Software Requirements

  • Arduino IDE (version 1.8.x or later)
  • ESP32 Board Package installed in Arduino IDE
  • USB to UART Driver (CP210x or CH340)

Arduino IDE Setup:

  1. Install Arduino IDE from arduino.cc
  2. Add ESP32 board support:
    • Go to File β†’ Preferences
    • Add to Additional Board Manager URLs:
      https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
      
    • Go to Tools β†’ Board β†’ Board Manager
    • Search for "ESP32" and install

πŸ“ Code Implementation

Digital Output Mode (Simple Sound Detection)

// Define pins
#define SOUND_SENSOR_DO 4  // Digital output
#define LED_PIN 2          // Built-in LED

void setup() {
  Serial.begin(115200);
  pinMode(SOUND_SENSOR_DO, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.println("Sound Sensor Initialized");
}

void loop() {
  int soundDetected = digitalRead(SOUND_SENSOR_DO);
  
  if (soundDetected == LOW) {  // Sound detected (active LOW)
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Sound Detected!");
  } else {
    digitalWrite(LED_PIN, LOW);
  }
  
  delay(100);
}

Analog Output Mode (Sound Intensity Measurement)

// Define pins
#define SOUND_SENSOR_AO 34  // Analog output (ADC)
#define LED_PIN 2           // Built-in LED
#define THRESHOLD 2000      // Adjust based on your environment

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  Serial.println("Sound Intensity Monitor Started");
}

void loop() {
  int soundLevel = analogRead(SOUND_SENSOR_AO);
  
  Serial.print("Sound Level: ");
  Serial.println(soundLevel);
  
  if (soundLevel > THRESHOLD) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Loud sound detected!");
  } else {
    digitalWrite(LED_PIN, LOW);
  }
  
  delay(100);
}

πŸš€ Getting Started

Step 1: Hardware Assembly

  1. Connect the KY-038 module to ESP32 according to the pin connections table
  2. Connect the optional LED with resistor to GPIO 2
  3. Double-check all connections before powering up

Step 2: Sensor Calibration

  1. Power up the circuit
  2. Locate the potentiometer on the KY-038 module
  3. Turn the potentiometer clockwise to increase sensitivity
  4. Turn counter-clockwise to decrease sensitivity
  5. Adjust until the LED on the module lights up at desired sound levels

Step 3: Upload Code

  1. Open Arduino IDE
  2. Select the correct board: Tools β†’ Board β†’ ESP32 Dev Module
  3. Select the correct port: Tools β†’ Port β†’ (your COM port)
  4. Copy and paste the code
  5. Click Upload button
  6. Open Serial Monitor (115200 baud) to view output

Step 4: Testing

  1. Make sounds near the sensor (clap, talk, play music)
  2. Observe the LED and Serial Monitor output
  3. Adjust sensitivity if needed using the onboard potentiometer

🎨 Project Applications

  • Voice-Activated Switches: Control lights or appliances with claps or voice
  • Security Systems: Detect glass breaking or unusual sounds
  • Noise Monitoring: Environmental noise level detection
  • Interactive Art: Sound-responsive LED displays
  • Smart Home Automation: Integrate with existing home automation systems
  • Baby Monitor: Alert system for crying detection
  • Music Visualization: Create sound-reactive displays

πŸ”§ Troubleshooting

Issue Solution
No response from sensor Check power connections (VCC and GND)
Always detecting sound Decrease sensitivity using potentiometer
Never detecting sound Increase sensitivity or check DO/AO connections
Erratic readings Add small delay in loop, check for loose connections
ESP32 won't program Hold BOOT button while uploading

πŸ“Š Understanding the KY-038 Module

Digital Output (DO):

  • Provides HIGH/LOW signal based on threshold
  • Threshold adjusted via onboard potentiometer
  • Good for simple on/off applications

Analog Output (AO):

  • Provides variable voltage based on sound intensity
  • ESP32 ADC reads values 0-4095
  • Allows for graduated response and fine control

LED Indicators:

  • Power LED: Lights when module is powered
  • Status LED: Lights when sound exceeds threshold

🌟 Advanced Features

Sound-Activated Relay Control

Add relay module to control high-power devices:

#define RELAY_PIN 5

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  // ... other setup code
}

void loop() {
  if (soundDetected == LOW) {
    digitalWrite(RELAY_PIN, HIGH);  // Turn on device
    delay(5000);  // Keep on for 5 seconds
    digitalWrite(RELAY_PIN, LOW);
  }
}

Multiple Sensor Integration

Use multiple KY-038 modules for directional sound detection or room coverage.

πŸ“š Additional Resources

πŸ“„ License

This project is open-source and available for educational and commercial use.

🀝 Contributing

Contributions, issues, and feature requests are welcome! Feel free to check issues page.

πŸ‘¨β€πŸ’» Author

Your Name - Your Contact Information

πŸ™ Acknowledgments

  • Circuit Digest for the comprehensive tutorial
  • ESP32 community for support and libraries
  • Arduino community for the development platform

⭐ If you found this project helpful, please star it!

πŸ“§ For questions or suggestions, feel free to open an issue.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages