
1. Introduction
1, SW-520, SW-520D is a ball-type tilt vibration induction single-directional trigger switch.
2, When the product tilts towards the cap end and the tilt angle is greater than 10 degrees, it is in the open circuit OFF state. When the product changes its horizontal state (and the wires are parallel to the plane direction, with the same height up and down), and the trigger end (gold-plated pin ends A and B) is lower than the horizontal tilt angle by more than 10 degrees, it is in the closed circuit ON state.
2. Schematic
Tilt Sensor-HS-S21P SchematicClick to view
Module Parameters
Pin Name | description |
|---|---|
G | GND (Negative Power Input) |
V | VCC (Positive Power Input) |
S | Digital Signal Pin |
Power Supply Voltage: 3.3V / 5V
Connection method: PH2.0 terminal wire
Installation Method: Double Screw Fixed
4, Circuit Board Size

5 of Arduino IDE example program
Attention: If prompted with an error message about the library file during program upload, please import the library file first!
Arduino IDE Library Download and Import Tutorial:Click to view
Example program (UNO development board):
int ledpin = 6; //定义数字6引脚为LED灯的引脚
int tiltSwitchpin = 4; //设置数字4引脚为倾斜开关的引脚
int val; //定义变量值
void setup()
{
Serial.begin(9600);//设置串口波特率
pinMode(ledpin, OUTPUT);//设置数字6口模式,OUTPUT为输出
pinMode(tiltSwitchpin, INPUT); //设置数字4口模式,INPUT为输入
}
void loop()
{
val = digitalRead(tiltSwitchpin); //读取数字4口值分配给 val
Serial.println(val);//串口打印读取val的值
if (val == LOW) //检测倾斜开关是否连通,连通输出低电平(0)
{
digitalWrite(ledpin, HIGH); //输出高电平,LED 亮起
}
else //检测倾斜开关是否断开,断开输出高电平(1)
{
digitalWrite(ledpin, LOW); //输出低电平,LED 熄灭
}
}6, ESP32 Python Example (for Mixly IDE/Misashi)
Choose the development board Python ESP32 [ESP32 Generic(4MB)] and upload in code mode
Attention: If prompted with an error message about the library file during program upload, please import the library file first!
Download and import tutorial for Mixly IDE ESP32 library:Click to view
Example program (ESP32-Python):
import machine
import time
pin2 = machine.Pin(2, machine.Pin.IN)
pin4 = machine.Pin(4, machine.Pin.OUT)
while True:
if pin2.value() == 0:
pin4.value(1)
else:
pin4.value(0)
time.sleep_ms(500)7, Mixly example program (graphical language)
Example program (UNO development board):Click to download
Attention: If prompted with an error message about the library file during program upload, please import the library file first!
Download and import tutorial of Mixly IDE Arduino library:Click to view

Example Program (ESP32 Development Board):Click to download
Attention: If prompted with an error message about the library file during program upload, please import the library file first!
Download and import tutorial for Mixly IDE ESP32 library:Click to view

8. Setting up the Test Environment
Arduino UNO Test Environment Setup
Prepare Components:
HELLO STEM UNO R3 DEVELOPMENT BOARD *1
HELLO STEM UNO R3 P EXPANSION BOARD *1
USB TYPE-C DATA CABLE *1
LED module (HS-F08P) *1
Tilt sensor (HS-S21P) *1
PH2.0 3P terminal to DuPont wire *2 or PH2.0 3P dual-head terminal wire *2

ESP32 Test Environment Setup
Prepare Components:Pending update...
Circuit wiring diagram:Pending update...
9, Video tutorial
Video tutorial:Click to view
10, Test results
Arduino UNO test results:
After the device is connected to the wire, upload the above program to the Arduino UNO development board, and then open the serial monitor.After the tilt switch is tilted to a certain angle, the serial port outputs a low-level value of 0, and the light turns on.After returning to the regression level, the serial port outputs a high level value of 1, and the light is off.By learning the principles and applications of the tilt switch.The tilt switch is a very simple electronic component, but simple devices can usually make some interesting things.

ESP32 Test Results:
Pending update...