Arduino Uno Tilt Sensor: Industrial Stability Monitor

Professional ball-bearing tilt switch module contains precision steel sphere rolling within conductive barrel electrodes creating <0.5Ω contact closure when tilted >15° from horizontal. Mercury-free design survives 10M cycles with 5° hysteresis preventing chatter during boundary conditions.

Digital pin 2 interrupt captures instantaneous state changes with LM393 comparator providing clean 5V TTL output. Adjustable sensitivity pot tunes 10-45° trigger threshold. 3.3-5V operation, <1μA quiescent current, 360° omnidirectional response.

How It Works

A mercury tilt switch is a sealed glass capsule containing a bead of mercury and two electrodes. Tilt the capsule one way and the mercury flows across both electrodes, closing the circuit; tilt it back and the connection opens. The KY-017 module packages this with a resistor and indicator LED.

Mercury makes a genuinely excellent contact — it is liquid, so there is no wear, no pitting and no oxidation, and the switching action is clean. That is why these switches were used for decades in thermostats and tilt alarms.

They are also hazardous. Mercury is toxic and the capsule is glass; a broken switch is a contamination problem, and these parts are restricted or banned in many jurisdictions under RoHS and similar regulations. Modern designs use a ball tilt switch instead, where a conductive ball rolls between contacts. It behaves almost identically and the KY-020 is a drop-in replacement.

Both types bounce as the mercury or ball settles, so the same debouncing a mechanical switch needs applies here — and the settling time is typically longer.

Components Needed

  • Arduino Uno
  • Ball-Bearing Tilt Sensor Module (SW-520D)
  • N52 Neodymium test weights
  • Male-to-male jumper wires (3 pieces)
  • Arduino Uno
  • 85dB piezo siren + 220Ω LED driver

Wiring to the Arduino Uno

Connect S to D2, the middle pin to the 5 V rail and − to GND. Declaring the pin as INPUT_PULLUP gives a defined idle level whichever way the carrier is wired.

Orientation is the whole point of this sensor, so mount it deliberately. Note the angle at which it switches — typically 15–30° from its reference position — and fix it so that angle corresponds to the tilt you care about.

Handle the glass capsule gently and never force the leads close to the body. If a mercury capsule breaks, treat it as hazardous waste rather than sweeping it up.

Module pinArduino Uno pinFunction
S (signal)D2Switch state — HIGH or LOW depending on tilt
Middle / +5VSupply
− (GND)GNDCommon ground

Example Code

Debounced tilt detection, reporting only settled changes. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.

Debounced tilt detection, reporting only settled changes
const int TILT_PIN = 2;
const unsigned long SETTLE_MS = 80;   // longer than a dry contact — the bead rolls

int stable = HIGH;
int lastRead = HIGH;
unsigned long lastChange = 0;

void setup() {
  Serial.begin(9600);
  pinMode(TILT_PIN, INPUT_PULLUP);
}

void loop() {
  int reading = digitalRead(TILT_PIN);

  if (reading != lastRead) {
    lastChange = millis();
    lastRead = reading;
  }

  if (millis() - lastChange > SETTLE_MS && reading != stable) {
    stable = reading;
    Serial.println(stable == LOW ? "TILTED" : "upright");
  }
}

Applications

A tilt sensor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Tilt and tip-over alarms on equipment, heaters and machinery
  • Orientation detection in handheld and wearable projects
  • Anti-theft triggers that fire when an object is lifted or moved
  • Simple game controllers responding to board tilt
  • Automatic shutoff for appliances that must not run on their side

Working with the Arduino Uno

The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.

With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.

The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.

Arduino Uno characteristicValueWhy it matters here
Logic voltage5 VMatches most hobby modules directly
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsA0–A5 (six channels)Determines how many analog sensors can share the board
PWM outputsD3, D5, D6, D9, D10 and D11Needed for brightness, speed and tone control
I²C pinsA4 (SDA) and A5 (SCL)Fixed by hardware — wiring copied from another board may not match
Interrupt pinsD2 and D3 onlyRequired for counting fast or asynchronous events
Seriala single hardware UART shared with USBMonitor runs at 9600 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • The state flickers around the switching angle — that is the bead settling. Lengthen the debounce window.
  • It never changes — the switch is mounted at the wrong angle; rotate it and retest through its full travel.
  • The output is inverted compared with expectations — carrier wiring varies, so simply swap the comparison.
  • Readings are unstable with vibration present — tilt switches cannot distinguish vibration from tilt. Use an accelerometer such as the MPU6050 for anything that moves.
  • Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
  • An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).

Taking It Further on the Arduino Uno

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:

The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.

Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.

Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.

Notes and Practical Limits

Prefer the ball-type KY-020 over a mercury KY-017 for any new build. The electrical behaviour is the same, the code is identical, and it avoids both the toxicity and the regulatory problem entirely.

A tilt switch reports one axis crossing one threshold. For actual angle measurement in three dimensions, an accelerometer is the right part — it reports continuous orientation rather than a single binary transition.