If you just need a binary alarm to know if a device fell over, buy the SW-520D mechanical tilt switch ($0.10/ea). If your project requires measuring the exact physical angle of tilt in degrees, buy the ADXL345 MEMS accelerometer ($2.50/ea) or a dedicated analog liquid tilt module. Conflating these two categories is the most common mistake in embedded sensor design; a mechanical switch cannot give you an angle, and a MEMS sensor is overkill for a simple theft alarm. Below is the exact wiring, raw-to-unit math, and interference mitigation for both architectures.
The Sensing Principle: Mechanical vs. Solid-State
Mechanical tilt switches (like the ubiquitous SW-520D or the omnidirectional SW-200D) rely on a conductive metal ball rolling between two electrodes inside a sealed, nitrogen-filled tube. When the sensor is tilted past its physical threshold (typically 15° to 45° depending on the model), gravity pulls the ball away from the contacts, breaking the circuit. It acts as a simple, unpowered SPST (Single Pole Single Throw) switch that only cares about crossing a binary threshold.
Continuous-measurement alternatives include solid-state MEMS accelerometers and liquid resistive sensors. MEMS chips (like the ADXL345) measure the static 1G gravity vector across X, Y, and Z silicon micro-cantilevers, calculating precise pitch and roll via trigonometry. Liquid tilt sensors use an electrolytic fluid moving across internal electrodes; the fluid acts as a variable resistor or capacitor, outputting a continuous analog voltage directly proportional to the tilt angle without requiring complex I2C/SPI register configuration.
Output Signals and Raw-to-Degree Math
The output of a mechanical tilt switch is strictly digital (a floating node or a pulled-up/pulled-down voltage rail). There is no physical unit math required, only time-domain debouncing. The output of an analog tilt module is a voltage (typically 0V to 3.3V), which must be scaled to degrees.
Analog Raw-to-Unit Math (ESP32 12-bit ADC)
For a standard 0-3.3V analog tilt module mapping -90° to +90°, you must convert the raw ADC integer to a voltage, subtract the zero-offset (the voltage when perfectly level), and apply the sensor's sensitivity. Here is the exact C++ math for an ESP32:
const int TILT_PIN = 34; // ESP32 ADC1 pin (avoid ADC2 if using WiFi)
const float V_REF = 3.3; // Reference voltage
const int ADC_MAX = 4095; // 12-bit resolution
const float ZERO_V = 1.65; // Voltage at exactly 0 degrees (calibrated)
const float SENSITIVITY = 18.5;// mV per degree (check your specific datasheet)
float getTiltAngle() {
int raw = analogRead(TILT_PIN);
float voltage = (raw * V_REF) / ADC_MAX;
// Calculate angle: (Voltage - ZeroOffset) / Sensitivity
float angle = (voltage - ZERO_V) / (SENSITIVITY / 1000.0);
// Clamp to physical limits
return constrain(angle, -90.0, 90.0);
}
Calibration and Scaling
Analog tilt sensors require a one-time zero-offset calibration. Mount the sensor perfectly level, read the raw ADC value 100 times, average it, and convert that to your ZERO_V constant. Mechanical switches require no scaling, but they require debounce time (typically 20ms to 50ms) to prevent a single physical tilt from registering as 10 rapid triggers due to contact bounce.
Wiring Table and Circuit Requirements
Both sensor types require specific pull-up or pull-down resistors to prevent floating GPIO pins from generating phantom interrupts. Below is the spec-sheet wiring table for the three most common modules used in 2026 hobbyist and prototyping environments.
| Sensor Module | Type | Supply Range | Output Pin | Required External Components |
|---|---|---|---|---|
| SW-520D (Bare) | Digital (Mechanical) | N/A (Passive) | GPIO (Interrupt) | 10kΩ pull-up to VCC, 100nF cap to GND |
| SW-520D Module | Digital (w/ Comparator) | 3.3V - 5.0V | DO (Digital Out) | None (LM393 handles pull-up) |
| Analog Liquid Tilt | Analog (Resistive) | 3.3V - 5.0V | AO (Analog Out) | 10µF decoupling cap on VCC, shielded wire |
| ADXL345 (MEMS) | Digital (I2C/SPI) | 2.0V - 3.6V | SDA / SCL | 4.7kΩ I2C pull-ups (if not on breakout) |
Interference Sources and Signal Conditioning
Tilt sensors operate in high-vibration, high-EMI environments (e.g., automotive, industrial enclosures, robotics). Failing to condition the signal will result in ghost triggers or noisy angle readings.
- Contact Bounce (Mechanical): When the ball inside an SW-520D breaks contact, it physically micro-bounces for 5 to 20 milliseconds. Fix: Add a 100nF ceramic capacitor directly across the switch pins to form a low-pass RC filter, and implement a 50ms software ignore-window after the first edge trigger (All About Circuits: Switch Debouncing).
- Vibration False-Triggering (Mechanical): High-frequency vibration (like a motor running) can cause the ball to momentarily lift off the contacts. Fix: If using a mechanical switch near motors, switch to an omnidirectional SW-200D (which requires a full 360° roll to trigger) or upgrade to a solid-state MEMS sensor with a built-in high-pass filter.
- High-Impedance Noise (Analog): Analog liquid tilt sensors have high output impedance. Running a 2-meter unshielded cable to an Arduino ADC will act as an antenna, picking up 50/60Hz mains hum. Fix: Place a 10kΩ resistor in series with the analog output, followed by a 100nF capacitor to ground at the microcontroller pin, creating a hardware low-pass filter at ~160Hz.
- I2C Bus Capacitance (MEMS): When wiring an ADXL345 via I2C, long wires add parasitic capacitance, corrupting the SDA/SCL square waves. Fix: Keep I2C traces under 30cm, or use an I2C bus extender (like the PCA9600) for remote mounting.
Decision Matrix: Choosing Your Tilt Sensor
Do not default to the most expensive sensor. Use this decision tree to select the exact part number for your BOM based on your physical and data requirements.
| Project Requirement | If YES... | If NO... |
|---|---|---|
| Do you need the exact angle in degrees (Pitch/Roll)? | Go to Row 2 | You only need a binary 'Fallen Over' alert. Buy: SW-520D |
| Do you need to measure static tilt without complex I2C code? | Buy: Analog Liquid Tilt Module (e.g., DFRobot SEN0018) | Go to Row 3 |
| Do you need high precision (<1° error), shock detection, or tap-gestures? | Buy: ADXL345 or MPU6050 (MEMS) | Re-evaluate; you likely just need a basic SW-520D. |
The Final Verdict and Default Pick
For 90% of DIY security alarms, smart-trash-can lids, and 'device-tamper' switches, the SW-520D is the definitive choice. It draws zero quiescent current (crucial for battery-powered ESP32 deep-sleep wake-up circuits), costs pennies, and requires only a single GPIO interrupt. Wire it with a 10kΩ external pull-up to 3.3V, add a 100nF debounce capacitor, and configure your microcontroller's interrupt to trigger on the falling edge.
However, if your application is a camera gimbal, a solar panel sun-tracker, or a robotic arm joint where the physical angle dictates the next control loop step, abandon mechanical switches entirely. Use the ADXL345 via I2C, leveraging the SparkFun ADXL345 Hookup Guide library to pull the raw X/Y/Z registers and calculate the atan2() vectors. Select the architecture first, condition the signal second, and your embedded system will survive the real world.






