Introduction
In this tutorial, we will learn about the KY-031 module, and we will build a simple project using the KY-031 module and an Arduino.
Knock Sensor Module KY-031
The KY-031 Module will be our main component for this tutorial. This module has a spring-based sensor mounted on a breakout board with a resistor. Figure 1 shows the module as seen in fritzing.
Pin Out
The KY-031 module has three pins.
Pin | Description |
---|---|
(-) | GND |
middle | +5V |
S | Signal |
How it Works
The sensor of the module has a conductive material near a spring. This spring-based sensor act like a switch. When the module experiences a shock, the spring makes contact with the adjacent conductive material and closes the circuit.
Project - Arduino Shock Detector
After learning about the KY-031 module and how the vibration switch works, it is now time to build a project using the module. Our project will monitor if the module senses shock/vibration and will light-up the built-in LED of the Arduino Uno.
Components
For this project, we need the following components:
- Arduino Uno board (1 pc.)
- KY-031 Knock Sensor Module (1 pc.)
- Jumper wires
Wiring Diagram
Figure 2 shows the connection between the Arduino Uno and the KY-031 Knock Sensor Module.

The KY-031 module pins are connected to the Arduino Uno board as follows:
Component Pin | UNO Board Pin |
---|---|
(-) | GND |
middle | +5V |
S | 8 |
Code
Below is the Arduino sketch for our project. I have added comments to explain important parts of the code. Save the code as KY-031.ino and upload it to your Arduino board.
// Arduino and KY-031 module
void setup() {
pinMode(8, INPUT); // module is connected to pin 8
pinMode(13, OUTPUT); // set pin 13 to output which is also the built-in LED
}
void loop() {
if(digitalRead(8) == HIGH) { // module will make pin 8 HIGH if
// it detects vibration
digitalWrite(13, HIGH); // swtich ON the built-in LED
}
else digitalWrite(13, LOW); // switch off LED otherwise
}
Project Test
Apply power to your Arduino Uno board. Shake the KY-031 module and observe the built-in LED. As you shake the module, the LED should blink giving a visual indication that the module senses vibration.