กลับหน้าหลัก
views
How to Use ESP32 with KY-002 Vibration Sensor Module Read Vibration Data
Last updated on

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 PinESP32 PinDescription
+ (VCC)VIN (5V)Power supply
- (GND)GNDGround
S (Signal)D22Digital signal output

![Circuit diagram showing ESP32 connected to KY-002 and LED on breadboard]

Connect the LED as follows:

LED PinESP32 PinDescription
Long leg (Anode)D23Through 220Ω resistor
Short leg (Cathode)GNDCommon 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_PIN to D3 or D4 and LED_PIN to D7 or D8
  • Arduino Uno/Nano: Use analog pins A0-A5 for Signal and digital pins 2-13 for LED

Upload and Test Steps

  1. Open Arduino IDE, copy the code above and paste
  2. Select your board at Tools → Board → choose your board
  3. Select connected Port at Tools → Port
  4. Click Upload to flash code to the board
  5. When done, open Serial Monitor
  6. Set baud rate to 9600
  7. 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

ProjectHow to Extend
Door opening detectionMount sensor on door, vibration means someone opened it
Movement detectionAttach to equipment you want to alert if moved
Fan speed measurementUse 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

จ้างทำโปรเจคเลย

ความคิดเห็น