KY-031 Knock Sensor Module

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.

KY-031 Knock Sensor Module image
KY-031 · Digital
Digital
Interface
3pins
Connections
3.3-5V
Supply
$1
Typical price
On this page

KY-031 pinout

3 pins · Digital

The KY-031 is a 3-pin knock/vibration sensor module:

View:
KY-031 Knock Sensor Module pinout
PinTypeDescriptionNotes
-PowerGround connection
middlePowerPower supply3.3V or 5V
SCommunicationSignal outputHIGH pulse on knock detection
  • 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

Wiring the KY-031 to ESP32

3 connections · all required

To interface the KY-031 with an ESP32 for knock detection:

KY-031 Knock Sensor Module wiring with ESP32
KY-031 pinESP32 pinPurpose
-GNDGround
middle3.3V or 5VPower supply
SGPIO16Digital 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 code examples

5 platforms
Platform:

KY-031 Arduino example

Copy
int knock = 16; // KY-031 signal pin (GPIO16, matches the wiring above)
int value;

void setup() {
    pinMode(knock, INPUT_PULLUP); // Spring switch closes to GND on a knock
    Serial.begin(115200);
    Serial.println("KY-031 Knock test");
}

void loop() {
    value = digitalRead(knock);
    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.

KY-031 ESP-IDF example

Copy
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define KNOCK_SENSOR_PIN GPIO_NUM_16

static void IRAM_ATTR knock_isr_handler(void* arg);

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));
    }
}

static 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.

KY-031 ESPHome example

Copy
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.

KY-031 PlatformIO example

Copy
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
src/main.cppCopy
#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.

KY-031 MicroPython example

Copy
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.

KY-031 specifications

From the datasheet
Operating Voltage
3.3V to 5V
Output Type
Digital
Dimensions
19mm x 15mm x 10mm
Weight
2g

About the KY-031

The KY-031 uses the same kind of spring-in-a-can mechanism as the KY-002 vibration switch - a conductive spring inside a metal shell that taps the wall and briefly closes the circuit on impact - but sellers and hobbyist writeups consistently describe it as the less sensitive of the two, tuned to need a real knock rather than a light bump or vibration. There’s no comparator or potentiometer on the board to confirm that difference electrically, so treat it as a difference in the switch’s internal spring tension rather than anything adjustable from outside.

Like any mechanical contact, the connection bounces: a single knock on the enclosure typically produces several rapid HIGH/LOW transitions within milliseconds rather than one clean edge, so debounce it in software (a short delay or an edge-count window) the same way you would a pushbutton. It runs on the same 3.3-5V logic as the rest of the ESP32’s GPIOs, and since it only reports “impact happened” with no measure of force or direction, it’s a trigger for taps and hits, not a stand-in for an accelerometer.

KY-031 troubleshooting

2 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.

Where to buy the KY-031

KY-031 Knock Sensor Module
KY-031 Knock Sensor Module
$1per unit, typical
Amazon and AliExpress links are affiliate links - buying through them supports the site at no extra cost to you.

Resources