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:
- Wire a SW-420 vibration sensor module to the ESP32 S3 Uno
- Read the shock sensor output with digitalRead()
- Detect when vibration starts and when it stops
- Tune the sensitivity with the onboard potentiometer

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) |
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.

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.

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

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.
- Set the ESP32 S3 Uno pin as a digital input with pinMode(). Here D8 is GPIO21, so you write 21 in code.
- Read the pin state with digitalRead(). HIGH means vibration, LOW means the sensor is still.
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.
Detailed Instructions
Follow these steps in order:
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into Arduino IDE.
- Upload the sketch by clicking the Upload button.
- Open the Serial Monitor and set the baud rate to 9600.
- Test it by tapping the table or gently shaking the SW-420 sensor.
- Check the output on the Serial Monitor.
- 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 |