ESP32 UNO - SW-420 Vibration Sensor
Want to know when something gets knocked or shaken? Connect a SW-420 vibration sensor to the ESP32 Uno form board, and a short sketch tells you the moment a shock arrives and the moment it ends. The module works as a shock sensor or vibration switch with a simple HIGH/LOW output.
By the end, you can:
- Wire the SW-420 module to the ESP32 Uno with three wires
- Read the DO pin with digitalRead()
- Print a message when vibration starts and when it stops
- Set the trigger level with the onboard potentiometer

Later, you can add a buzzer, an LED or a phone alert to the same sketch.
Hardware Preparation
| 1 | × | ESP32 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 the SW-420 Module
By the end of this part, you know what is on the module and what its three pins do.
Inside the metal case is a small spring switch placed very close to a metal contact. A shake makes the spring touch the contact for a moment. An LM393 comparator on the board watches this switch and turns it into a clean digital level. A small potentiometer sets how hard the shake must be.
| Part | Role |
|---|---|
| Spring switch | Touches a metal contact when the module moves |
| LM393 comparator | Converts the switch into a clean HIGH or LOW level |
| Potentiometer | Sets the trigger level (sensitivity) |
| Power LED | On when the module has power |
| Signal LED | Lights up while vibration is detected |
SW-420 Pinout
The module has three pins.
| Pin | Function |
|---|---|
| VCC | Power input, 3.3V to 5V |
| GND | Ground (0V) |
| DO | Digital output, LOW at rest and HIGH when vibration or shock is detected |

Good to know: The signal LED on the module is a quick test. If it lights up when you tap the table, the sensor works before you write any code.
How It Works
- At rest, the spring switch stays in its normal position and the comparator holds DO LOW.
- A knock or shake moves the spring, and the comparator pushes DO HIGH.
- A turn of the potentiometer makes the module react to weaker or stronger shocks.
ESP32 Uno Pinout
Every Uno header pin on the ESP32 Uno form board has its own GPIO number, and the sketch uses that number. Look at the picture first, so each wire lands on the right pin.

How to Connect
Three wires are all you need. Follow the image and the table.

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
The wiring diagram may not show the correct pin. On the ESP32 Uno, the DO wire goes to D7 (GPIO14), not D8 as the picture may show. We will update the diagram soon. Until then, follow the wiring table and the code on this page, not the picture.
| SW-420 Pin | ESP32 Uno Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| DO | D7 (GPIO14) |
Good to know: ESP32 Uno pins are 3.3V logic and are not 5V tolerant. Power the module from 3.3V, so DO also stays at 3.3V.
How To Program For SW-420 Vibration Sensor
Reading this sensor is as easy as reading a push button. Choose the pin mode once, then poll the pin in a loop.
- Make the pin a digital input with pinMode(). D7 is GPIO14, so the code uses 14.
- Get the sensor state with digitalRead().
ESP32 Uno Code - Detecting Vibration
The sketch compares each new reading with the last one and prints text only when the state changes.
| Code Part | What It Does |
|---|---|
| SENSOR_PIN 14 | Selects GPIO14 (D7) as the sensor input |
| pinMode(SENSOR_PIN, INPUT) | Makes the pin an input in setup() |
| digitalRead(SENSOR_PIN) | Samples the DO level in every loop |
| prev_vibration_state | Stores the last level to spot a change |
| Serial.println() | Prints a line when vibration starts or stops |
The result is exactly one line per change, not a flood of text.
Detailed Instructions
Follow these in order:
- First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
- Build the circuit by following the wiring table above.
- Plug in the USB Type-C cable between the ESP32 Uno and your computer.
- Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
- Copy the code into a new sketch.
- Click Upload and wait for "Done uploading".
- Open the Serial Monitor and set 9600 baud.
- Shake the sensor or tap the table next to it.
- Look at the Serial Monitor for the new lines.
- Pro tip: Stick the module to a flat surface with tape. A loose module swings on its wires and triggers by itself.
Video Tutorial
See the ESP32 Uno build in action in the video below.
ESP32 Uno vs UNO R4 for This Project
Moving this project to the ESP32 Uno changes the pin number in code and the voltage rules.
| Item | Arduino UNO R4 | ESP32 Uno |
|---|---|---|
| Logic voltage | 5V | 3.3V |
| DO pin in code | 8 | 14 |
| DO pin on the header | D8 | D7 (GPIO14) |
| Module power | 5V or 3.3V | 3.3V is best |
| Arduino IDE board | Arduino UNO R4 WiFi | ESP32 Dev Module |
| USB chip | Native USB | CH340 driver |
The DO wire moves from D8 to D7 here. The picture may still show D8. On the ESP32 Uno, D8 (GPIO12) is a boot pin that must be LOW at power-on, so D7 (GPIO14) is safer. Change the pin number in the sketch, and keep the DO signal at 3.3V.
Go Wireless: Vibration Alerts on Your Phone
By the end of this part, you have an idea to see shocks on a screen instead of the Serial Monitor.
The ESP32 Uno has WiFi and Bluetooth. You can send the DO state to a web page or a phone app, and then watch the knocks live from another room. It also works for a simple alarm that tells you when a door, a box or a machine gets shaken.
Function References
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port appears | CH340 driver missing or a charge-only cable | Install the CH340 driver and use a USB cable that carries data |
| Failed to connect or Waiting for download | The board is not in upload mode | Hold the BOOT button when the upload starts |
| Module power LED is off | VCC or GND wire is loose | Check VCC to 3.3V and GND to GND |
| No reaction at all | Trigger level set too high, or DO wired to another pin | Turn the knob slowly and confirm the DO wire sits on D7 |
| Constant triggering | Trigger level set too low, or a motor is close | Turn the knob back and place the module away from fans |
| One knock prints many lines | The spring switch bounces | Insert a small delay in loop() |
| Nothing in the Serial Monitor | Wrong baud rate | Set the Serial Monitor to 9600 |
| Board resets when the sensor is on | A 5V signal enters a 3.3V pin | Power the module from 3.3V |
FAQ
How does the SW-420 detect vibration?
A small spring switch inside the module touches a metal contact when the module moves. An LM393 comparator turns that touch into a clean HIGH or LOW signal on the DO pin.
Should I use 3.3V or 5V for the SW-420 on the ESP32 Uno?
Use 3.3V. At 5V the DO pin also outputs 5V, and the ESP32 Uno pins are not 5V tolerant. If you must use 5V, add a voltage divider on the DO line.
Why does the code use 14 and not 7?
The label D7 is the position on the Uno header. The chip knows only the GPIO number, and D7 is wired to GPIO14. So the sketch always writes 14.
Can I measure how strong the vibration is?
No. The SW-420 gives only a digital HIGH or LOW. The knob only moves the trigger point, so the force stays unknown. For real values, use an accelerometer such as the MPU6050.
Why do I get many messages from one knock?
The spring bounces for a few milliseconds after a shock. A small delay in the loop gives you one event per knock.
Do I need analogSetAttenuation for this sensor?
No. The sketch reads a digital pin, not the ADC, so you do not need that line.
Can I use two SW-420 modules on one ESP32 Uno?
Yes. Use two input pins, for example the extra GPIO32 and GPIO33 holes, and read each one with its own digitalRead().
What if my project needs more pins than the Uno headers give?
The ESP32 Uno has a few extra holes near the power header. GPIO32 and GPIO33 are I/O, PWM and analog on ADC1, so they also work while WiFi is on. GPIO15 is I/O, PWM and analog on ADC2. Solder pin headers to use them and write the GPIO number in code. For a second vibration sensor, GPIO32 and GPIO33 are the best choice. Be careful with two things. First, GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32 and GPIO33 first. Second, never use the six holes under the digital header (SD0 to SD3, CMD and CLK, which are GPIO6 to GPIO11). They are wired to the flash chip, and using them crashes the board.
Can I use the code from the UNO R4 sketch?
Almost. Only the pin number changes from 8 to 14, and the DO wire moves to D7. The logic is the same, but the ESP32 Uno runs at 3.3V.
Can I see the vibration on my phone?
Yes. The ESP32 Uno has WiFi and Bluetooth, so you can send the DO state to a web page or a phone app. See the Go Wireless section above.