ESP8266 Vibration Sensor Project
The ESP8266 Vibration Sensor project demonstrates how to interface a vibration sensor module (such as the popular SW‑420) with an ESP8266‑based board (like NodeMCU or Wemos‑D1). The sensor outputs a digital HIGH when it detects vibration or shock, and the ESP8266 can trigger LEDs, buzzers, or send alerts over Wi‑Fi, making it suitable for security systems, machinery monitoring, and impact‑detection applications.
How the Vibration Sensor Works
A typical vibration sensor module (e.g. SW‑420) contains a small spring‑weight mechanism that moves when the sensor experiences vibration or shock. This movement closes an internal switch, pulling the digital output pin HIGH. The pin returns LOW when the vibration stops. The ESP8266 reads this state with digitalRead() and can act immediately—for example, lighting an LED, sounding a buzzer, or sending an HTTP request to a cloud service or alert platform.
Many modules also include a sensitivity‑adjust potentiometer and a small LED that lights when vibration is detected, helping you tune the sensor’s response to your environment (e.g. gentle bumps vs strong impacts).
Components Needed
- ESP8266 development board (e.g. NodeMCU‑v2 or Wemos‑D1)
- Vibration sensor module (e.g. SW‑420 style)
- LED (optional, for visual indication)
- Passive buzzer (optional, for alarm)
- 220 Ω resistor (for LED)
- Jumper Wires
- Breadboard
Circuit Diagram
Circuit Setup
1. Connect Vibration Sensor to ESP8266
Most vibration sensor modules expose three pins:
- VCC → 3.3 V or 5 V on the ESP8266 (check module; SW‑420 typically runs on 3.3–5 V).
- GND → GND on the ESP8266.
- OUT (digital output) → GPIO 2 (D4 on NodeMCU, or any digital pin) on the ESP8266.
Ensure a common ground between the sensor and the ESP8266 so the logic levels are referenced correctly.
2. Optional Indicator Devices
- LED: Connect an LED anode to GPIO 5 (D1 on NodeMCU) through a 220 Ω resistor; connect the cathode to GND.
- Buzzer: Connect the buzzer’s positive leg to GPIO 13 (D7 on NodeMCU) and negative leg to GND for a simple alarm when vibration is detected.
Instructions
1. Software Setup
Configure the Arduino IDE for ESP8266 (e.g. board: NodeMCU‑v2). Initialize serial communication at 9600 baud for debugging and set the vibration sensor pin as an input using pinMode(vibrationPin, INPUT). If you want remote alerts, add Wi‑Fi configuration (WiFi.begin(ssid, password)) and optional HTTP/MQTT code.
2. Operation in loop()
In the loop() function, read the vibration sensor state with digitalRead(vibrationPin). When the value is HIGH, treat it as a vibration event; light the LED, sound the buzzer, and optionally send a Wi‑Fi request or MQTT message.
3. Sensor Sensitivity and Behavior
Most modules include a small potentiometer to adjust sensitivity. Turn it clockwise to make the sensor more sensitive (it triggers on smaller vibrations) or counterclockwise to make it less sensitive (it responds only to larger shocks). Avoid placing the sensor on surfaces that constantly vibrate (e.g. large motors or fans) to reduce false alarms.
4. Power and Mounting Considerations
Power the ESP8266 and sensor from a stable 3.3–5 V source. Mount the vibration sensor firmly to the object or surface you want to monitor (e.g. a door, window, machine chassis, or wall), using screws or adhesive tape so that vibrations transfer well to the sensor without mechanical backlash.
C++ Example Code (ESP8266 Vibration Sensor with LED & Buzzer)
#include <ESP8266WiFi.h>
// Replace with your Wi‑Fi credentials (if using Wi‑Fi alerts)
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Vibration sensor and indicator pins
const int vibrationPin = 4; // D4 on NodeMCU
const int ledPin = 5; // D1 on NodeMCU
const int buzzerPin = 13; // D7 on NodeMCU
// Optional: delay to avoid repeated alerts in a short time
unsigned long lastAlert = 0;
const unsigned long alertInterval = 5000; // 5 seconds
void setup() {
Serial.begin(9600);
// Optional: connect to Wi‑Fi
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) delay(1000);
pinMode(vibrationPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
Serial.println("=== ESP8266 VIBRATION SENSOR READY ===");
}
void loop() {
int vibState = digitalRead(vibrationPin);
if (vibState == HIGH) {
// Vibration detected
unsigned long now = millis();
if (now - lastAlert >= alertInterval) {
// Avoid spamming alerts
Serial.println("VIBRATION DETECTED!");
// Optional: send Wi‑Fi/MQTT alert here
// e.g., HTTP POST or MQTT publish
lastAlert = now;
}
// Light LED and buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
} else {
// No vibration
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
delay(50); // Small debounce delay
}
Applications
Security Systems: Use the sensor on doors, windows, or safes to detect forced entry or tampering and send alerts to a phone or logging system.
Industrial Monitoring: Mount the sensor on machinery to detect abnormal vibration patterns that indicate wear, misalignment, or failure, and trigger maintenance notifications.
Home Automation and IoT: Integrate the sensor with Wi‑Fi services (ThingSpeak, Blynk, or IFTTT) so that vibration events send push notifications, SMS, or email alerts, or turn on lights or cameras when impacts are detected.
Notes
Digital Output Behavior
The vibration sensor outputs a simple digital signal (HIGH or LOW) when the internal spring‑weight switch closes. This makes it easy to interface with ESP8266 but does not give a fine‑grained measure of vibration strength; for that level of detail, you would typically use an accelerometer (e.g. MPU‑6050) instead.
Serial and Debugging
Use the Serial Monitor (9600 baud) to observe when “VIBRATION DETECTED!” is printed and to verify that the LED and buzzer respond correctly. This helps you tune the sensitivity and check for false triggers caused by nearby machines or human movement.
Remote Alert Extension
You can extend this project by connecting the ESP8266 to Wi‑Fi and sending vibration events to a cloud dashboard or alert service (e.g. ThingSpeak, Blynk, or IFTTT) so that you receive notifications on your phone even when you are away from the sensor location.