How to Use ESP32 with KY-002 Vibration Sensor Module Read Vibration Data
How to Use ESP32 with KY-002 Vibration Sensor Module Read Vibration Data
The KY-002 is a digital vibration sensor module that works simply and costs cheap. It is suitable for learning the basics of vibration detection, or for projects that need to know whether movement or vibration has occurred.
Required Components
- ESP32 board (or ESP8266)
- KY-002 vibration sensor module
- Red LED 5mm
- 220 ohm resistor
- Breadboard
- Jumper wires (Male-Male, Male-Female, Female-Female)
- Micro USB cable for upload
Wiring Diagram
Start by connecting the KY-002 sensor. This module has 3 pins:
| KY-002 Pin | ESP32 Pin | Description |
|---|---|---|
| + (VCC) | VIN (5V) | Power supply |
| - (GND) | GND | Ground |
| S (Signal) | D22 | Digital signal output |
![Circuit diagram showing ESP32 connected to KY-002 and LED on breadboard]
Connect the LED as follows:
| LED Pin | ESP32 Pin | Description |
|---|---|---|
| Long leg (Anode) | D23 | Through 220Ω resistor |
| Short leg (Cathode) | GND | Common ground |
![LED circuit with 220 ohm resistor showing long and short legs]
Arduino Code for Reading Vibration Sensor
// Define used pins
const int SENSOR_PIN = 22; // Signal pin from KY-002
const int LED_PIN = 23; // LED control pin
// State variable
int vibrationState = 0; // Detection state (0 = normal, 1 = detected)
void setup() {
Serial.begin(9600); // Start Serial Monitor at 9600 baud
// Set pin modes
pinMode(SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println("Ready to detect vibration");
}
void loop() {
// Read sensor value (HIGH = detected, LOW = no vibration)
vibrationState = digitalRead(SENSOR_PIN);
if (vibrationState == HIGH) {
// If vibration detected, turn LED on for 3 seconds
digitalWrite(LED_PIN, HIGH);
Serial.println("Vibration detected!");
delay(3000); // Delay 3 seconds
digitalWrite(LED_PIN, LOW);
} else {
// No vibration
Serial.println("No vibration");
digitalWrite(LED_PIN, LOW);
}
delay(100); // Short delay between loops
}
Points to Adjust Based on Your Board
- ESP8266 (NodeMCU): Change
SENSOR_PINtoD3orD4andLED_PINtoD7orD8 - Arduino Uno/Nano: Use analog pins A0-A5 for Signal and digital pins 2-13 for LED
Upload and Test Steps
- Open Arduino IDE, copy the code above and paste
- Select your board at Tools → Board → choose your board
- Select connected Port at Tools → Port
- Click Upload to flash code to the board
- When done, open Serial Monitor
- Set baud rate to 9600
- The output will show “No vibration” when idle
![Serial Monitor screenshot showing output results]
Testing Detection
Simple test method is to tap the table lightly with your hand or tap on the breadboard. If the sensor detects it
- LED will light up for 3 seconds
- Serial Monitor will display “Vibration detected!”
If there is no response, the vibration is not strong enough. Try tapping harder or directly on the sensor.
How KY-002 Works
The KY-002 module uses a ball switch inside. There is a small metal ball inside the switch. When vibration is strong enough, the ball will hit the wall and close the electrical circuit, causing the output to change from LOW to HIGH.
Important limitations to know:
- Outputs only HIGH/LOW, cannot measure vibration intensity
- Vibration must exceed the threshold to trigger
- Sensitivity depends on mounting angle and vibration direction
Practical Application Examples
| Project | How to Extend |
|---|---|
| Door opening detection | Mount sensor on door, vibration means someone opened it |
| Movement detection | Attach to equipment you want to alert if moved |
| Fan speed measurement | Use multiple units to measure rotation timing |
Summary
KY-002 is an easy-to-understand sensor with uncomplicated operation. It is suitable for those starting to learn about vibration detection. The key point is to understand that this sensor provides digital output, not analog. If you need to measure vibration intensity, choose a different module type instead.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย