Labs / Electronic modules

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

Author: Li Zisheng · Source: FreakStudio open documentation, v1.3 · License: CC BY 4.0 · Driver code (sensors/vibration_driver): MIT License

Contents
GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)
GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

I. Module Introduction

Ball-type vibration sensor module based on SW-520D The core component is SW-520D , a single-directional tilt/vibration detection module based on the ball principle. The core adopts a bimetallic ball design, and the circuit is turned on and off through the rolling contact guide pin of the ball. It is an environmentally friendly device that replaces the traditional mercury switch. Its core characteristics and application scenarios are as follows:

The relationship and working principle among vibration switches, ball switches and spring switches:

Vibration switch is a general term, which is mainly divided into two categories in the industry: spring switches and ball switches. A spring switch realizes the on-off of the circuit through the deformation of its internal spring caused by vibration force to contact with or separate from the pins, which is suitable for vertical installation and focuses on detecting the magnitude of vibration force. A ball switch makes the metal ball roll to conduct the circuit through angle change, which is suitable for flat installation and focuses on detecting the tilt angle. The two together constitute a vibration sensing element that converts vibration or angle signals into electrical signals by means of mechanical structure displacement.

Common vibration switches SW series fall into two categories:

  • SW-18xxx Spring-loaded vibration switches (e. g., SW-18010P, SW-18015P, etc.):
    • Inside the component, there is a thin metal spring and a central electrode, which are separated under normal conditions; when vibration occurs, the spring shakes to come into contact with the electrode, thus realizing on/off switching.
    • Features: High sensitivity, capable of detecting slight vibrations, but prone to generating jitter signals.
  • SW-20xx Ball-type vibration switches (e. g., SW-200D, SW-200DR, etc.):
    • Inside the device, there is a small metal ball placed in a cylinder; when subjected to vibration, the ball moves to come into contact with the electrode.
    • Features: Relatively low sensitivity, but more stable triggering and better anti-jitter performance.

👉 So the SW-18010P you asked about belongs to the spring-switch type vibration sensor.

This section of knowledge is referenced from: Understanding the Relationship and Working Principles Among Vibration Switches, Ball Switches, and Spring Switches.

II. Introduction to Major Chips

This module mainly relies on SW-520D bimetal ball type vibration/tilt induction trigger switch to realize signal changes.

2.1 Chip Overview

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)
GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

2.2 Pin Definition

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

SW-520 (single bead) and SW-520D (double beads) both feature a 2-pin plug-in structure. The bottom view shows two gold-colored conductive pins arranged in parallel with a spacing of approximately 1.5mm:

This symmetrical non-polarity design enables more flexible installation and is suitable for automated control in scenarios such as inclination and vibration.

2.3 Functional Block Diagram

None.

2.4 Performance Parameters

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

2.5 Reference Circuit

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

2.6 Chip Configuration

None

None.

2.8 PCB Routing Precautions

None.

III. Circuit Engineering

3.1 Circuit Source Engineering

To obtain the source project of the circuit, please follow the official account [Freak Embedded]

3.2 Module Usage Instructions

The schematic diagram of this module is shown below:

SCH_Ball-Type Vibration Sensor Module Based on SW-520D_v1.0_2025-09-21.pdf 2.8 MB

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)
GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

This module uses digital output DIN: when stationary DIN outputs a high level, and when vibrating DIN outputs a low level.

It also features a dual-indicator design: the power indicator (LED1) staying on indicates the module is powered on; the vibration indicator (LED2) being on indicates vibration is detected.

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

The module takes SW-520D as the core, and cooperates with components such as the voltage comparator LM393, indicator light, filter capacitor, etc. , to convert mechanical vibration into a stable electrical signal output. The overall circuit is divided into the following parts:

3.3 Application Experiment

Here, we use the GraftPort-RP2040 development board for testing, and connect the ball-type vibration sensor module based on SW-520D to communicate with the main controller via the DIO interface.

Here, we first use the HY2.0-4P connection cable to connect the module to the GraftPort-RP2040 development board's DIO0 interface:

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

The module connections are shown in the following table:

Development Board Pins

Module Pins

Description

Pin 6

DIN

Sensor level signal input pin

Pin 7

NC

-

Here, we need to search for the relevant driver packages on uPyPI: https://upypi.net/zh/

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

Enter vibration_driver to search, and the following will be displayed:

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

Click to copy the local installation command:

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

Here, we can use the mip tool to import module driver library code. The following are the usage commands for the mpremote tool:

mpremote mip install github:FreakStudioCN/GraftSense-Drivers-MicroPython/sensors/vibration_driver

After importing the vibration sensor module - MicroPython driver code, copy the following main.py code into our project:

# MicroPython v1.23.0
# -*- coding: utf-8 -*-   
# @Time    : 2025/8/23 PM 4:08
# @Author  : 缪贵成
# @File    : main.py
# @Description : Rolling ball vibration sensor driver test file

# ======================================== Import Related Modules ==========================================

import time
from machine import Pin
from vibration_sensor import VibrationSensor

# ======================================== Global Variables =============================================

# ======================================== Functions ==============================================

def vibration_callback() -> None:
    """
    Vibration callback function, triggered when vibration is detected.

    Notes:
        This function is triggered by an interrupt and scheduled for execution through micropython.schedule.

    ==========================================

    Vibration callback function, triggered when vibration is detected.

    Notes:
        This function is called from interrupt, scheduled via micropython.schedule.
    """
    print("Vibration detected callback triggered!")

# ======================================== Custom Classes =============================================

# ======================================== Initialization Configuration ============================================

# Power-up delay to ensure hardware stability
time.sleep(3)
print("FreakStudio: Vibration Sensor Test Start")
# Initialize the vibration sensor, GPIO pin 6 as input, handled by the callback function
sensor = VibrationSensor(pin=Pin(6), callback=vibration_callback, debounce_ms=10)
sensor.init()
print("Sensor initialized with callback and debounce 50ms.")

# ======================================== Main Program ===============================================

try:
    start_time = time.ticks_ms()
    while True:
        # Poll to read the sensor status
        current_state: bool = sensor.read()
        print(f"Current vibration state: {current_state}")

        # Print the status dictionary every 2 seconds
        if time.ticks_diff(time.ticks_ms(), start_time) > 2000:
            status: dict = sensor.get_status()
            print(f"Sensor status: {status}")
            start_time = time.ticks_ms()

        time.sleep(0.2)

except KeyboardInterrupt:
    # Exit on user interrupt
    print("KeyboardInterrupt detected. Exiting test...")

finally:
    # Safely release resources
    sensor.deinit()
    print("Sensor deinitialized. Test completed.")

Burn the code, open the terminal, and use the mpremote tool to connect to the main controller. The output is as follows:

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)

As you can see, when we shake the sensor by hand, the relevant interrupt callback function will be triggered continuously:

IV. Drivers

vibration_driver.zip 10 KB

None.

VI. Revision History

Document Version

Date of Modification

Modifier

Modified Content

v1.0

2025-09-21

Li Zisheng

Initialize the document

The BOM sheet has not been finally checked, the driver has not been re-verified, and the 3.3 application experiment

Not yet specified.

v1.1

2025-12-29

Li Zisheng

Added application experiments and production files

v1.2

2025-12-31

Xin Ruili

Create an open version of the document based on the internal document version v1.1.

v1.3

2026-03-29

Xin Ruili

Modified the application experiment section

VII. Relevant Materials

7.1 Chip Datasheet

C5379904_Vibration Sensor_SW-520D_Datasheet_SW-520D.PDF 1.7 MB

7.1 Unique Icons for PCB Silk Screen

GraftSense: Ball-Type Vibration Sensor Module Based on SW-520D (Open Version)