MyRoboPathOpen Robotics Lab
Arduino Uno #19 of 35
Est. 0.5 Hours
Budget: $2 - $4
Beginner

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.

Arduino Uno R3KY-037 MicrophonePeak-to-Peak ADCLM393 ComparatorClap Toggle
Electret Microphone Sound Detection & Clap Switch Blueprint
Electret Microphone Sound Detection & Clap SwitchCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Understand why audio waveforms oscillate around a DC midpoint and require peak-to-peak (max - min) sampling.
2Calibrate the multi-turn LM393 threshold potentiometer for crisp hand-clap sensitivity.
3Use a 350ms acoustic lockout window to ignore room reverberation after a clap.
4Stream live sound amplitude bars to Arduino Serial Plotter.

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

The 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.

Electret Microphone Sound Detection & Clap Switch Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for Electret Microphone Sound Detection & Clap Switch
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P @ 16MHz1$8.00
KY-037 / KY-038 Sound Sensor ModuleElectret capsule + LM393 comparator + AO/DO1$1.50
5mm LED + 220Ω ResistorClap-activated visual load1$0.15
Breadboard & Jumper Wires6× breadboard leads1$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.

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for Electret Microphone Sound Detection & Clap Switch.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for Electret Microphone Sound Detection & Clap Switch.Wiring Schematic

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.

Figure: Firmware execution flow and state transitions for Electret Microphone Sound Detection & Clap Switch.
Figure: Firmware execution flow and state transitions for Electret Microphone Sound Detection & Clap Switch.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Inspect KY-037 4-Pin Header & Multi-Turn Trimpot

Duration: 5 Mins

Identify AO, G (GND), + (5V), and DO on the sound module.

Electret Microphone Sound Detection & Clap Switch hardware layout diagram
Stage 1: KY-037 / KY-038 Electret Sound Sensor Module hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Peak-to-peak analog measurement on A0 works reliably even before you touch the blue trimpot!
2

Wire Sound Module to A0/D2 & LED to D8

Duration: 5 Mins

Connect power, analog/digital outputs, and the clap-toggled LED.

Electret Microphone Sound Detection & Clap Switch breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Connect Sound Module + → 5V and G → GND.
  • Connect AO → A0 and DO → D2.
  • Wire D8 → 220Ω resistor → LED anode → cathode to GND.
Engineering & Tuning Tip: Avoid placing the microphone right next to a noisy cooling fan.
3

Observe Live Noise Floor in Serial Plotter

Duration: 5 Mins

Run the peak-to-peak diagnostic and clap once to compare quiet room vs. clap peak.

Electret Microphone Sound Detection & Clap Switch power rail protection diagram
Stage 3: Supply rail check (USB 5.0V Regulated Rail (<5mA)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • 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).
Engineering & Tuning Tip: Set CLAP_PEAK_THRESHOLD halfway between your quiet baseline and a crisp hand clap.
4

Upload Clap-Switch & Noise Meter Firmware

Duration: 10 Mins

Flash the peak-to-peak clap detector and verify clean single-clap LED toggling.

Electret Microphone Sound Detection & Clap Switch serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: A 350ms lockout window prevents clap echoes in tiled rooms from immediately toggling the LED back off.
5

Tune Hardware DO Comparator (Optional)

Duration: 5 Mins

Rotate the multi-turn trimpot until the onboard L2 LED just extinguishes in silence and flashes on claps.

Electret Microphone Sound Detection & Clap Switch live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • 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.
Engineering & Tuning Tip: Multi-turn trimpots take 10–15 full turns end-to-end—be patient while finding the trigger threshold.

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 Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for KY-037 / KY-038 Electret Sound Sensor Module before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// 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);
}
Expected Output: Prints low numbers (5–20) in silence and sharp spikes (100+) on a hand clap.

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_sound_detection_sensor.ino
arduino_uno_sound_detection_sensor.inoArduino C++
/**
 * 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

1Double-check all 4 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: Prints low numbers (5–20) in silence and sharp spikes (100+) on a hand clap.
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for Electret Microphone Sound Detection & Clap Switch
Input / StatePin / SignalDetected ConditionExpected Output
Peak < 95A0 Quiet RoomAmbient Noise FloorHold Current LED State
Peak ≥ 95A0 Acoustic ImpulseClap DetectedToggle D8 LED + 350ms Lockout

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for Electret Microphone Sound Detection & Clap Switch
ProblemLikely CauseDiagnostic CheckEngineering Solution
Claps never trigger the LEDCLAP_PEAK_THRESHOLD higher than module gainRun diagnostic sketch while clapping 30cm awayLower CLAP_PEAK_THRESHOLD to ~60% of observed clap peak.
Triggers constantly even in silenceThreshold too low or noisy power railCheck quiet peak-to-peak baselineRaise CLAP_PEAK_THRESHOLD above quiet baseline + 30.
Peak always reads 0Wired to DO instead of AOInspect jumper on module pinoutEnsure A0 connects to AO (Analog Output).

Engineering Challenges & Upgrades

Take your build to the next level with these progressive engineering upgrades:

Level 1Double-Clap Lamp Filter

Require two claps spaced 200–700ms apart so accidental single bumps never flip the light.

Level 25-LED Sound Level Bar

Drive 5 LEDs proportionally to live room volume as an instant decibel meter.

Level 3Matrix Audio Visualizer

Send live sound peaks to the Project 14 MAX7219 8×8 matrix display.