const int knockPin = 2; // Connect the KY-031 to Arduino Pin 2
void setup() {
pinMode(knockPin, INPUT); // Set KY-031 pin as an input
Serial.begin(9600); // Initialize serial communication for debugging (optional)
}
void loop() {
int sensorValue = digitalRead(knockPin); // Read the value from KY-031
if (sensorValue == LOW) { // If the sensor value is LOW (knock detected)
Serial.println(“Knock detected!”); // Print a message to the Serial Monitor (optional)
// Additional actions or functions you want to perform when a knock is detected
delay(1000); // To avoid multiple detections in quick succession
}
}