Arduino Tutorial: KY-038 Sound Sensor Module
Abstract
Learn how to use the KY-038 Sound Sensor Module with Arduino. This module uses a microphone to detect sound and provides two outputs: a digital ON/OFF signal for noise events and a raw analog value representing sound intensity. This tutorial focuses on configuring both digital and analog input pins to detect sound and measure its approximate level.
1. Introduction
The KY-038 module is designed to detect the presence and approximate loudness of sound. It integrates:
- Microphone: Converts sound waves into a tiny electrical voltage.
- Amplifier Circuitry: Boosts the microphone’s weak signal.
- Comparator Circuitry: Converts the amplified signal into a sharp digital HIGH or LOW signal (D0).
- Analog Output (A0): Provides the raw, amplified waveform, which correlates with sound intensity.
- Potentiometer: Allows the user to adjust the trigger threshold for the digital output (D0).
In this episode, you’ll learn:
- The difference between the module’s digital (D0) and analog (A0) outputs.
- How to use the analog output (A0) to measure instantaneous sound volume.
- How to use the digital output (D0) for simple clap or noise detection.
- How to use the module’s onboard potentiometer to set the digital trigger sensitivity.
This project adds basic audio sensing to your Arduino capabilities.
2. Prerequisites
Make sure you have:
- An Arduino Uno or compatible board.
- One KY-038 Sound Sensor Module.
- Jumper Wires.
- Arduino IDE
3. Wiring and Setup for Arduino
The KY-038 module has four pins and requires both digital and analog input pins.
Step 1 – Identify Pins
The module typically has four pins: A0 (Analog Output), D0 (Digital Output), VCC (+), and GND (− or GND).
Step 2 – Connect the Module
Wire the module to the Arduino as follows:
- Connect the GND pin of the KY-038 to the GND pin on the Arduino.
- Connect the VCC pin of the KY-038 to the 5V pin on the Arduino.
- Connect the D0 pin of the KY-038 to Arduino Digital Pin 7 (Digital Input).
- Connect the A0 pin of the KY-038 to Arduino Analog Pin A0 (Analog Input).
This image was created with Fritzing
Step 3 – Initialize Pin Modes
pinMode(7, INPUT); // D0 pin for digital ON/OFF detection
4. Writing Dual-Mode Sound Sensing Code
We will monitor both the analog output (to get a sense of noise level) and the digital output (to detect a sound event like a clap). We’ll use the onboard LED (Pin 13) to reflect the digital state (D0).
Open main.ino and implement the following code.
const int DIGITAL_PIN = 7; // D0 Output
const int ANALOG_PIN = A0; // A0 Output
const int LED_PIN = 13; // Built-in LED
// Variables for filtering the analog reading
int maxVolume = 0; // Tracks the peak volume since the last reading
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(DIGITAL_PIN, INPUT);
Serial.begin(9600);
Serial.println("KY-038 Sound Sensor Ready!");
}
void loop() {
// 1. Analog Reading (A0) - Read 100 samples to find the peak volume
// This helps to smooth the fluctuating waveform into a single 'loudness' value.
maxVolume = 0;
for(int i = 0; i < 100; i++) {
int raw = analogRead(ANALOG_PIN);
if (raw > maxVolume) {
maxVolume = raw;
}
}
// 2. Read Digital State (D0) - Active LOW typically means noise detected
int digitalState = digitalRead(DIGITAL_PIN);
// Control LED based on Digital Output
if (digitalState == LOW) {
digitalWrite(LED_PIN, HIGH); // Turn LED ON (Sound Detected)
} else {
digitalWrite(LED_PIN, LOW); // Turn LED OFF (Quiet)
}
// Print both outputs to the Serial Monitor
Serial.print("Analog (Peak Volume): ");
Serial.print(maxVolume);
Serial.print(" | Digital (Switch): ");
if (digitalState == LOW) {
Serial.println("DETECTED (LOW)");
} else {
Serial.println("CLEAR (HIGH)");
}
delay(100);
}
Code Explanation
- Analog Filtering: Sound is a wave that changes rapidly. Simply reading analogRead() once gives a random point on the wave. The for loop reads 100 times quickly and finds the peak voltage (maxVolume), which correlates better to the perceived loudness.
- D0 Output: The digital output switches to LOW when the sound volume exceeds the fixed threshold set by the potentiometer.
5. Adjusting Sensitivity (Potentiometer)
The potentiometer on the KY-038 controls the sensitivity of the Digital Threshold for the D0 pin:
- Open the Serial Monitor and observe the Analog (Peak Volume) when the room is quiet. This is your baseline noise.
- Make a medium-loud noise (e.g., a clap) a few feet away. Note the peak volume.
- Adjust the Potentiometer with a small screwdriver. You are setting the sensitivity:
- Turn clockwise: Decreases sensitivity, requiring a much louder sound to trigger the D0 output to LOW.
- Turn counter-clockwise: Increases sensitivity, making even quiet ambient noise trigger the D0 output.
- Find the sweet spot where only your intended sound (e.g., a clap) triggers the Digital (Switch) output.
6. Uploading and Running the Project
Step 1 – Build & Upload
Complete the standard build and upload process.
Step 2 – Test
- Calibrate the digital threshold using the potentiometer (Section 5).
- Quiet Room: The Arduino LED (Pin 13) should be OFF, and the Digital state should be CLEAR (HIGH).
- Make Noise: When you make a noise that exceeds the calibrated threshold, the LED should turn ON, and the Digital state should read DETECTED (LOW).
- Observe the Analog (Peak Volume) to see how the raw loudness changes with your voice or claps.
7. Hands-On Lab Recap
You’ve learned:
- The operational difference between the Analog (A0) and Digital (D0) sound sensor outputs.
- A basic technique for filtering the analog sound signal to get a meaningful loudness value.
- The crucial skill of calibrating the digital threshold with the onboard potentiometer.
This concludes the comprehensive series on fundamental KY-series modules, providing you with a complete toolkit for basic electronic projects.
8. Common Issues & Fixes
| Issue | Cause | Solution |
|---|---|---|
| Digital LED is always ON or never turns ON. | Potentiometer is mis-calibrated. | Calibrate the sensor carefully (Section 5). It must be set to separate the ambient noise baseline from the target sound's amplitude. |
| Analog reading is fixed near 512 and amplitude is near 0. | The simple microphone output is fixed near VCC/2. | This is normal; the maxVolume calculation is correctly tracking the tiny fluctuation around 512. Focus on the maxVolume variable, which should increase with loud noise. |
| Digital works, but Analog does not respond to the sound. | Faulty microphone or amplifier circuit. | Check that the VCC and GND are secure. If maxVolume never changes, the sensor may be defective. |


