KY-002 Vibration Switch Module

The KY-002 is a vibration switch module that detects shocks and vibrations through a conductive spring mechanism. Upon movement, it sends a digital signal, making it suitable for motion detection applications in various electronic projects.

KY-002 Vibration Switch Module image
KY-002 · Digital
Digital
Interface
3pins
Connections
3.3-5V
Supply
$2
Typical price
On this page

KY-002 pinout

3 pins · Digital

The KY-002 is a 3-pin vibration/shock detection module:

View:
KY-002 Vibration Switch Module pinout
PinTypeDescriptionNotes
Pin (-)PowerGround connection
Pin (middle)PowerPower supply3.3V or 5V
Pin (S)CommunicationDigital signal outputGoes HIGH when vibration detected
  • Interface: Digital output (active high on vibration)

  • Detection: Conductive spring mechanism closes on impact

  • Operation: Momentary signal when shock/vibration occurs

  • Power: 3.3V or 5V operation

  • Low Power: Minimal current consumption when idle

  • Applications: Security alarms, motion detection, interactive devices

Wiring the KY-002 to ESP32

3 connections · all required

To interface the KY-002 with an ESP32 for vibration detection:

KY-002 Vibration Switch Module wiring with ESP32
KY-002 pinESP32 pinPurpose
Pin (-)GNDGround
Pin (middle)3.3V or 5VPower supply
Pin (S)GPIO4Digital input (any GPIO)
  • GPIO Selection: Any digital GPIO pin works, GPIO4 is just an example

  • Voltage: Use 3.3V for ESP32 compatibility

  • Debouncing: May require software debouncing for clean signal

  • Sensitivity: Fixed sensitivity, no adjustment possible

KY-002 code examples

5 platforms
Platform:

KY-002 Arduino example

Copy
#define VIBRATION_PIN 4 // KY-002 signal pin (GPIO4, matches the wiring above)
#define LED_PIN 2       // Onboard LED on most ESP32 dev boards

void setup() {
    pinMode(VIBRATION_PIN, INPUT_PULLUP); // Sensor closes to GND on vibration
    pinMode(LED_PIN, OUTPUT);
    Serial.begin(115200);
    Serial.println("KY-002 Vibration detection");
}

void loop() {
    int sensorValue = digitalRead(VIBRATION_PIN);
    if (sensorValue == LOW) {
        Serial.println("Vibration detected");
        digitalWrite(LED_PIN, HIGH);
        delay(100);
        digitalWrite(LED_PIN, LOW);
    }
    delay(100);
}

The sensor's signal pin connects to GPIO4 (matching the wiring above) and is read as a digital input with the internal pull-up enabled. On the ESP32, pull-ups are enabled with INPUT_PULLUP - the old AVR trick of writing HIGH to an input pin does nothing here. When the spring switch closes on vibration the line reads LOW and the onboard LED (GPIO2 on most ESP32 dev boards) flashes.

KY-002 ESP-IDF example

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

#define VIBRATION_PIN GPIO_NUM_4
#define LED_PIN GPIO_NUM_2

void app_main(void) {
    gpio_set_direction(VIBRATION_PIN, GPIO_MODE_INPUT);
    gpio_set_pull_mode(VIBRATION_PIN, GPIO_PULLUP_ONLY);
    gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
    printf("KY-002 Vibration detection\n");
    while (1) {
        int sensor_value = gpio_get_level(VIBRATION_PIN);
        if (sensor_value == 0) {
            printf("Vibration detected\n");
            gpio_set_level(LED_PIN, 1);
            vTaskDelay(pdMS_TO_TICKS(100));
            gpio_set_level(LED_PIN, 0);
        }
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

This ESP-IDF code configures GPIO4 as an input for the KY-002 vibration sensor and GPIO2 as an output for an LED. When a vibration is detected (sensor outputs LOW), the LED flashes, and a message is printed to the console.

KY-002 ESPHome example

Copy
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
    name: "KY-002 Vibration Sensor"
    filters:
      - delayed_on: 10ms
      - delayed_off: 10ms
    on_press:
      - then:
          - lambda: |-
              ESP_LOGD("sensor", "Vibration detected!");

This ESPHome configuration sets up the KY-002 vibration sensor as a binary sensor on GPIO4 with an internal pull-up resistor. It includes filters to debounce false triggers caused by short vibrations and logs when a vibration is detected.

KY-002 PlatformIO example

Copy
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
src/main.cppCopy
#include <Arduino.h>

#define VIBRATION_PIN 4
#define LED_PIN 2

void setup() {
    pinMode(VIBRATION_PIN, INPUT_PULLUP);
    pinMode(LED_PIN, OUTPUT);
    Serial.begin(115200);
    Serial.println("KY-002 Vibration detection started");
}

void loop() {
    if (digitalRead(VIBRATION_PIN) == LOW) {
        Serial.println("Vibration detected");
        digitalWrite(LED_PIN, HIGH);
        delay(100);
        digitalWrite(LED_PIN, LOW);
    }
    delay(100);
}

This PlatformIO code configures the KY-002 vibration sensor on GPIO4 and an LED on GPIO2. When the sensor detects vibration, it outputs a LOW signal, triggering an LED blink and logging a message to the serial monitor.

KY-002 MicroPython example

Copy
import machine
import time

VIBRATION_PIN = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
LED_PIN = machine.Pin(2, machine.Pin.OUT)

while True:
    if VIBRATION_PIN.value() == 0:
        print("Vibration detected")
        LED_PIN.on()
        time.sleep(0.1)
        LED_PIN.off()
    time.sleep(0.1)

This MicroPython script configures the KY-002 vibration sensor on GPIO4 and an LED on GPIO2. When a vibration is detected (LOW signal), the LED blinks, and a message is printed to the console.

KY-002 specifications

From the datasheet
Operating Voltage
3.3V to 5V
Dimensions
18.5mm x 15mm
Detection Mechanism
Conductive spring
Output Type
Digital signal

About the KY-002

The KY-002 is about as basic as motion detection gets: a small conductive spring suspended inside a metal can (sold under part numbers like SW-18015P), soldered to a 3-pin breakout. Shake it and the spring’s tip taps the can wall, briefly closing the circuit; the module just carries that raw switch out to a signal pin. Nothing here is analog and nothing measures how hard the impact was - the output is “something happened,” not a reading of intensity, direction, or frequency, so it’s not a substitute for an accelerometer if a project actually needs vibration data rather than a shock trigger.

The normally-open contact bounces on every hit, so a single knock on the enclosure usually produces several rapid transitions on the GPIO within milliseconds; debounce it in software the same way you would a mechanical button (a short delay or an edge-count window) or a single tap will register as several. It runs on the same 3.3-5V logic as the rest of the ESP32’s GPIOs, so no special interfacing is needed beyond that.

KY-002 troubleshooting

2 common issues

No Response from KY-002 Module

›

Issue: The module does not detect any vibrations or shocks.

Solutions:

  • Verify all connections are secure and correctly placed.
  • Ensure the module is receiving the appropriate voltage (3.3V or 5V).
  • Check the microcontroller's GPIO pin configuration in the code.

False Triggering of Vibration Detection

›

Issue: The module sends signals without any actual vibration.

Solutions:

  • Reduce environmental noise or interference that might affect the sensor.
  • Implement software debouncing to filter out spurious signals.
  • Ensure the module is securely mounted to prevent unintended movements.

Where to buy the KY-002

KY-002 Vibration Switch Module
KY-002 Vibration Switch Module
$2per unit, typical
Amazon and AliExpress links are affiliate links - buying through them supports the site at no extra cost to you.

Resources