Arduino MKR WiFi 1010 - SW-420 Vibration Sensor

A SW-420 vibration sensor is a small but handy module that reacts the instant something shakes, bumps, or knocks it. Pair it with the Arduino MKR WiFi 1010 and you can build a shock alarm, a knock-triggered light, or a package that "tells" you if it has been dropped or tampered with.

In this tutorial, we are going to learn:

Arduino MKR WiFi 1010 SW-420 vibration sensor

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×SW-420 Vibration Sensor Module
1×Breadboard
1×Jumper Wires

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 module is built around a tiny spring-based switch that sits close to a metal contact inside its metal can. As long as everything is still, the switch stays in its resting position. The moment the module is shaken, knocked, or bumped, the spring swings and briefly touches the contact, and an onboard LM393 comparator turns that brief touch into a clean, sharp digital signal for the Arduino MKR WiFi 1010 to read. A small potentiometer on the board lets you dial in how much shaking it takes before it reports a trigger - turn it one way for hair-trigger sensitivity, the other way for something closer to needing an actual jolt.

The SW-420 Vibration Sensor Pinout

The SW-420 vibration sensor module has three pins that you'll connect to your Arduino MKR WiFi 1010:

  • VCC pin: Connects to your power supply. Works with both 3.3V and 5V, making it compatible with the Arduino MKR WiFi 1010's voltage levels.
  • GND pin: Connects to ground (0V) - complete the circuit by connecting this to your Arduino MKR WiFi 1010's GND pin.
  • DO pin: The digital output pin that reports vibration to your Arduino MKR WiFi 1010. This pin stays LOW while everything is still and jumps to HIGH the moment vibration or shock is detected. Connect this to any digital input pin on your Arduino MKR WiFi 1010.
SW-420 Vibration Sensor Pinout
image source: diyables.io

Helpful Features:

  • LED indicators: Two helpful LEDs on the board:
    • Power LED: Lights up whenever the module is powered, confirming proper connection
    • Trigger LED: Lights up together with the DO pin, so you can see a trigger happen without needing the Arduino at all

    How It Works

    Here is how the sensor's output pin behaves:

    • While the module sits still, the internal switch stays open and the output pin reads LOW.
    • The instant vibration or a shock disturbs the switch, the output pin flips to HIGH.

Wiring Diagram

The wiring diagram between Arduino MKR WiFi 1010 SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW-420 Vibration Sensor

  • This sets the Arduino MKR WiFi 1010 pin to act as a digital input using the pinMode() function. For example, you can use pin D3.
pinMode(3, INPUT);
  • It checks if the Arduino MKR WiFi 1010 pin is HIGH or LOW using the digitalRead() function.
int vibrationState = digitalRead(D3);

Arduino MKR WiFi 1010 Code - Detecting the vibration

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-sw-420-vibration-sensor */ // Arduino's pin connected to DO pin of the SW-420 vibration sensor #define SENSOR_PIN 3 int lastState = LOW; // the previous state from the input pin 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 (lastState == LOW && currentState == HIGH) Serial.println("Vibration detected!"); else if (lastState == HIGH && currentState == LOW) Serial.println("Vibration stopped"); // save the last state lastState = currentState; }

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Connect the components to the Arduino MKR WiFi 1010 board as depicted in the diagram
  • Plug your Arduino MKR WiFi 1010 into your computer's USB port
  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and its COM port
  • Copy the above code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino MKR WiFi 1010.
  • Tap or shake the SW-420 sensor a few times.
  • Check the result in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' on 'COM15')
New Line
9600 baud
Vibration detected! Vibration stopped Vibration detected! Vibration stopped
Ln 11, Col 1
Arduino MKR WiFi 1010 on COM15
2

Now, we can change the code to sound a buzzer, flash an LED, or send a notification whenever vibration is detected. For more details and easy step-by-step instructions, check the tutorials at the end of this guide.

Troubleshooting

If you have any problem with the SW-420 vibration sensor, please try these steps to fix it:

  • Adjust the sensitivity: Turn the onboard potentiometer with a small screwdriver. Too sensitive and it triggers on its own; not sensitive enough and light knocks go unnoticed.
  • Isolate ambient vibration: Fans, motors, footsteps, or a wobbly desk can all trigger the sensor. Mount it on a stable, still surface away from unrelated sources of shaking.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Make sure the power supply is clean and stable, since noisy power can cause false triggers.

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!