A 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.
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
| Type | Inside it | Behavior |
|---|---|---|
| Metal ball type | Rolling metal ball | Conducts when level |
| Mercury type | Liquid metal drop | Conducts when tilted |
| MEMS Tilt Module | Accelerometer-based | Gives angle info |
We'll focus on the basic ball type, which is easiest to use with ESP32.
ESP32 board
Tilt sensor module (KY-017, SW-520D, or similar)
Jumper wires
LED (optional for output)
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.
| Tilt Sensor Pin | ESP32 Pin |
|---|---|
| VCC | 3.3V or 5V |
| GND | GND |
| OUT | GPIO 14 (or any digital pin) |
Reads digital value from the sensor.
If LOW: the sensor is upright.
If HIGH: the sensor is tilted.
LED shows the state.
| Use Case | Description |
|---|---|
| ๐ ๏ธ Anti-Tamper Alarm | Alert when a box/device is moved |
| ๐ Laptop Security | Lock or alert if tilted or stolen |
| ๐ฎ Game Controller | Basic motion control in DIY games |
| ๐ Vehicle Crash Alert | Detect if a device tips over |
| ๐ฆ Package Monitoring | Detect tilting during delivery/shipping |
| Problem | Fix |
|---|---|
| No response | Check VCC, GND, and OUT connection |
| Inverted readings | Try changing logic (HIGH/LOW check) |
| Floating output | Add external pull-up/down resistor |
| No LED on module | Try tilting more or check supply voltage |