Sound-Activated LED with Arduino
Detect a clap or loud noise with a KY-038 sound sensor and flash an LED. Learn digital input and simple if-logic.
Beginner20 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
sound_led.ino
// Flash an LED when the KY-038 detects sound above its threshold.
// Turn the module's potentiometer to set sensitivity (the module's own
// LED lights when it triggers).
const int soundPin = 2; // KY-038 DO (digital out)
const int ledPin = 13; // LED (and onboard LED)
void setup() {
pinMode(soundPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// DO reads HIGH when sound exceeds the threshold on most KY-038 modules.
// If your LED behaves backwards, swap HIGH and LOW below.
if (digitalRead(soundPin) == HIGH) {
digitalWrite(ledPin, HIGH);
delay(200); // keep the LED on briefly so the flash is visible
} else {
digitalWrite(ledPin, LOW);
}
}
Steps
- Wire the KY-038: + to Arduino 5V, GND to GND, DO to D2.
- Wire the LED on D13 through a 220Ω resistor, cathode to GND.
- Upload the sketch.
- Clap near the sensor. Adjust the module's potentiometer until the LED reacts cleanly.