Home SensorsKit Arduino and KY-031 Knock Sensor

Arduino and KY-031 Knock Sensor

by shedboy71

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



Pin 2 Signal
5 V +V


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);
    digitalWrite (LedPin, HIGH);


Serial Monitor Output






You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More