ESP32 KY-031 Knock Sensor Module
Overview
The KY-031 is a knock sensor module that detects physical impacts or vibrations. Upon detecting a knock, it outputs a digital signal, making it suitable for applications like interactive projects, security systems, or any setup requiring tap detection.
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.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
Pin S (Signal):
Outputs a digital signal when a knock is detected.Pin middle (+):
Connects to the power supply (3.3V or 5V).Pin - (GND):
Connects to the ground of the circuit.
Troubleshooting Guide
Common Issues
❌ No Response from Sensor
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.
⚠️ False Triggering
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
🔍 Serial Monitor
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.
⚡ Voltage Checks
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
Code Examples
Arduino Example
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.
ESP-IDF Example
#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.
ESPHome Example
binary_sensor:
- platform: gpio
pin:
number: GPIO16
mode: INPUT_PULLUP
name: "KY-031 Knock Sensor"
filters:
- delayed_off: 200ms
This 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 Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#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.
MicroPython Example
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.
Conclusion
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.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.