Back to Tutorial

Tilt Sensor Module SW520D


🤔 What is a Tilt Sensor?

tilt sensor (also called a ball switch or mercury switch) is a digital switch that changes its output based on orientation. When tilted beyond a certain angle, the internal mechanism (metal ball or mercury drop) makes or breaks contact, changing the signal.

📌 Key Features:

  • Simple ON/OFF output
  • Detects if the object is upright or tilted
  • Digital signal (HIGH or LOW)
  • No orientation angle info — only tilt or no tilt

📦 Types of Tilt Sensors:

TypeInside itBehavior
Metal ball typeRolling metal ballConducts when level
Mercury typeLiquid metal dropConducts when tilted
MEMS Tilt ModuleAccelerometer-basedGives angle info

We’ll focus on the basic ball type, which is easiest to use with ESP32.


🧰 Components Needed:

  • ESP32 board
  • Tilt sensor module (KY-017, SW-520D, or similar)
  • Jumper wires
  • LED (optional for output)

🧪 How It Works:

  • Inside the sensor is a small metal ball.
  • When upright → ball connects two metal contacts → Circuit closed → Output LOW
  • When tilted → ball breaks contact → Circuit open → Output HIGH (or floating)

Some modules have built-in pull-up resistors and LED to indicate state.


🔌 Wiring Tilt Sensor to ESP32

Tilt Sensor PinESP32 Pin
VCC3.3V or 5V
GNDGND
OUTGPIO 14 (or any digital pin)

 

🧠 What This Code Does:

  • Reads digital value from the sensor.
  • If LOW: the sensor is upright.
  • If HIGH: the sensor is tilted.
  • LED shows the state.

💡 Real-world Applications

Use CaseDescription
🛠️ Anti-Tamper AlarmAlert when a box/device is moved
🔒 Laptop SecurityLock or alert if tilted or stolen
🎮 Game ControllerBasic motion control in DIY games
🛑 Vehicle Crash AlertDetect if a device tips over
📦 Package MonitoringDetect tilting during delivery/shipping

 

🛠️ Sensor Not Working? Troubleshooting:

ProblemFix
No responseCheck VCC, GND, and OUT connection
Inverted readingsTry changing logic (HIGH/LOW check)
Floating outputAdd external pull-up/down resistor
No LED on moduleTry tilting more or check supply voltage
HOW TO OPERATE
 
// #include <LiquidCrystal_I2C.h>

#define MOI 15

// LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display


void setup() {
 pinMode(MOI,INPUT);
  Serial.begin(9600);
  Serial.println("MOISTURE START");
// lcd.init();
//  lcd.backlight();
//   lcd.setCursor(3,0);
//   lcd.print("moisture data");
//   lcd.setCursor(3, 1);
}

void loop() {
 
 int data=analogRead(MOI);
 Serial.println(data);
// lcd.print(data);
 delay(1000);
//  lcd.clear();

}

Tilt Sensor Module SW520D

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial