Arduino Mega - SW-420 Vibration Sensor

This project pairs an Arduino Mega with a SW-420 vibration sensor to catch shocks and shaking as they happen. Here is what we will go over:

Arduino Mega SW-420 vibration sensor

Once the basic detection works, you can extend the sketch to sound a buzzer, flash an LED, or trigger a relay whenever vibration is picked up.

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×SW-420 Vibration Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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

The SW-420 Vibration Sensor Module watches for shocks, knocks, and shaking. 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 disturbance into a clean digital signal. A small potentiometer on the board lets you dial in how much shaking is needed before it reports a trigger.

The SW-420 Vibration Sensor Pinout

The module breaks out three pins.

  • VCC pin: power input, accepts 3.3V to 5V.
  • GND pin: ground reference (0V).
  • DO pin: digital output pin. It stays LOW while the module is still, and switches HIGH the instant vibration or shock is detected. Wire this pin to a digital input on the Arduino Mega.
SW-420 Vibration Sensor Pinout
image source: diyables.io

Two onboard LEDs help with debugging.

  • A power indicator LED
  • A trigger indicator LED that lights up whenever the DO pin goes HIGH

How It Works

  • While at rest, the internal spring switch stays open, so the comparator holds the DO pin LOW.
  • When vibration or a shock disturbs the spring, it briefly touches the contact, and the comparator drives the DO pin HIGH.
  • Turning the onboard potentiometer changes how strong a vibration has to be before the module reports it.

Wiring Diagram

The wiring diagram between Arduino Mega SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW-420 Vibration Sensor

  • Configures a pin on the Arduino Mega as a digital input with the pinMode() function. For example, pin 8.
pinMode(8, INPUT);
  • Reads the current state of that pin with the digitalRead() function.
int vibrationState = digitalRead(8);

Arduino Mega Code - Detecting vibration

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-sw-420-vibration-sensor */ #define SENSOR_PIN 8 // The Arduino Mega pin connected to the DO pin of the SW-420 vibration sensor int prev_vibration_state = LOW; // the previous state from the input pin 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 Arduino's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of 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.

  • Wire the SW-420 vibration sensor to the Arduino Mega as shown in the diagram.
  • Plug the Arduino Mega into your computer using a USB cable.
  • Launch the Arduino IDE.
  • Select the correct board: Arduino Mega, and the matching COM port.
  • Paste the code below into a new sketch in the Arduino IDE.
  • Click Upload to send the sketch to the Arduino Mega.
  • Tap or shake the SW-420 sensor to trigger it.
  • Watch the results in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Vibration detected! Vibration stopped Vibration detected! Vibration stopped
Ln 11, Col 1
Arduino Mega Or... on COM15
2

Troubleshooting

If the SW-420 vibration sensor is not responding the way you expect, try these steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to make the module more or less sensitive to shaking.
  • Isolate ambient vibration: Mount the sensor away from motors, fans, or other sources of constant vibration that could cause false triggers.
  • Check the wiring: Confirm that VCC, GND, and DO are all connected to the right pins.
  • Check the power supply: An unstable supply can lead to inconsistent or noisy readings.

Video Tutorial

Function References

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!