ESP32 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.
π Quick Links
π KY-002 Price
βΉοΈ About KY-002 Vibration Switch Module
The KY-002 is a vibration switch module designed to detect shocks and movement. Using a conductive spring mechanism, it closes the circuit momentarily upon detecting vibrations, sending a digital signal to microcontrollers like ESP32 and Arduino. This makes it ideal for motion detection in security systems, interactive devices, and monitoring equipment.
β‘ Key Features #
- Shock & Vibration Detection β Activates when movement or impact is detected.
- Simple Digital Output β Easily interfaces with ESP32, Arduino, and other MCUs.
- Compact & Low Power β Suitable for battery-powered projects.
- Versatile Applications β Used in security alarms, motion-sensitive devices, and monitoring systems.
With its reliable motion detection capabilities, the KY-002 is an excellent choice for interactive and security-based applications. π
βοΈ KY-002 Sensor Technical Specifications
Below you can see the KY-002 Vibration Switch Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensorβs datasheet.
- Type: module
- Protocol: Digital
- Operating Voltage: 3.3V to 5V
- Dimensions: 18.5mm x 15mm
- Detection Mechanism: Conductive spring
- Output Type: Digital signal
π KY-002 Sensor Pinout
Below you can see the pinout for the KY-002 Vibration Switch Module. 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 (-):
Connects to Ground (GND).Pin (middle):
Connects to VCC (3.3V or 5V).Pin (S):
Outputs a digital signal when vibration is detected.
𧡠KY-002 Wiring with ESP32
Below you can see the wiring for the KY-002 Vibration Switch Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
KY-002 Pin (-):
Connect to ESP32GND
.KY-002 Pin (middle):
Connect to ESP323.3V
or5V
.KY-002 Pin (S):
Connect to a GPIO pin on ESP32 (e.g.,GPIO4
).
π οΈ KY-002 Vibration Switch Module Troubleshooting
This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug 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.
π» Code Examples
Below you can find code examples of KY-002 Vibration Switch Module with ESP32 in several frameworks:
If you encounter issues while using the KY-002 Vibration Switch Module, check the Common Issues Troubleshooting Guide.

ESP32 KY-002 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the KY-002 Vibration Switch Module:
#define VIBRATION_PIN 10
#define LED_PIN 13
void setup() {
pinMode(VIBRATION_PIN, INPUT);
digitalWrite(VIBRATION_PIN, HIGH); // Enable internal pull-up resistor
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
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);
}
This Arduino code sets up the KY-002 vibration sensor on pin 10 and an LED on pin 13. When a vibration is detected, the sensor outputs a LOW signal, triggering the LED to flash and a message to be printed to the serial monitor.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP32 KY-002 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the KY-002 Vibration Switch Module, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#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.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESP32 KY-002 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the KY-002 Vibration Switch Module
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.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.

ESP32 KY-002 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
ESP32 KY-002 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the KY-002 Vibration Switch Module:
#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.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.

ESP32 KY-002 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the KY-002 Vibration Switch Module with your ESP32.
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.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of KY-002 Vibration Switch Module, its pinout, connection with ESP32 and KY-002 Vibration Switch Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.