In this article, we connect an KY-031 Knock Sensor to an Arduino Uno
The KY-031 Knock Sensor module is a vibration sensor that sends a signal when a knock/tap is detected on the module.
This module consists of a spring-based sensor, a 10K resistor and a header. The spring sends a high signal when a vibration is detected.
Parts Required
You can connect to the module using dupont style jumper wire.
This should work with other Arduino board – I have tried an Uno and Mega
Name | Link | |
Arduino Uno | ||
37 in one sensor kit | ||
Connecting cables |
Schematic/Connection
ARDUINO | SENSOR |
---|---|
Pin 2 | Signal |
5 V | +V |
GND | GND |
Code Example
The following code example will switch the onboard LED the Arduino when the module is tapped
int LedPin = 13 ;// define LED Interface int ShockPin = 2; // define the percussion Sensor Interface int val ;// define numeric variables val void setup () { pinMode (LedPin, OUTPUT) ;// define LED as output interface pinMode (ShockPin, INPUT) ;// define knock sensor output interface } void loop () { val = digitalRead (ShockPin) ;// read ShockPin if (val == HIGH) // When the knock switch is triggered the LED flashes { digitalWrite (LedPin, LOW); } else { digitalWrite (LedPin, HIGH); } }
Serial Monitor Output
N/A
Links
https://github.com/getelectronics/ArduinoCode/tree/main/37%20Sensor%20Kit/KY-031_Knock_Sensor