How to interface KY-031 knock sensor module with Arduino

How to use a knock sensor (ky-031) with Arduino & how a knock sensor works are some questions that may arise in your mind while programming an Arduino.

Hello guy! Welcome back to my blog today we are going to see about vibration sensors so let’s begin.

How Knock Sensor works??

knock sensor Arduino

Knock sensor detects tap or vibration using spring. Spring is the main element in this sensor. In the spring the vibration sensor acts as a switch that connects and disconnects the circuit. Normally this switch is open. 

knock sensor working

When we tap or when vibration occurs then the switch gets too close to the wire &this moment makes contact to close connection. When vibration is detected then the connection is closed and the sensor sends a HIGH signal as Output. Normally Output of this sensor is LOW.


The knock sensor has a pull-down resistor configuration and a pull-up resistor configuration. In the pull-down configuration sensor by default sends a LOW signal and changes its State to HIGH when a knock is detected. Whereas in pull-up configuration sensor by default sends a HIGH signal and changes its State to LOW when a knock is detected.

Application of such sensors is used to detect collision or fall detections of robot, security circuit, Doorknock detector, and balance control robots.

Parts Required

  • KY-031 (Knock sensor module)
  • Arduino
  • Jumper wires
  • LED
  • Breadboard

Schematic Diagram



Arduino knock

Connect the ground pin of the knock sensor to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 8. Also, connect the positive lead of LED to Arduino pin 11.

Code -


int ledpin=11;                   // Connect led pin at arduino pin 11
int knocksensor=8;               // Connect sensor pin at arduino pin 8
int sensorvalue;                 // initialze variable to store sensor data
void setup()
{
 pinMode(ledpin,OUTPUT);       // LED pin set to output
 pinMode(knocksensor,INPUT);  // knock sensor pin set to input
 Serial.begin(115200);        // set baudrate for Serial communication
}
void loop() 
{
 sensorvalue = digitalRead(knocksensor); // read values from sensor pin and store in variable
 if (sensorvalue==HIGH)      // if knock detected
 // if (sensorvalue==LOW)  // for pull-up configuration
 {
  digitalWrite(ledpin,HIGH);   // turn LED on
  Serial.println("Knock detected"); // Print message 
 }
 else{
  digitalWrite(ledpin,LOW);   // turn LED off
  Serial.println("no knock");       // Print message
 }
 delay(20);                   // delay 
}

First, initialize pins of knock sensors & LED. We have declared a variable name sensor value to read values from knock sensors. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT and LED as OUTPUT. We also configure the baud rate to 115200.

In the void loop, we continuously read values from the knock sensor and store values in variable name sensor value and we check here condition if it is HIGH or not. HIGH means knock has been detected, the sensor sends a HIGH signal to our Arduino which we printed as “Knock detected”. Here we have written code to turn ON led when a knock is detected. Whereas when the sensor is in pull-up configuration then the sensor will send a low signal in the presence of vibration or knock.

When the sensor doesn’t sense a knock or vibration then the sensor sends a LOW signal to Arduino which is printed as “No Knock“ and the LED connected to pin 11 will be turned OFF. Whereas when the sensor is in pull-up configuration, then the sensor will send a HIGH signal. We have introduced a delay of 20 milliseconds.

 Conclusion - 

Today we learn about what is Arduino knock sensor, how to use a knock sensor with Arduino & working principle of the knock sensor.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu