Electret Microphone Sound Detection & Clap Switch
Capture ambient acoustic peaks with a KY-037/KY-038 microphone module to build clap-activated switches and noise meters.
Interface an electret condenser microphone module with Arduino Uno to measure sound pressure envelopes and detect sharp hand claps. Combine analog peak-to-peak sampling on A0 with the onboard LM393 comparator on D2 for reliable clap toggling.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerThe KY-037 sound module draws ~4mA from Arduino Uno 5V. Its analog output centers around ~2.5V (ADC ~512) and swings dynamically with acoustic waves. Keep microphone isolated from mechanical motor vibration; tune multi-turn blue potentiometer carefully.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| KY-037 / KY-038 Sound Sensor Module | Electret capsule + LM393 comparator + AO/DO | 1 | $1.50 |
| 5mm LED + 220Ω Resistor | Clap-activated visual load | 1 | $0.15 |
| Breadboard & Jumper Wires | 6× breadboard leads | 1 | $2.00 |
Electrical & System Architecture
Connect Sound Module VCC (+) to 5V, GND (-) to GND, AO to A0, and DO to D2. Wire a clap-controlled LED (+220Ω resistor) to D8.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling KY-037 / KY-038 Electret Sound Sensor Module acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Inspect KY-037 4-Pin Header & Multi-Turn Trimpot
Identify AO, G (GND), + (5V), and DO on the sound module.
- Check silkscreen labels carefully—pin order varies across KY-037 and KY-038 batches.
- Note the brass screw on top of the blue multi-turn potentiometer.
- Face the black felt-covered electret capsule upward toward the room.
Wire Sound Module to A0/D2 & LED to D8
Connect power, analog/digital outputs, and the clap-toggled LED.
- Connect Sound Module + → 5V and G → GND.
- Connect AO → A0 and DO → D2.
- Wire D8 → 220Ω resistor → LED anode → cathode to GND.
Observe Live Noise Floor in Serial Plotter
Run the peak-to-peak diagnostic and clap once to compare quiet room vs. clap peak.
- Upload the diagnostic snippet and open Serial Monitor or Serial Plotter.
- Note your quiet baseline peak-to-peak (usually 5–25 ADC counts).
- Clap 30cm away and note the spike height (typically 80–350 counts).
Upload Clap-Switch & Noise Meter Firmware
Flash the peak-to-peak clap detector and verify clean single-clap LED toggling.
- Adjust CLAP_PEAK_THRESHOLD if needed and upload the production sketch.
- Clap once cleanly → D8 LED switches ON.
- Clap again → D8 LED switches OFF with zero double-bounce.
Tune Hardware DO Comparator (Optional)
Rotate the multi-turn trimpot until the onboard L2 LED just extinguishes in silence and flashes on claps.
- In a quiet room, turn the brass screw slowly until the threshold LED sits right at the edge of turning OFF.
- Clap once and confirm the onboard LED flashes sharply.
- Compare D2 digital trigger vs A0 analog amplitude.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: KY-037 / KY-038 Electret Sound Sensor Module Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for KY-037 / KY-038 Electret Sound Sensor Module before running the full project firmware.
// Diagnostic: 40ms Peak-to-Peak Sound Amplitude
void setup() { Serial.begin(115200); }
void loop() {
int mn = 1023, mx = 0; unsigned long t0 = millis();
while (millis() - t0 < 40) { int v = analogRead(A0); if (v < mn) mn = v; if (v > mx) mx = v; }
Serial.println(mx - mn);
}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 19: Electret Microphone Sound Meter & Clap Switch
* Board: Arduino Uno R3 | Pins: A0 (Sound AO), D8 (Clap LED)
*/
const uint8_t SOUND_AO = A0;
const uint8_t CLAP_LED = 8;
const int CLAP_PEAK_THRESHOLD = 95; // Tune from quiet vs clap baseline
const unsigned long LOCKOUT_MS = 350;
bool ledState = false;
unsigned long lastClapMs = 0;
int samplePeakToPeak(uint16_t windowMs) {
int sigMin = 1023, sigMax = 0;
unsigned long start = millis();
while (millis() - start < windowMs) {
int val = analogRead(SOUND_AO);
if (val < sigMin) sigMin = val;
if (val > sigMax) sigMax = val;
}
return sigMax - sigMin;
}
void setup() {
pinMode(CLAP_LED, OUTPUT);
Serial.begin(115200);
Serial.println(F("Sound Detector & Clap Switch Ready"));
}
void loop() {
int peak = samplePeakToPeak(40);
unsigned long now = millis();
if (peak >= CLAP_PEAK_THRESHOLD && (now - lastClapMs >= LOCKOUT_MS)) {
lastClapMs = now;
ledState = !ledState;
digitalWrite(CLAP_LED, ledState ? HIGH : LOW);
Serial.print(F("[CLAP!] Peak=")); Serial.print(peak);
Serial.print(F(" -> LED ")); Serial.println(ledState ? F("ON") : F("OFF"));
}
}Firmware Architecture Explanation:
- Measures peak-to-peak excursion over 40ms windows so AC audio waves yield accurate loudness values.
- Applies a 350ms acoustic lockout window to reject room echo after each clap.
- Toggles D8 cleanly without needing delicate hardware potentiometer tuning.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Peak < 95 | A0 Quiet Room | Ambient Noise Floor | Hold Current LED State |
| Peak ≥ 95 | A0 Acoustic Impulse | Clap Detected | Toggle D8 LED + 350ms Lockout |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Claps never trigger the LED | CLAP_PEAK_THRESHOLD higher than module gain | Run diagnostic sketch while clapping 30cm away | Lower CLAP_PEAK_THRESHOLD to ~60% of observed clap peak. |
| Triggers constantly even in silence | Threshold too low or noisy power rail | Check quiet peak-to-peak baseline | Raise CLAP_PEAK_THRESHOLD above quiet baseline + 30. |
| Peak always reads 0 | Wired to DO instead of AO | Inspect jumper on module pinout | Ensure A0 connects to AO (Analog Output). |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Require two claps spaced 200–700ms apart so accidental single bumps never flip the light.
Drive 5 LEDs proportionally to live room volume as an instant decibel meter.
Send live sound peaks to the Project 14 MAX7219 8×8 matrix display.