ESP8266 - SW-420 Vibration Sensor

The SW-420 vibration sensor module reacts to shaking, knocking, or shock in whatever it is mounted on. It suits projects such as a simple burglar or tamper alarm, a package that reports rough handling, or a machine that needs a warning when it starts shaking abnormally.

This tutorial instructs you how to use the ESP8266 and a SW-420 vibration sensor to detect vibration. We will explore:

ESP8266 NodeMCU NodeMCU SW-420 vibration sensor

From there, the code can easily be extended to sound a buzzer, flash an LED, or send a notification whenever vibration is picked up.

Hardware Preparation

1×ESP8266 NodeMCU ESP-12F
1×Alternatively, ESP8266 D1 Mini NodeMCU ESP-12F
1×Micro USB Cable
1×Alternatively, ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×SW-420 Vibration Sensor Module
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of SW-420 Vibration Sensor

The SW-420 vibration sensor module is built to pick up shaking, knocking, or shock affecting whatever it is mounted on. Inside its metal case sits a small spring-based switch positioned close to a metal contact; an onboard LM393 comparator watches that switch and turns any disturbance into a clean digital signal, while a small onboard potentiometer lets you dial in how much shaking is needed before it trips.

The SW-420 Vibration Sensor Pinout

The SW-420 vibration sensor has three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • DO pin: is an output pin: LOW while everything is still and HIGH the moment vibration or shock is detected. This pin needs to be connected to ESP8266's input pin.
SW-420 Vibration Sensor Pinout
image source: diyables.io

The SW-420 vibration sensor module comes with two LED indicators:

  • One LED indicates the power status.
  • Another LED lights up whenever the DO pin goes HIGH, i.e. whenever vibration is detected.

How It Works

Here's how the sensor behaves:

  • While the module sits still, the internal spring switch stays open and the DO pin stays LOW.
  • The instant the module is shaken, knocked, or bumped, the spring switch touches the contact and the LM393 comparator drives the DO pin HIGH.
  • Turning the onboard potentiometer clockwise or counterclockwise raises or lowers how strong a shock must be before the output switches.

Wiring Diagram

  • The wiring diagram between ESP8266 and SW-420 vibration sensor
The wiring diagram between ESP8266 NodeMCU and SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For SW-420 Vibration Sensor

  • Initializes the ESP8266 pin to the digital input mode by using pinMode() function. For example, pin D7
pinMode(D7, INPUT);
  • Reads the state of the ESP8266 pin by using digitalRead() function.
int vibrationState = digitalRead(D7);

ESP8266 Code - Detecting the vibration

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-sw-420-vibration-sensor */ #define SENSOR_PIN D7 // The ESP8266 pin D7 connected to the DO pin of the SW-420 vibration sensor int last_state = LOW; // The last reading from the input pin, LOW means idle (no vibration) int cur_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the ESP8266's input pin cur_state = digitalRead(SENSOR_PIN); if (last_state == LOW && cur_state == HIGH) Serial.println("vibration detected"); else if (last_state == HIGH && cur_state == LOW) Serial.println("vibration stopped"); // save the current state for the next comparison last_state = cur_state; }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Tap or shake the SW-420 sensor
  • Check out the result on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
vibration detected vibration stopped vibration detected vibration stopped
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

From here, the sketch can be extended so the ESP8266 sounds a buzzer, lights an LED, or pushes a notification whenever vibration is picked up. For detailed instructions and additional information, please refer to the tutorials provided at the end of this guide.

Troubleshooting

If you encounter issues with the SW-420 vibration sensor not functioning correctly, try the following troubleshooting steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to make the sensor more or less sensitive if it triggers too easily or not at all.
  • Isolate it from ambient vibration: Mount the module away from motors, fans, or other sources of constant background vibration that could cause false triggers.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Ensure that the power supply is clean and stable for consistent readings.

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!