ESP32 S3 UNO - SW-420 Vibration Sensor

This ESP32 S3 Uno SW-420 vibration sensor tutorial shows you how to wire the module to the ESP32 S3 Uno form board and detect vibration with Arduino code. The SW-420 works as a simple shock sensor or vibration switch. When something bumps or shakes it, your sketch prints a message on the Serial Monitor.

In this tutorial, you will:

  1. Wire a SW-420 vibration sensor module to the ESP32 S3 Uno
  2. Read the shock sensor output with digitalRead()
  3. Detect when vibration starts and when it stops
  4. Tune the sensitivity with the onboard potentiometer
ESP32 S3 Uno SW-420 vibration sensor

When this basic sketch works, you can grow it into a bigger project. For example, you can sound a buzzer, flash an LED, or send an alert each time your project gets knocked or shaken.

Hardware Preparation

1×ESP32 S3 Uno-form Board
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

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

Before you write any code, it helps to know what is inside the module. Then the HIGH and LOW readings will make sense.

The SW-420 module is also called a shock sensor or a vibration switch module. Its main part is a small spring switch that sits very close to a metal contact inside a metal case. When the board shakes, the spring touches the contact for a moment. An LM393 comparator on the board watches this switch and turns every disturbance into a clean digital signal. A small potentiometer lets you set how much shaking is needed before the output changes.

The SW-420 Vibration Sensor Pinout

The module has only three pins, so wiring is quick.

VCC pin

Power input. It accepts 3.3V to 5V. On the ESP32 S3 Uno, use 3.3V so the output signal is also 3.3V.

GND pin

Connect it to ground (0V).

DO pin

The digital output. It stays LOW while the module is still. It goes HIGH as soon as vibration or shock is detected. Connect it to an input pin of the ESP32 S3 Uno.

SW-420 Vibration Sensor Pinout
image source: diyables.io

The board also has two small LEDs. One shows that power is on. The other lights up each time vibration is detected, which is very handy for testing before you upload any code.

How It Works

When nothing moves the module, the spring switch stays in its rest position. The LM393 comparator keeps the DO pin LOW.

When a shake or knock disturbs the spring switch, the comparator switches the DO pin to HIGH. Turning the potentiometer changes how strong that disturbance must be. Turn it one way for a very sensitive sensor, and the other way to react only to strong shocks.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D8, GND, 3.3V…) and their GPIO numbers while you wire the sensor.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The sensor needs just three wires. Connect it as shown in the image and the table below.

The wiring diagram between ESP32 S3 Uno SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

SW-420 Pin ESP32 S3 Uno Pin
VCC 3.3V
GND GND
DO D8 (GPIO21)

WARNING

ESP32 S3 Uno pins are NOT 5V tolerant. If you power the SW-420 from 5V, its DO pin also outputs 5V, which can damage GPIO21. Power the module from 3.3V, or add a voltage divider or level shifter on the DO line.

How To Program For SW-420 Vibration Sensor

Reading the SW-420 is the same as reading a button. You set the pin as an input once, then read it again and again.

  1. Set the ESP32 S3 Uno pin as a digital input with pinMode(). Here D8 is GPIO21, so you write 21 in code.
pinMode(21, INPUT);
  1. Read the pin state with digitalRead(). HIGH means vibration, LOW means the sensor is still.
int vibration_state = digitalRead(21);

ESP32 S3 Uno Code - Detecting vibration

The sketch below reads the DO pin in a loop and compares each reading with the last one. It prints a message only when the state changes, so the Serial Monitor shows when shaking starts and when it stops, without flooding the screen.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-sw-420-vibration-sensor */ #define SENSOR_PIN 21 // The ESP32 S3 Uno pin (D8 = GPIO21) connected to DO pin of the SW-420 vibration sensor int prev_vibration_state = LOW; // the previous state from the input pin (LOW means no vibration) int vibration_state; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the 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 the last state prev_vibration_state = vibration_state; }

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the sketch by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Test it by tapping the table or gently shaking the SW-420 sensor.
  9. Check the output on the Serial Monitor.
∞
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
Vibration detected! Vibration stopped Vibration detected! Vibration stopped
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: If you see no messages, watch the LED on the module while you shake it. If the LED does not light, turn the potentiometer slowly until it reacts.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Function References

These pages explain the functions used in the sketch in more detail.

FAQ

Can I power the SW-420 from 5V with the ESP32 S3 Uno?

The module works at 5V, but then its DO pin also gives 5V. ESP32 S3 Uno pins are 3.3V only and are not 5V tolerant. Power the module from 3.3V, or use a voltage divider or level shifter on the DO line.

Which ESP32 S3 Uno pin should I use for the DO output?

This tutorial uses D8, which is GPIO21 on the ESP32 S3 Uno. Any normal digital input pin works. Avoid D0/D1 (USB serial) and the boot strapping pins D6 (GPIO3) and D9 (GPIO46).

Why do I write 21 in the code and not 8?

On the ESP32 S3 Uno, the Uno header label and the GPIO number are different. The D8 header is wired to GPIO21, and the code always uses the GPIO number.

Can the SW-420 measure how strong the vibration is?

No. The SW-420 only gives a digital ON/OFF signal. The potentiometer sets the trigger level, but you cannot read the strength. For real measurements, use an accelerometer such as the MPU6050 or ADXL345.

Why does the sensor trigger many times from one knock?

The spring switch bounces for a short time after a shock. The output can flip HIGH and LOW several times. Add a short delay or a debounce time in your code if you want one event per knock.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only with PWM. Solder pin headers to use them and write the GPIO number in code. For more vibration sensors, GPIO35 to GPIO42 or GPIO15/GPIO16 are good choices, because the sensor needs an input pin (not GPIO47/GPIO48). Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port shows in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, try another cable, or hold BOOT while pressing RESET
Nothing prints on the Serial Monitor Wrong baud rate or wrong board selected Set the Serial Monitor to 9600 baud and choose ESP32S3 Dev Module
Module power LED is off VCC or GND wire is loose or wrong Check the wiring and make sure VCC goes to 3.3V and GND goes to GND
Sensor never triggers Sensitivity too low or DO on the wrong pin Turn the potentiometer slowly and check that DO goes to D8 (GPIO21)
Sensor triggers all the time Sensitivity too high or nearby motors and fans shake it Turn the potentiometer the other way and mount the sensor away from steady vibration sources
One knock gives many messages The spring switch bounces Add a short delay or debounce time in the code
Board resets or acts strangely Module powered from 5V sends 5V into GPIO21 Power the module from 3.3V or add a voltage divider on DO
Readings are unstable Weak or noisy power supply Use a good USB cable and a stable power source

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!