Arduino Nano ESP32 - SW-420 Vibration Sensor

The SW-420 vibration sensor module can catch sudden shocks or shaking that occur around it, making it a handy building block for knock detectors, impact alarms, or any project that needs to notice a bump.

In this guide, we will learn how to use the SW-420 vibration sensor with the Arduino Nano ESP32. In detail, we will learn:

Arduino Nano ESP32 and SW-420 vibration sensor module

Once the basic sketch is working, you can adapt it to sound a buzzer, flash an LED, or send a notification whenever the module picks up a shock.

Hardware Preparation

1×Arduino Nano ESP32
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×Breadboard
1×Jumper Wires
1×Optionally, 5V Power Adapter
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

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 reacts to shaking, knocking, or shock in its surroundings. Inside the module sits a small spring-based switch positioned next to a metal contact; an onboard LM393 comparator watches this switch and produces a clean digital signal the moment vibration disturbs it. A trimmer potentiometer on the board lets you dial in how much shaking is needed before it reports a trigger.

Pinout

The SW-420 vibration sensor module has three pins:

  • VCC pin: connect to VCC (3.3V to 5V)
  • GND pin: connect to GND (0V)
  • DO pin: digital output pin, LOW while the module is at rest and HIGH the moment vibration or shock is picked up. Wire this pin to an input pin on the Arduino Nano ESP32.
SW-420 Vibration Sensor Pinout
image source: diyables.io

Two onboard LEDs help with a quick visual check:

  • A power LED stays lit whenever the module is powered.
  • A trigger LED lights up together with the DO pin whenever vibration is detected.

How It Works

The output pin follows the state of the internal switch:

  • At rest, the spring switch stays open, so the comparator holds the output pin LOW.
  • When a shock or vibration disturbs the switch, it momentarily closes, and the comparator drives the output pin HIGH.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW-420 Vibration Sensor

  • Configure the pin connected to the sensor's DO output as a digital input using pinMode(). For example, pin D3
pinMode(D3, INPUT);
int vibrationState = digitalRead(D3);

Arduino Nano ESP32 Code - Detecting vibration

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-sw-420-vibration-sensor */ #define SENSOR_PIN D3 // The Arduino Nano ESP32 pin D3 connected to the DO pin of the SW-420 vibration sensor int prev_vibration_state = LOW; // The previous reading from the input pin (idle/resting state) int vibration_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP32's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the ESP32's input pin vibration_state = digitalRead(SENSOR_PIN); if (prev_vibration_state == LOW && vibration_state == HIGH) Serial.println("Vibration detected!"); else if (prev_vibration_state == HIGH && vibration_state == LOW) Serial.println("Vibration stopped."); // save the last state prev_vibration_state = vibration_state; }

Detailed Instructions

Follow these instructions step by step:

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Wire the components according to the diagram above.
  • Copy the code above into the Arduino IDE.
  • Select the correct board and COM port, then click the Upload button.
  • Give the sensor module a light tap or shake it gently.
  • Open the Serial Monitor to view the reported events.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
Vibration detected! Vibration stopped. Vibration detected! Vibration stopped.
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2

From here, you can extend the sketch to light an LED, sound a buzzer, or push a notification whenever the SW-420 reports a shock. Check the tutorials linked at the bottom of this page for ideas on where to take it next.

Troubleshooting

If the SW-420 vibration sensor does not behave as expected, work through these checks:

  • Adjust the sensitivity: Turn the onboard potentiometer to raise or lower the amount of shaking needed to trigger the output.
  • Isolate ambient vibration: Mount the module away from motors, fans, or other sources of constant background shaking that can cause false triggers.
  • Check the wiring: Confirm the VCC, GND, and DO pins are connected to the correct pins on the Arduino Nano ESP32.
  • Check the power supply: Make sure the module is receiving a steady 3.3V to 5V supply.

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!