Raspberry Pi Pico W

SW-420 Vibration Sensor with Raspberry Pi Pico W using MicroPython


SW-420 Vibration Sensor with Raspberry Pi Pico W using MicroPython– Are you ready to dive into the world of the SW-420 Vibration Sensor? Look no further! In this comprehensive guide, we will explore the incredible power of the SW-420 Vibration Sensor using Raspberry Pi Pico W. From understanding the basic principles behind vibration sensing to practical applications, we’ve got you covered.

As technology continues to evolve, so does the demand for advanced sensor systems. The SW-420 Vibration Sensor is a versatile and cost-effective tool that detects vibrations and provides valuable data for various applications, such as burglar alarms, vehicle tracking, and structural health monitoring.

By combining the power of the SW-420 Vibration Sensor with Raspberry Pi Pico W microcontroller, you can unlock a world of possibilities. Whether you are a hobbyist, a student, or a professional, this guide will walk you through the necessary steps to set up and utilize this powerful combination.

So, get ready to explore the limitless potential of the SW-420 Vibration Sensor and Raspberry Pi Pico W as we take you on an exciting journey into the world of vibration sensing and IoT applications. Let’s get started!

If you want to learn how to interface the SW-420 Vibration Sensor with Arduino then read this article.

Amazon links:

Raspberry Pi Pico W

SW-420 Vibration Sensor



Jumper wires

A breadboard (optional, for easier connections)

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Understanding Raspberry Pi Pico W

SW-420 Vibration Sensor with Raspberry Pi Pico W

The Raspberry Pi Pico W is a powerful microcontroller board that provides a wide range of features and capabilities. It is based on the Raspberry Pi Foundation’s RP2040 microcontroller, which offers high-performance processing and low power consumption. With its compact size and GPIO pins, the Raspberry Pi Pico W is an ideal choice for connecting and controlling various sensors, including the SW-420 Vibration Sensor.

One of the key advantages of the Raspberry Pi Pico W is its compatibility with popular programming languages such as MicroPython and C/C++. This flexibility allows users to develop applications and projects using their preferred programming language. Additionally, the Raspberry Pi Pico W supports a variety of interfaces, including I2C, SPI, and UART, making it easy to interface with sensors like the SW-420 Vibration Sensor.

To get started with the Raspberry Pi Pico W, you’ll need to install the required software and connect it to your computer via USB. Once connected, you can start programming the microcontroller and interacting with the SW-420 Vibration Sensor.

Specification Detail
Microcontroller RP2040
CPU Dual-core ARM Cortex-M0+ processor
Clock Speed Up to 133 MHz
On-board Storage 2 MB Flash memory
Wireless 802.11n Wireless LAN (2.4 GHz)
GPIO Pins 26 multi-function GPIO pins
Analog Inputs 3 analog inputs (12-bit ADC)
UART, SPI, I2C Available
USB Support 1 x Micro-USB B port (USB 1.1)
PWM Channels 16
Temperature Sensor On-board
Power Supply 5V via Micro-USB port or GPIO header

51mm x 21mm

About SW-420 Vibration Sensor:

SW-420 Vibration Sensor with Raspberry Pi Pico W

The SW-420 Vibration Sensor is a simple and effective component commonly used in many electronic projects, particularly for applications that require vibration, tilt, or movement detection. Here’s an overview of its specifications and pinout:


Operating Voltage: Typically ranges from 3.3V to 5V. This makes it compatible with most microcontrollers and development boards like Arduino and Raspberry Pi.

Current Consumption: It has a low current consumption, usually around 15 mA.

Sensitivity: The sensor is highly sensitive to vibration. It often includes a potentiometer to adjust the sensitivity threshold.

Digital Output: It usually provides a digital output signal. When the sensor detects vibration beyond a certain threshold, it outputs a high signal (logic HIGH/1); otherwise, it remains low (logic LOW/0).

Size and Form Factor: Compact and easy to integrate into circuits. The exact dimensions can vary based on the manufacturer.

Operating Temperature: Typically, it can operate in a wide range of temperatures, but this can vary by model and manufacturer.


The SW-420 Vibration Sensor module typically comes with three pins:

VCC (Power): This pin is connected to the power supply. It supports 3.3V to 5V, aligning with the operating voltage of the sensor.

GND (Ground): This is the ground pin and should be connected to the ground of the power source or the microcontroller.

DO (Digital Output): This pin outputs the digital signal based on the vibration detected. When vibration is detected above the set threshold, this pin will output a HIGH signal. It can be connected directly to a digital input pin on a microcontroller.


The SW-420 Vibration Sensor is often used in security systems, anti-theft devices, earthquake detection, vehicle movement detection systems, and in various DIY projects where vibration detection is required. The sensitivity can be adjusted using the onboard potentiometer. When the module detects vibration, the output is a digital signal which is easy to interface with microcontrollers and other digital circuits.

Wiring the SW-420 Vibration Sensor to Raspberry Pi Pico W

Before you can start using the SW-420 Vibration Sensor with the Raspberry Pi Pico W, you’ll need to wire the two components together. The SW-420 Vibration Sensor typically has three pins: VCC, GND, and D0. VCC is the power supply pin, GND is the ground pin, and D0 is the digital output pin that provides the vibration data.

SW-420 Vibration Sensor with Raspberry Pi Pico W

Connect the VCC pin of the SW-420 to one of the 3.3V pins on the Pico W.

Connect the GND pin to one of the ground (GND) pins on the Pico W.

Connect the DO (Digital Output) pin of the SW-420 to the GPIO15 on the Pico W.

Connect the positive leg of the LED through a 330-ohm resistor to the GPIO 14 pin of the Pico W, and connect the LED’s negative leg to the Pico W’s ground (GND) pin

Once the wiring is complete, you can move on to programming the Raspberry Pi Pico W to read data from the SW-420 Vibration Sensor.

SW-420 Vibration Sensor MicroPython code:

Steps to Run the Script:

Start by opening the IDE you like to use, like Thonny IDE or any other. Copy the program I provided and paste it into your IDE.

Next, click on ‘File’ at the top and choose ‘Save’.

SW-420 Vibration Sensor with Raspberry Pi Pico W

 A window will pop up asking you where to save the file. Select your Raspberry Pi Pico from this window.

SW-420 Vibration Sensor with Raspberry Pi Pico W

Name the file ‘main.py’ and then click ‘OK’ to save it on the Pico.

SW-420 Vibration Sensor with Raspberry Pi Pico W

By naming your script main.py, you’re ensuring that the functionality of the vibration sensor and LED indicator is immediately active following each boot-up of the Raspberry Pi Pico W. This is particularly useful for applications where you need the sensor to start working as soon as the device is powered.

Once the program saved, just press the ‘Run‘ button or the ‘F5‘ button on the keyboard.

SW-420 Vibration Sensor with Raspberry Pi Pico W

With this setup, whenever the SW-420 vibration sensor detects a vibration, it triggers the LED to turn on for 1 second. Simultaneously, a message ‘Vibration detected!’ will be printed on the console, as you can see.

SW-420 Vibration Sensor with Raspberry Pi Pico W

Code Explanation:

The SW-420 Vibration Sensor Micropython script begins with two crucial import statements. The first line, from machine import Pin, brings into the script the Pin class from the machine module. This class is pivotal for controlling the General Purpose Input/Output (GPIO) pins on the Raspberry Pi Pico W, allowing the script to interact with external hardware components like sensors and LEDs. The second line, import time, is equally essential as it imports the time module. This module is known for its time-related functions, which are utilized in this script to introduce delays. These delays are crucial for controlling the timing of events, like the duration for which an LED stays lit or the interval between sensor readings, ensuring that the script operates with the desired rhythm and responsiveness.

In the script, specific GPIO pins on the Raspberry Pi Pico W are initialized to interact with external components. The line vibration_sensor = Pin(15, Pin.IN) is dedicated to setting up GPIO 15 as an input pin. This is where the vibration sensor is connected. By using Pin.IN as an argument, the script configures this pin to receive input signals, which in this case, are the vibrations detected by the sensor. In a similar vein, the line led = Pin(14, Pin.OUT) is responsible for initializing GPIO 14, but unlike the previous pin, this one is set as an output pin, as indicated by Pin.OUT. This output pin is connected to an LED. By designating it as an output, the script can send signals to the LED, effectively controlling when it turns on or off. Together, these lines of code establish a fundamental link between the Raspberry Pi Pico W’s GPIO pins and the external hardware components, the vibration sensor, and the LED, enabling the device to interact with its physical environment.

The script includes a defined function named detect_vibration, which plays a crucial role in monitoring for vibrations. Inside this function, the first operation is a conditional check using if vibration_sensor.value(). This line is the heart of the vibration detection logic. It leverages the value() method to read the digital state of the pin connected to the vibration sensor. This method returns True if the pin state is HIGH, indicating that vibration has been detected. Upon detection, the script executes a series of actions. Firstly, it outputs the message “Vibration detected!” to the console, providing a clear, textual indication of vibration detection. Following this, the function activates an LED by executing led.on(), thereby providing a visual alert. To ensure the LED remains lit just long enough to be noticeable but not overly persistent, the script employs time.sleep(1). This function call pauses the script’s execution for exactly one second, maintaining the LED in its ‘on’ state for this duration. After the one-second pause, led.off() is called, turning the LED off and readying the system for the next vibration detection event. This combination of operations within the detect_vibration function creates a responsive and informative vibration detection system, visually signaling each detection event while also logging it to the console.

The script features a vital component, an infinite loop created by the statement while True. This loop is the driving force that ensures continuous operation of the code within it. At the heart of this loop is the call to the detect_vibration() function. By placing this function call inside the loop, the script is set up to perpetually monitor for vibrations. Each pass through the loop represents a new opportunity for the system to detect and respond to vibration. However, to maintain efficiency and prevent the system from overloading, there is a strategic pause introduced after each call to detect_vibration(). This is achieved through the time.sleep(0.1) command, which suspends the execution of the script for a brief period of 100 milliseconds, or 0.1 seconds. This short delay serves a dual purpose: it conserves processing power by preventing the script from running too rapidly and relentlessly, and it also enhances the script’s overall responsiveness. By integrating these elements—the continuous loop, the consistent monitoring for vibrations, and the judicious use of pauses—the script achieves a balance of vigilant monitoring and efficient resource utilization.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button