Tilt Sensor · Astro Tech Blog

Tilt Sensor

A tilt sensor is a simple electromechanical switch that opens or closes when tilted past a certain angle. It contains a metal ball or conductive liquid that rolls to one end, making or breaking contact between the two pins. It is commonly used for orientation detection, tilt alarms, vibration sensing, and game controllers.

Tilt Sensor Module

For this interfacing you need the following components:

  • Arduino board (Uno, Nano, Mega, etc.)
  • Tilt sensor (SW-520D, SW-200D, or mercury-free ball switch)
  • 10k ohm resistor (pull-down)
  • Breadboard and jumper wires
  • USB cable to connect Arduino to your computer

Schematic

Connect the tilt sensor to the Arduino as follows:

Tilt Sensor           Arduino
------------          -------
Pin 1         -->     Digital Pin 2
Pin 2         -->     GND

Connect a 10k ohm pull-down resistor from Digital Pin 2 to GND so the pin reads LOW when the switch is open. Alternatively, use the Arduino’s internal pull-up by configuring INPUT_PULLUP and connecting the sensor between the pin and GND (logic is then inverted).

With internal pull-up:
Tilt Sensor           Arduino
------------          -------
Pin 1         -->     Digital Pin 2
Pin 2         -->     GND
// No external resistor needed - use pinMode(pin, INPUT_PULLUP)

Pin Map

Tilt PinConnection
Pin 1Digital pin 2 (with 10k ohm pull-down) or directly with INPUT_PULLUP
Pin 2GND

Tilt sensors are non-polarized — the two pins are symmetric and can be connected either way.

Install necessary Library

No external library is required. A tilt sensor reads like a digital switch using standard digitalRead().

Code with complete explanation

This sketch reads the tilt sensor state and prints a message to the Serial Monitor when the sensor is tilted.

const int tiltPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(tiltPin, INPUT_PULLUP); // Use internal pull-up
}

void loop()
{
  int sensorValue = digitalRead(tiltPin);

  // With INPUT_PULLUP: LOW = tilted (switch closed to GND)
  // HIGH = upright (switch open, pulled HIGH by internal resistor)
  if (sensorValue == LOW)
  {
    Serial.println("Tilted!");
    delay(200); // Debounce
  }
  else
  {
    Serial.println("Upright");
  }

  delay(100);
}

Code breakdown

  • pinMode(tiltPin, INPUT_PULLUP) — enables the Arduino’s internal 20k-50k ohm pull-up resistor, eliminating the need for an external resistor. The pin reads HIGH by default (open switch) and LOW when the sensor closes to GND.
  • digitalRead(tiltPin) — returns LOW when the sensor is tilted past its threshold angle (ball/conductor bridges the pins) and HIGH when upright (no connection).
  • With an external pull-down resistor instead, the logic is inverted: HIGH = tilted, LOW = upright.
  • The delay(200) provides debouncing — mechanical tilt switches can bounce when transitioning.

Using an external pull-down resistor

If you prefer active-HIGH output (HIGH when tilted):

const int tiltPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(tiltPin, INPUT); // No internal pull-up
}

void loop()
{
  int sensorValue = digitalRead(tiltPin); // HIGH = tilted

  if (sensorValue == HIGH)
  {
    Serial.println("Tilted!");
  }
  else
  {
    Serial.println("Upright");
  }

  delay(100);
}

This requires an external 10k ohm resistor connecting the pin to GND as shown in the first schematic.

Steps to perform this interfacing

  1. Connect the tilt sensor to the Arduino with a pull-down resistor (or use INPUT_PULLUP).
  2. Copy the code into the Arduino IDE.
  3. Select the correct board and port (Tools > Board and Tools > Port).
  4. Upload the sketch to the Arduino.
  5. Open the Serial Monitor (Tools > Serial Monitor, set baud rate to 9600).
  6. Hold the sensor upright and observe “Upright” messages.
  7. Tilt the sensor past its threshold angle and observe “Tilted!” messages.

Caution

  • Older tilt sensors may contain mercury, which is toxic and hazardous. Use mercury-free ball-switch sensors (SW-520D, SW-200D) for safe handling.
  • Tilt sensors are mechanical switches and have a limited lifespan (typically 100,000 to 1,000,000 cycles). They will wear out over time.
  • The tilt threshold angle varies by model. Some sensors trigger at 15 degrees, others at 45 degrees. Check the datasheet for your specific part.
  • Vibration can cause false triggering. Add a debounce delay (100-300 ms) in software to filter out rapid transitions.
  • The sensor’s orientation matters — most tilt sensors are directional and detect tilt in one specific axis. Mount it according to the desired detection direction.
  • Do not exceed the sensor’s maximum current rating (usually 10-20 mA). The Arduino’s internal pull-up (limited to ~200 µA) is safe. If using an external pull-down, a 10k ohm resistor limits current to 0.5 mA at 5V.