Skip to content

Motion · ESP32 DevKit (GPIO27 example) guide

Interfacing SW-420 Vibration Switch Module with Comparator with ESP32 DevKit (GPIO27 example)

A simple vibration and knock detector with a digital output and an adjustable sensitivity trimmer.

Updated October 29, 2026 Arduino C++digitalRead (built-in)Also forArduino Uno R3

What is the SW-420 Vibration Sensor?

The SW-420 vibration sensor module is a tiny, low-cost board that tells your microcontroller when something has been shaken, knocked or bumped. It has three pins, no library and no configuration: when vibration crosses a threshold you set with a small screw trimmer, the digital output changes state.

That simplicity is what makes it useful. Typical projects include a knock-activated doorbell, an alarm that fires when a box, bike or cabinet is moved, a counter that registers each time a machine thumps, and a "wake up" trigger that lets a battery project sleep until something happens.

It is important to know what the module is not. It does not measure how hard or how fast something shakes, and it cannot tell you the direction or frequency. It answers one question only: did the vibration cross my threshold or not? If you need numbers, use an accelerometer such as the ADXL345 or MPU-6050 instead.

How the module works

The SW-420 sensor is a small spring-type switch that is normally closed. When the board is still, the switch sits in one state. When it is shaken or knocked, the contacts flicker for a moment, producing a brief electrical disturbance.

That disturbance alone is too small and ragged for a microcontroller to use, so an LM393 comparator cleans it up. It compares the signal from the sensor against a reference voltage set by the blue trimmer. When the signal crosses the reference, the comparator output flips and the DO pin changes state. The second LED on the board lights to show this.

That is why the trimmer is called a threshold control rather than a calibration. Turn it one way and a gentle tap triggers the output; turn it the other way and only a hard knock does. The module is inherently a yes or no detector, not a measuring instrument.

Which level means vibration?

Documentation for this module generally describes the output as low at rest and high when vibration is detected, but cheap clones do not all follow that. Some boards are inverted, and the behaviour can change with the trimmer setting. Never assume. Upload the sketch below, watch the Serial Monitor and the output LED, and note which level appears when you tap the board. The code uses a single constant so you can flip it in one place.

Setting the sensitivity

  1. Mount the module in its final position first. How it is fixed changes how vibration reaches the sensor, so tune it where it will live.
  2. Power it up and let the output settle at rest.
  3. Turn the trimmer slowly with a small screwdriver until the output LED just stays in its resting state.
  4. Tap the surface where you expect the event. The LED should flash. If it does not, back the trimmer off a little at a time.
  5. Check that normal background vibration, such as a fan or footsteps, does not trigger it.

Small turns make a big difference, and the direction that raises sensitivity differs between boards, so go gently and watch the LED.

Basic sketch: count vibration events

This sketch checks the pin in the main loop and ignores repeats for half a second, so a single knock is counted once rather than dozens of times. Change ACTIVE_LEVEL to LOW if your module is inverted. On an ESP32, change SENSOR_PIN to the GPIO you wired.

Interrupt version for fast or low-power projects

Polling can miss a very short pulse if your loop is busy, and it keeps the processor awake. An interrupt reacts the instant the pin changes and lets you do other work, or sleep, in between. On an Uno or Mega use pin 2 or 3 (the Mega also has 18 to 21). On an ESP32 any GPIO works, but add IRAM_ATTR before the handler. Use RISING for an active-high module and FALLING if yours is inverted.

Using it well

  1. Fix it firmly. A loose board makes its own vibration and triggers falsely.
  2. Tune in place. Always set the trimmer in the finished enclosure, not on the desk.
  3. Debounce in software. One knock produces several quick transitions, so ignore repeats for a short time.
  4. Match the logic voltage. Power it at 3.3 V on 3.3 V boards.
  5. Combine it with an output. An active buzzer turns it into a simple alarm in a few lines of code.
  6. Use an interrupt for battery projects. Let the processor sleep until the sensor wakes it.

Frequently asked questions

Does it have an analog output? No. Only the digital DO pin is provided on this module, so you cannot read the strength of the vibration.

Can it detect an earthquake? It can react to strong shaking, but it is far too crude and unreliable to be used as an earthquake detector.

Why is the output inverted on my board? Different manufacturers wire the comparator differently. Check the resting level with the Serial Monitor and set ACTIVE_LEVEL to match.

Can I power it from 3.3 V? Yes. The module is rated for 3.3 to 5 V, and 3.3 V is the right choice for ESP32 and Raspberry Pi projects.

How sensitive is it? It depends on the trimmer and on how the module is mounted. There is no calibrated figure, so test it against the event you care about.

Specifications

Sensing element
SW-420 vibration switch (normally closed type)
Signal conditioning
LM393 voltage comparator
Operating voltage
3.3 to 5 V DC
Operating current
About 15 mA
Output
Digital (DO) only; there is no analog output
Output behaviour
Low at rest and high on vibration on most documented boards; some clones are inverted
Sensitivity adjustment
Onboard 10 kΩ trimmer potentiometer
Indicators
Two LEDs: power and output status
Mounting
Bolt holes on the board

Pinout

PinNameDescription
1VCCPower supply, 3.3 to 5 V
2GNDGround
3DODigital output, changes state when vibration crosses the set threshold

Swipe sideways to see every column →

Power it from the logic voltage of your board. The output swings between 0 V and the supply voltage. On a 5 V Arduino that is fine. On an ESP32 or Raspberry Pi, power the module from 3.3 V so the DO pin can never exceed what the GPIO tolerates.

Pin order varies between makers. Most boards print VCC, GND and DO along the edge, but always follow the silkscreen on your own module rather than the position of the pins.

Share the ground. The module and the microcontroller must be on the same ground, or the output level is meaningless.

Wiring & code

Setting up SW-420 Vibration Sensor with ESP32 DevKit (GPIO27 example)

Library

digitalRead (built-in) by Arduino-ESP32 / SolderHub

Use an ESP32 board package in Arduino IDE; no additional library.

Wiring — ESP32 DevKit (GPIO27 example)

Module pinESP32 pinNotes
VCC3V3Use 3.3V only if supported by the module
GNDGNDCommon ground
DOGPIO27Verify output never rises above 3.3V and determine active polarity

Swipe sideways to see every column →

C++

Power only a module that supports 3.3V from 3V3; verify DO never exceeds 3.3V and confirm active polarity. Use a level shifter if the output can be 5V. This module is not a calibrated vibration instrument. Not physically bench-tested.

Keep learning

Related guides & projects