Raspberry Pi - SW-420 Vibration Sensor

The SW-420 vibration sensor module can pick up shocks, knocks, or shaking on whatever it is mounted to. It is handy for projects such as an intrusion alarm that reacts to someone bumping a door, a package that reports rough handling, or a machine that needs to flag abnormal shaking.

This tutorial instructs you how to use the Raspberry Pi and a SW-420 vibration sensor to detect vibration. We will explore:

Once this is working, you can extend the code to switch on an LED, sound a buzzer, or trigger a relay-controlled light whenever vibration is picked up.

Hardware Preparation

1×Raspberry Pi 5
1×SW-420 Vibration Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×Recommended: HDMI Touch Screen Monitor for Raspberry Pi

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 reacts to shocks and shaking. Inside the module sits a small spring-based vibration switch positioned near a metal contact; an onboard LM393 comparator watches that switch and turns any disturbance into a clean digital signal, while an onboard potentiometer lets you set how strong the shaking must be before the output trips.

Pinout

The SW-420 vibration sensor includes three pins:

  • VCC pin: needs to be connected to the voltage supply (3.3V to 5V)
  • GND pin: needs to be connected to ground (0V)
  • DO pin: is an output pin: LOW while everything is still and HIGH as soon as vibration or a shock is detected. This pin needs to be connected to Raspberry Pi's input pin.
SW-420 Vibration Sensor Pinout
image source: diyables.io

The SW-420 vibration sensor module features two LED indicators:

  • One LED indicates the power status.
  • Another LED indicates the trigger state, lighting up whenever the DO pin goes HIGH.

How It Works

Here's how the output pin of the sensor behaves:

  • While the module sits still, the spring switch stays open and the output pin reads LOW.
  • When vibration or a shock disturbs the spring switch, it makes contact and the output pin is driven HIGH.

Wiring Diagram

The wiring diagram between Raspberry Pi and SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

How To Program For SW-420 Vibration Sensor

  • Configures the Raspberry Pi pin as a digital input by using GPIO.setup() function.
GPIO.setup(SENSOR_PIN, GPIO.IN)
  • Reads the current state of the Raspberry Pi pin by using GPIO.input() function.
vibration_state = GPIO.input(SENSOR_PIN)

Raspberry Pi Code - Detecting vibration

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file vibration_sensor.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-sw-420-vibration-sensor """ import RPi.GPIO as GPIO from time import sleep # Set the Raspberry Pi GPIO pin number where the SW-420 vibration sensor is connected SENSOR_PIN = 7 # Set the GPIO mode and configure the vibration sensor pin as INPUT GPIO.setmode(GPIO.BCM) GPIO.setup(SENSOR_PIN, GPIO.IN) # Initialize the previous state variable with the current state prev_vibration_state = GPIO.input(SENSOR_PIN) try: while True: # Read the current state of the vibration sensor vibration_state = GPIO.input(SENSOR_PIN) # Check for a state change (LOW to HIGH or HIGH to LOW) if vibration_state != prev_vibration_state: if vibration_state == GPIO.HIGH: print("Vibration detected!") else: print("Vibration stopped!") # Update the previous state variable prev_vibration_state = vibration_state # Add a small delay to prevent continuous readings sleep(0.1) except KeyboardInterrupt: # Clean up GPIO settings when Ctrl+C is pressed GPIO.cleanup() print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 vibration_sensor.py
  • Tap or shake the SW-420 sensor a couple of times.
  • See the result in the Terminal.
PuTTY - Raspberry Pi
Vibration detected! Vibration stopped! Vibration detected! Vibration stopped!

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

From here, you can adapt the code to flash an LED, sound a buzzer, or switch a relay-controlled light on whenever vibration is detected. For detailed instructions and more information, please refer to the tutorials provided at the end of this guide.

Troubleshooting

If the SW-420 vibration sensor is not behaving as expected, try the following troubleshooting steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to raise or lower how much shaking is needed before the output triggers.
  • Isolate it from ambient vibration: Mount the module firmly and away from motors, fans, or foot traffic that could cause false triggers.
  • 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 and free from any electrical noise for consistent readings.

By following these steps, you should be able to address any potential issues with the SW-420 vibration sensor.

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!