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 the Vibration module. The major difference between these two is its sensitivity. The vibration sensor detects even a small shock, whereas the 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 an 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 the Knock Sensor module to GPIO pin 17 of Raspberry Pi. Then, connect the 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 a sensor pin and an LED pin as usual. Set LED pin as output and sensor pin as input and use pull up down resistor. The event detect will detect the signal (falling edge) of the sensor. If the signal is detected then the call is made to active function and LED will light up. The exception will detect the 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 the circuit diagram. When the 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: raspberrypi.org/documentation/usage/python/

Learn basic hardware information of Raspberry Pi 3.

Leave a Reply