Clap switch on an Arduino Uno
A KY-038 sound sensor: clap and the lamp toggles. A clap is a burst of pulses on D0, and A0 barely moves, because there is no amplifier. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
BeginnerRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Clap switch: clap once and the lamp comes on, clap again and it goes off.
//
// pin 2 the KY-038's D0 A0 its A0
// pin 9 the lamp, through 220 ohm to GND
// + -> 5 V, G -> GND
//
// The KY-038 has no amplifier. Its microphone's signal is a few millivolts on
// top of half the supply, and the comparator on the board lets D0 go high on
// every instant that signal pokes above the trimmer. So a clap is not one
// clean high on D0: it is a burst of dozens of pulses over a tenth of a
// second, and a sketch that toggles on every rising edge toggles dozens of
// times.
//
// # Latch the first edge, then stop listening
//
// The fix is the same one a pushbutton needs: take the first rising edge and
// ignore D0 for a while after it. 300 ms is longer than any clap rings and
// shorter than two claps in a row.
//
// # And what A0 says
//
// Once a second the sketch prints the lowest and highest A0 it saw. In a quiet
// room they are the same number, about 512. Drag the level slider up and even
// "very loud" moves them by only a few counts either side: that is what an
// unamplified microphone gives a 10-bit ADC. The clap button is the thing that
// reaches the trimmer.
const int SOUND_DO = 2;
const int SOUND_AO = A0;
const int LAMP = 9;
const unsigned long QUIET_MS = 300;
bool lampOn = false;
bool wasHigh = false;
unsigned long lastClap = 0;
int lowest = 1023;
int highest = 0;
unsigned long lastReport = 0;
void setup() {
pinMode(SOUND_DO, INPUT);
pinMode(LAMP, OUTPUT);
digitalWrite(LAMP, LOW);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: clap switch");
Serial.println("press clap on the sensor");
}
void loop() {
unsigned long now = millis();
bool high = digitalRead(SOUND_DO) == HIGH;
if (high && !wasHigh && now - lastClap > QUIET_MS) {
lastClap = now;
lampOn = !lampOn;
digitalWrite(LAMP, lampOn ? HIGH : LOW);
Serial.println(lampOn ? "clap: lamp on" : "clap: lamp off");
}
wasHigh = high;
int level = analogRead(SOUND_AO);
if (level < lowest) {
lowest = level;
}
if (level > highest) {
highest = level;
}
if (now - lastReport >= 1000) {
lastReport = now;
Serial.print("A0 over the last second: ");
Serial.print(lowest);
Serial.print(" to ");
Serial.println(highest);
lowest = 1023;
highest = 0;
}
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Sound sensor, KY-038
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 9; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 2; Sound sensor, KY-038 pin DO
- Arduino Uno R3 pin 5V; Sound sensor, KY-038 pin VCC
- Ground: Arduino Uno R3 pin GND; Sound sensor, KY-038 pin GND; LED, yellow pin C
- Arduino Uno R3 pin A0; Sound sensor, KY-038 pin AO
- Resistor, 220 Ω pin 2; LED, yellow pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.