ESP32 S3 - SW-420 Vibration Sensor

An ESP32 S3 paired with a SW-420 vibration sensor can catch a knock, a shake, or a sudden jolt the instant it happens, which makes it a great starting point for alarms, shipping monitors, or simple shake-activated gadgets. This tutorial walks through wiring the module and writing the firmware needed to react to that vibration in real time.

What you'll build:

  1. An overview of how the SW-420 module senses shock and vibration
  2. A wired connection between the SW-420 module and the ESP32 S3
  3. Firmware that watches the sensor's digital pin and reacts to changes
  4. Serial Monitor output confirming vibration events as they start and end
ESP32 S3 SW-420 vibration sensor

Once the basic sketch is running, you can extend it to sound a buzzer, send a WiFi alert, or drive a relay whenever a shock is detected.

Hardware Preparation

1×ESP32 S3 WROOM N16R8
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

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 detects shock and vibration using a small spring-based switch mounted next to a metal contact. Any knock or shake disturbs the spring, and an onboard LM393 comparator turns that disturbance into a clean digital signal the ESP32 S3 can read directly - there's no analog pin or extra math involved.

Key Specifications

  • Operating voltage: 3.3V to 5V DC
  • Digital-only output (no analog pin on this module)
  • Sensitivity adjustable through an onboard potentiometer/trimmer
  • Comparator: onboard LM393
  • Onboard power LED plus a trigger LED for the digital output

The SW-420 Vibration Sensor Pinout

Three pins on the SW-420 module connect straight to the ESP32 S3, no extra components needed.

  1. VCC pin: Connect to power supply (3.3V to 5V)
  2. GND pin: Connect to ground (0V)
  3. DO pin: Digital output pin - stays LOW while idle and switches HIGH the moment vibration or shock is detected - connect to ESP32 S3 digital input pin
SW-420 Vibration Sensor Pinout
image source: diyables.io

Additional features:

  • Power LED indicator confirms the module is receiving voltage
  • Trigger LED lights up whenever the DO pin goes HIGH, handy for testing without a Serial Monitor
  • Onboard potentiometer tunes how much shaking is needed before DO trips HIGH

How It Works

The spring switch inside the SW-420 either rests undisturbed or gets knocked into brief contact, and the comparator turns that into a signal your sketch can poll.

Digital output behavior:

  • At rest, with no vibration: the spring switch stays open → output pin reads LOW
  • The instant vibration or shock disturbs the spring: the switch momentarily closes → output pin reads HIGH

Wiring Diagram

Wire the SW-420 module's three pins to the ESP32 S3 as shown below, keeping the connections short and secure on the breadboard.

Safety Notes

Match the VCC wire to a 3.3V or 5V pin on your ESP32 S3, never anything higher, and make sure GND is solidly connected since a floating ground is a common cause of a DO pin that never settles. If the module still triggers randomly after wiring is confirmed, turn the onboard potentiometer to lower its sensitivity before suspecting the code.

The wiring diagram between ESP32 S3 SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

SW-420 Vibration Sensor Pin ESP32 S3 Pin
VCC 3.3V or 5V
GND GND
DO D16 (GPIO16)

How To Program For SW-420 Vibration Sensor

The firmware only needs to set up one digital input pin and then watch it change state inside the loop. Below, pin D16 is configured as an input and polled continuously, and the sketch prints a message to the Serial Monitor the moment the reading flips from idle to triggered, and again once it settles back down.

Step 1 - Configure the input pin:

pinMode(D16, INPUT);

Step 2 - Poll the current reading:

int vibrationState = digitalRead(D16);

ESP32 S3 Code - Detecting vibration

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-sw-420-vibration-sensor */ #define SENSOR_PIN 16 // The ESP32 S3 pin 16 connected to the DO pin of the SW-420 vibration sensor int prev_vibration_state = LOW; // The previous state from the input pin (LOW = 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(115200); // 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Check the wiring: Match every wire to the diagram above before powering the board
  3. Copy the code: Copy the sketch above into a new Arduino IDE window
  4. Select your board: Choose ESP32 S3 from the boards menu
  5. Upload the sketch: Click Upload to flash the code onto your ESP32 S3
  6. Open Serial Monitor: Set the baud rate to 115200
  7. Give it a tap: Gently knock or shake the SW-420 module
  8. Watch the readings: Confirm the Serial Monitor logs a detected/stopped pair for each tap
  9. Pro Tip: If every tiny knock triggers it, turn the onboard potentiometer clockwise or counter-clockwise a little at a time until the sensitivity feels right for your project
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
11:02:07.318 - Vibration detected 11:02:07.664 - Vibration stopped 11:02:11.902 - Vibration detected 11:02:12.130 - Vibration stopped 11:02:15.477 - Vibration detected 11:02:15.951 - Vibration stopped
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

From here, swap the Serial.println() calls for anything you need - light an LED, latch a relay, or fire off a notification - the moment vibration is detected. The project ideas and challenges below offer a few directions to take it.

Application and Project Ideas

A digital vibration switch this simple fits into far more than lab demos - here are a few ways to put it to work with an ESP32 S3.

  1. Shock Alarm: Trigger a buzzer or siren the instant an unexpected knock or impact is detected
  2. Package Shock Logger: Log timestamps of every jolt a parcel experiences in transit
  3. Washing Machine Imbalance Monitor: Flag excessive shaking during a spin cycle before it becomes a problem
  4. Tutorial Tool Safety Monitor: Detect abnormal vibration on a running power tool and cut power automatically
  5. Mini Seismograph Demo: Chart vibration events over time for a simple earthquake-detector-style classroom project

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Try these challenges to build on the basic vibration sketch and push your ESP32 S3 project further.

  1. Beginner: Light an LED the moment vibration is detected and turn it off once things settle
  2. Beginner: Show the vibration state on an LCD instead of only the Serial Monitor
  3. Intermediate: Keep a running count of vibration events and print a total every minute
  4. Intermediate: Wire the sensor to a relay so a shock automatically cuts power to another device
  5. Advanced: Combine readings from several SW-420 modules placed around an object to map where vibration is strongest

Troubleshooting

If your ESP32 S3 and SW-420 vibration sensor aren't behaving as expected, work through these checks.

Common issues and fixes:

  • Adjust the potentiometer: Too sensitive and it fires on every footstep; too dull and it misses real taps - turn the onboard trimmer gradually and retest after each turn
  • Isolate from ambient vibration: Mount the module away from motors, fans, or a shaky table so background rumble doesn't masquerade as a real event
  • Check wiring: Reconfirm VCC, GND, and DO all match the wiring diagram - a loose DO wire looks identical to "no vibration" on the Serial Monitor
  • Verify power supply: An unstable or noisy 3.3V/5V source can make the comparator output chatter on its own
  • Watch the trigger LED: The onboard LED should flash in step with each knock - if it doesn't, the fault is on the sensor side, not in your code

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!