Arduino - SW-420 Vibration Sensor

Shocks, bumps, and shaking are easy for us to feel but hard for a plain Arduino to notice on its own. The SW-420 vibration sensor module fills that gap: it watches for mechanical disturbance and reports it as a simple digital signal, which is handy for security alarms, impact loggers, or any project that needs to react when something gets bumped or shaken.

In this tutorial, we are going to learn:

Arduino SW-420 vibration sensor

Once the basic sketch is running, you can extend it to sound a buzzer, flash an LED, or send an alert whenever vibration is picked up.

About SW-420 Vibration Sensor

The SW-420 vibration sensor module reacts to shaking, bumping, or shock in its surroundings. Inside the module sits a small spring-based switch positioned close to a metal contact; when vibration disturbs the spring, an onboard LM393 comparator turns that mechanical disturbance into a clean digital signal, so Arduino only has to read a HIGH or LOW pin instead of dealing with noisy analog readings.

The SW-420 Vibration Sensor Pinout

The SW-420 module breaks out three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • DO pin: is an output pin: LOW while everything is still, HIGH the moment vibration or shock is detected. This pin needs to be connected to Arduino's input pin.
SW-420 Vibration Sensor Pinout
image source: diyables.io

Two LEDs sit on the board:

  • One LED indicator for power
  • One LED indicator that lights up whenever the DO pin goes HIGH (vibration detected)

A small onboard potentiometer lets you dial the sensitivity up or down, so light taps or only stronger shocks can trigger the output, depending on how you turn it.

How It Works

  • While the module sits still, the spring switch stays open, the comparator keeps its output LOW, and the trigger LED stays off.
  • Once vibration or shock disturbs the spring, the switch closes momentarily, the comparator drives the output HIGH, and the trigger LED lights up.

Wiring Diagram

Arduino SW-420 Vibration Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How To Program For SW-420 Vibration Sensor

  • Sets the Arduino pin to digital input mode with pinMode() function. For example, pin 5
pinMode(5, INPUT);
  • Gets the current logic level of that pin with digitalRead() function.
int vibrationState = digitalRead(5);

Arduino Code - Detecting vibration

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-sw-420-vibration-sensor */ // Arduino's pin connected to DO pin of the SW-420 vibration sensor #define SENSOR_PIN 5 int previousState = LOW; // the previous reading from the input pin (LOW = idle) int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the input pin: currentState = digitalRead(SENSOR_PIN); if (previousState == LOW && currentState == HIGH) Serial.println("Vibration detected!"); else if (previousState == HIGH && currentState == LOW) Serial.println("Vibration stopped."); // save the current state for the next loop previousState = currentState; }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Tap or shake the SW-420 module gently
  • See the result on Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Vibration detected! Vibration stopped. Vibration detected! Vibration stopped.
Ln 11, Col 1
Arduino Uno on COM15
2

From here, you could trigger a buzzer, light up an LED, or push a notification whenever vibration shows up. You can find more information and step-by-step instructions in the tutorials provided at the end of this tutorial.

Troubleshooting

If the SW-420 vibration sensor is not working properly, try the following steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to make the module more or less sensitive; if it never triggers, try turning it the other way.
  • Isolate the module: Nearby machinery, fans, or footsteps can shake the surface it's mounted on and cause false triggers, so mount it away from unrelated vibration sources.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Ensure that the power supply is stable for consistent readings.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES