Arduino UNO R4 - SW-420 Vibration Sensor

In this guide, we will learn how to use a SW-420 vibration sensor with the Arduino UNO R4. In detail, we will learn:

Arduino UNO R4 SW-420 vibration sensor

Once this basic sketch is running, you can extend it to sound a buzzer, flash an LED, or send an alert whenever your project gets bumped or shaken.

Overview of SW-420 Vibration Sensor

The SW-420 vibration sensor module (also known as a shock sensor or vibration switch module) is built around a small spring-based switch mounted close to a metal contact inside its metal casing. An onboard LM393 comparator continuously watches that switch and turns any disturbance into a clean digital signal, while a small potentiometer on the board lets you dial in how much shaking or shock is needed before the output changes state.

The SW-420 Vibration Sensor Pinout

The SW-420 vibration sensor module has three pins:

  • VCC pin: Connect to VCC (3.3V to 5V).
  • GND pin: Connect to GND (0V).
  • DO pin: This is an output pin. It stays LOW while the module is still and goes HIGH the moment vibration or shock is detected. Connect this pin to the input pin of Arduino UNO R4.
SW-420 Vibration Sensor Pinout
image source: diyables.io

The SW-420 vibration sensor module also has two LED indicators.

  • One LED indicator for power
  • One LED indicator that lights up whenever vibration is detected

How It Works

  • While nothing is shaking the module, the spring switch stays in its resting position and the LM393 comparator keeps the output pin LOW.
  • As soon as vibration or shock disturbs the spring switch, the comparator flips the output pin to HIGH. Turning the onboard potentiometer raises or lowers how strong that disturbance needs to be before this happens.

Wiring Diagram

The wiring diagram between Arduino UNO R4 SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

How To Program For SW-420 Vibration Sensor

  • Sets up the Arduino UNO R4 pin as a digital input using the pinMode() function. For instance, pin 8
pinMode(8, INPUT);
  • Uses the digitalRead() function to check the status of a pin on the Arduino UNO R4.
int vibration_state = digitalRead(8);

Arduino UNO R4 Code - Detecting vibration

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-sw-420-vibration-sensor */ #define SENSOR_PIN 8 // The Arduino UNO R4 pin connected to DO pin of the SW-420 vibration sensor int prev_vibration_state = LOW; // the previous state from the input pin (LOW means no vibration) 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 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 instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the SW-420 vibration sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to send the code to the Arduino UNO R4.
  • Tap the table or gently shake the SW-420 sensor to trigger it.
  • Check the results on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Vibration detected! Vibration stopped Vibration detected! Vibration stopped
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

Troubleshooting

If the SW-420 vibration sensor isn't behaving as expected, try these steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to raise or lower how much shaking or shock is needed to trigger the output.
  • Isolate it from ambient vibration: Mount the sensor away from motors, fans, or other steady vibration sources that could cause false triggers.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Make sure the power supply is stable for reliable 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!