Skip to content

Instantly share code, notes, and snippets.

@cmoz
Created March 23, 2022 14:23
Show Gist options
  • Save cmoz/d6e5e0dc249a2ce05bdddd8391bb0b1c to your computer and use it in GitHub Desktop.
Save cmoz/d6e5e0dc249a2ce05bdddd8391bb0b1c to your computer and use it in GitHub Desktop.
Basic sound sensor with clap detection
#define sensorPin 7
// Variable to store the time when last event happened
unsigned long lastEvent = 0;
void setup() {
pinMode(sensorPin, INPUT); // Set sensor pin as an INPUT
Serial.begin(9600);
}
void loop() {
// Read Sound sensor
int sensorData = digitalRead(sensorPin);
// If pin goes LOW, sound is detected
if (sensorData == LOW) {
// If 25ms have passed since last LOW state, it means that
// the clap is detected and not due to any spurious sounds
if (millis() - lastEvent > 25) {
Serial.println("Clap detected!");
}
// Remember when last event happened
lastEvent = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment