Project

โ— Beginner ยท SW-420 Vibration Sensor Module

Detecting Vibration with SW-420 on ESP32

Overview

Learn how to use the SW-420 vibration sensor with an ESP32 to detect shaking or movement. This project is useful for tamper alarms, vibration alerts, and simple security systems.

Instructions

Step by step

1. Connect the SW-420 VCC pin to 3.3V. 2. Connect the SW-420 GND pin to GND. 3. Connect the SW-420 DO pin to D15. 4. Run the code and open the Serial Monitor. 5. Click or toggle the SW-420 sensor in the simulator. 6. When vibration is detected, the output should print "Vibration detected!". 7. When stable, the output should print "No vibration".

Code

Source code

code
const int vibrationPin = 15;

void setup() {
  Serial.begin(115200);
  pinMode(vibrationPin, INPUT);

  Serial.println("SW-420 Vibration Sensor Test");
}

void loop() {
  int vibrationState = digitalRead(vibrationPin);

  if (vibrationState == HIGH) {
    Serial.println("Vibration detected!");
  } else {
    Serial.println("No vibration");
  }

  delay(500);
}

Related

More projects with this sensor