Keyes KY-031 Arduino Knock Impact Sensor: Manual and Tutorial


KY-031 Knock SensorThis device is a lot like the KY-002 Vibration Sensor in that it registers a jolt or a shock.  About the only difference is that my testing showed it to be a less sensitive.

In other words, it takes a more powerful knock or jolt to register an impact.

The device itself is nothing more than a switch with a pull-up resistor.

Where to Get a KY-031

The Keyes family of sensors are available all over the internet.  You can get one of these sensors for way less than a Starbucks coffee.  Some of the online locations where you can find this device are as follows:


Ali Express

Keyes KY-031 Knock Sensor Pin Outs

Three connections to your Arduino is all you need.  The pins on the knock sensor that you will connect to are shown in the picture below:

KY-031 Knock Sensor Pin OutsKY-031 Knock Sensor Module Schematic

The device is configured as a normally open switch that is held high with a pull up resistor and is sent to ground when a knock, shock or jolt is registered.

KY-031 Knock Sensor Schematic

KY-031 Arduino Knock Sensor Tutorial

Connect Your Arduino to the Knock Sensor


Copy, Paste and Upload the Arduino Knock Sensor Sketch

The knock sensor switch will go only go low for a split second.  The key to put it to use is to have a clear understanding of what it is you’re trying to accomplish.  For example:

  • Your objective may be to allow operation of some control only when you deem it safe to do so.   In which case, you will want to ensure that there has been no registered shock within a given time period.  This tutorial sketch accommodates that by ensuring that there has been no knock for at least 750 mSec before declaring that there are “no knocks”.
  • On the other hand, you may wish to do something specific like turning a circuit off.  In this particular case, you simply take action immediately.  In this sketch, that is precisely what occurs.  We declare “KNOCK, KNOCK” the moment we see it.
//Henry's Bench
//KY-031 Knock Sensor Tutorial

int knockPin = 10; // Use Pin 10 as our Input
int knockVal = HIGH; // This is where we record our shock measurement
boolean bAlarm = false;

unsigned long lastKnockTime; // Record the time that we measured a shock

int knockAlarmTime = 500; // Number of milli seconds to keep the knock alarm high

void setup ()
  pinMode (knockPin, INPUT) ; // input from the KY-031
void loop ()
  knockVal = digitalRead (knockPin) ; // read KY-031 Value
  if (knockVal == LOW) // If we see a knock
    lastKnockTime = millis(); // record the time of the shock
    // The following is so you don't scroll on the output screen
    if (!bAlarm){
      Serial.println("KNOCK, KNOCK");
      bAlarm = true;
    if( (millis()-lastKnockTime) > knockAlarmTime  &&  bAlarm){
      Serial.println("no knocks");
      bAlarm = false;

Verify Your Tutorial Sketch

With your Arduino running,  go ahead a vigorously tap the sensor on your desk.  Do it  a few times.  Your output will look something like the picture below:

KY-031 Knock Sensor Tutorial Output

One Comment

  1. tim January 6, 2016

Leave a Reply

Your email address will not be published. Required fields are marked *