KY-031 Knock Sensor Module

View on Amazon
Overview
About KY-031 Knock Sensor Module
The KY-031 Knock Sensor Module is designed to detect knocks or vibrations. When subjected to such physical impacts, the sensorโs internal contacts momentarily connect, sending a digital signal. This module is ideal for projects that require tap or knock detection, such as interactive interfaces or security systems.
Get Your KY-031
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
KY-031 Specifications
Complete technical specification details for KY-031 Knock Sensor Module
๐ Technical Parameters
KY-031 Pinout
The **KY-031** is a 3-pin knock/vibration sensor module:
Visual Pinout Diagram

Pin Types
Quick Tips
**Interface**: Digital vibration sensor,๐จ **Sensor**: Vibration switch (spring-based contact mechanism)
**Signal**: Outputs HIGH pulse when knock/vibration detected,๐ฏ **Power**: 3.3V to 5V operation
**Sensitivity**: Detects sudden impacts and vibrations,๐ฎ **Applications**: Knock detection, tap interfaces, vibration alarms, security triggers
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 - | Power | Ground connection | |
2 middle | Power | Power supply | 3.3V or 5V |
3 S | Communication | Signal output | HIGH pulse on knock detection |
Wiring KY-031 to ESP32
To interface the **KY-031** with an **ESP32** for knock detection:
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| KY-031 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 - Required | GND | Ground | |
2 middle Required | 3.3V or 5V | Power supply | |
3 S Required | GPIO16 | Digital input (any GPIO) |
**Detection**: Momentary HIGH pulse when knock/vibration occurs
**Debouncing**: Software debouncing recommended to filter multiple triggers
**Pull-down**: May require pull-down resistor for stable LOW state
**Sensitivity**: Varies with impact force - stronger knocks produce more reliable triggers
**Mounting**: Secure mounting improves sensitivity and consistency
**False Triggers**: May respond to nearby vibrations or bumps
**LED Indicator**: Some versions include LED that lights on detection
KY-031 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The sensor does not detect knocks or vibrations.
Solutions:
- Verify all wiring connections are secure and correctly placed.
- Ensure the sensor is receiving the appropriate voltage (3.3V or 5V).
- Test the sensor with a known good microcontroller pin to rule out pin issues.
Issue: The sensor outputs signals without any physical impact.
Solutions:
- Check for external vibrations or noise that might be causing false triggers.
- Implement software debouncing to filter out spurious signals.
- Ensure the sensor is mounted securely to prevent unintended movements.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
KY-031 Programming Examples
Ready-to-use code examples for different platforms and frameworks
int knock = 10; // Sensor input pin
int value; // Variable to store sensor value
void setup() {
pinMode(knock, INPUT); // Initialize sensor pin as input
Serial.begin(9600); // Initialize serial communication
Serial.println("KY-031 Knock test");
}
void loop() {
value = digitalRead(knock); // Read sensor value
if (value == LOW) { // If knock detected
Serial.println("Knock recognized");
delay(200); // Delay to avoid multiple detections
}
}This Arduino code initializes the sensor input pin and serial communication. In the loop, it continuously reads the sensor value and prints "Knock recognized" to the serial monitor when a knock is detected.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define KNOCK_SENSOR_PIN GPIO_NUM_16
void app_main(void) {
gpio_config_t io_conf = {
.intr_type = GPIO_INTR_NEGEDGE, // Interrupt on falling edge
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << KNOCK_SENSOR_PIN),
.pull_up_en = GPIO_PULLUP_ENABLE
};
gpio_config(&io_conf);
gpio_install_isr_service(0);
gpio_isr_handler_add(KNOCK_SENSOR_PIN, knock_isr_handler, NULL);
printf("KY-031 Knock Sensor Test\n");
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void IRAM_ATTR knock_isr_handler(void* arg) {
printf("Knock detected!\n");
}This ESP-IDF code configures GPIO16 as an input with an interrupt on the falling edge. When a knock is detected, an interrupt service routine (ISR) is triggered, printing "Knock detected!" to the console.
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT_PULLUP
name: "KY-031 Knock Sensor"
filters:
- delayed_off: 200msThis ESPHome configuration sets up a binary sensor for the KY-031 knock sensor connected to GPIO16. It uses a delayed_off filter to prevent multiple detections from a single knock.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduinomain.cpp
#include <Arduino.h>
#define KNOCK_SENSOR_PIN 16
void setup() {
pinMode(KNOCK_SENSOR_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-031 Knock Sensor Test");
}
void loop() {
if (digitalRead(KNOCK_SENSOR_PIN) == LOW) {
Serial.println("Knock detected!");
delay(200); // Debounce delay
}
delay(50);
}This PlatformIO code sets up GPIO16 as an input with a pull-up resistor for the KY-031 knock sensor. When a knock is detected, a message is printed to the serial monitor, and a debounce delay of 200ms prevents false triggers.
import machine
import time
KNOCK_SENSOR_PIN = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if KNOCK_SENSOR_PIN.value() == 0:
print("Knock detected!")
time.sleep(0.2) # Debounce delay
time.sleep(0.05)This MicroPython script configures GPIO16 as an input with a pull-up resistor for the KY-031 knock sensor. It continuously checks for knocks and prints a message when one is detected, with a debounce delay to prevent multiple detections from a single event.
Wrapping Up KY-031
The ESP32 KY-031 Knock Sensor Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the KY-031 into your ESP32 project and bring your ideas to life!








