Interfacing Knock Sensor Module KY-031 in Raspberry Pi

Learn Interfacing Knock Sensor Module KY-031 in Raspberry Pi. Knock sensor module KY-031 is similar to Vibration module. The major difference between these two is its sensitivity. Vibration sensor detects even a small shock, whereas knock sensor requires a powerful knock or a jolt to activate. To check if the module has detected a knock or jolt, we will light up LED. So, Let’s start.

Step 1: Required Components

Knock Sensor Module KY-031 x 1
Generic LED x 1
Breadboard x 1
Raspberry Pi x 1
Jumper Wires

Step 2: Circuit Time

Firstly, connect LED to GPIO pin 27 of Raspberry Pi. Now, connect the signal pin of Knock Sensor module to GPIO pin 17 of Raspberry Pi. Then, connect VCC pin and GND pin of Knock sensor module to +5v and GND pin of Raspberry Pi.

Step 3: Code Time

The code for Knock Sensor Module KY-031 in Raspberry Pi is simple. Firstly, declare sensor pin and LED pin as usual. Set LED pin as output and sensor pin as input and use pull up down resistor. Event detect will detect the signal (falling edge) of the sensor. If the signal is detected then call is made to active function and LED will light up. The exception will detect keyboard interrupt and perform GPIO cleanup.

Step 4: Run code to Raspberry Pi

Run the Python program in your Raspberry Pi after setting components as per circuit diagram. When module detects any powerful knock or jolt, the closed circuit will be formed and LED will light up.

For running python code in Raspberry PI, visit:

For basic hardware information about Raspberry PI 3, Click here.

Leave a Reply