Detecting Movement with a Tilt Sensor

Draft. This tutorial has not been checked against the physical product yet. Confirm wiring, pin assignments and voltage limits against the product documentation before building it.
DifficultyBeginner
CategoryInputs
Supported boardsArduino Uno R3 and compatible ATmega328P boards
Time needed35 minutes
PrerequisitesRead a Push Button
Tested boardNot yet verified
Tested onNot yet verified
Arduino IDE versionNot yet verified
LibraryNot yet verified

What it does

A tilt sensor is a switch that is operated by gravity instead of a finger. Inside the small metal cylinder is a rolling ball; tip the sensor past a certain angle and the ball rolls onto two contacts and closes the circuit.

That makes it a push button that presses itself when the thing it is mounted on moves. It suits anything that needs to notice being disturbed: a parcel alarm, a drawer monitor, a toy that reacts when you pick it up, a machine that must stop if it is knocked over.

What it is not is an angle sensor. It tells you tilted or not tilted, with no reading in between. For an actual angle you want the MPU-6050.

Before you begin

Required parts and tools

  • An Arduino Uno or compatible board.
  • One tilt sensor module.
  • Three jumper wires.

The board Siratek sells

The module on the Siratek product page has three pins, marked DO, GND and VCC, a blue adjustment screw, two small indicator LEDs, and an LM393 comparator chip.

That tells you several useful things before you wire anything.

  • The output is digital only: tilted or not. There is no analog pin on this board.
  • One LED shows power and the other shows the output state, so you can see the sensor working without a single line of code.
  • The comparator means the output is a clean switched signal, not a bouncing contact passed straight through, though the ball inside can still chatter as it rolls.
  • The blue screw adjusts the comparator’s threshold. On a simple tilt switch there is little to adjust, and it is usually best left where it is. If the output never changes, that screw is the first thing to look at.

Which way means tilted

The sensor’s resting orientation decides everything, and the output may read HIGH when level and LOW when tilted, or the other way round. This varies between boards.

Do not guess it. The first sketch below prints the raw state, so you can tip the sensor and see which reading means what. Thirty seconds of watching saves an afternoon of inverted logic.

Safety notes

Low voltage throughout, and nothing here can hurt you. Wire it with the USB cable unplugged as usual.

Pin reference

Pin or label Connect to Purpose Notes
VCC Uno 5V Power The module has its own regulation and indicators
GND Uno GND Common ground
DO Uno D2 Digital output, tilted or not Read with plain INPUT

Read this with pinMode(pin, INPUT), not INPUT_PULLUP. The module drives the line itself, so it needs no help holding a level, unlike a bare push button.

A tilt sensor module with VCC to 5V, GND to ground and its digital output on pin 2
Read it with plain INPUT. The board drives the pin both ways.

Wiring steps

  1. Unplug the USB cable.
  2. Connect GND to a GND pin on the Uno.
  3. Connect VCC to 5V.
  4. Connect DO to digital pin 2.
  5. Plug the USB cable back in. The power LED on the module should light.
  6. Tip the module slowly and watch the second LED change. If it does, the sensor works and anything that goes wrong next is in your code.

Software and libraries

No library is required.

Minimal working example

Run this first, and tip the sensor while watching the Serial Monitor. The point is to learn which reading means tilted on your board.

// Siratek: reading a tilt sensor, and finding out which state means tilted.

const int tiltPin = 2;

void setup() {
  pinMode(tiltPin, INPUT);
  Serial.begin(9600);
  Serial.println("Siratek: tip the sensor and watch the reading.");
}

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

  Serial.print("reading = ");
  Serial.println(state);

  delay(200);
}

Acting on a change

Once you know which value means tilted, react to the moment it changes rather than to the state, and ignore the chatter as the ball rolls. This is the debounce pattern from Making a Button Behave, applied to a different switch.

// Siratek: reporting when the sensor is disturbed.

const int tiltPin = 2;
const int ledPin = 9;

const int tiltedReading = LOW;          // Change to HIGH if yours reads the other way.
const unsigned long settleTime = 80;    // The ball chatters as it rolls.

int steadyState = HIGH;
int lastReading = HIGH;
unsigned long lastChange = 0;
int disturbances = 0;

void setup() {
  pinMode(tiltPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Siratek: watching for movement.");
}

void loop() {
  unsigned long now = millis();
  int reading = digitalRead(tiltPin);

  if (reading != lastReading) {
    lastChange = now;
  }
  lastReading = reading;

  if (now - lastChange >= settleTime && reading != steadyState) {
    steadyState = reading;

    if (steadyState == tiltedReading) {
      disturbances++;
      digitalWrite(ledPin, HIGH);
      Serial.print("disturbed ");
      Serial.println(disturbances);
    } else {
      digitalWrite(ledPin, LOW);
      Serial.println("back to rest");
    }
  }
}

The one line to edit is tiltedReading. Set it to whatever the first sketch showed when you tipped the sensor.

Expected result

The first sketch prints a steady stream of 0 or 1, changing as you tip the module past its switching angle. The change is abrupt rather than gradual, because the ball either bridges the contacts or does not.

The second prints a numbered line each time the sensor is disturbed, and lights the LED while it stays tilted. Shaking it should give one count per genuine movement rather than a burst.

The switching angle is not adjustable and is set by the sensor’s construction. Mounting the module at a slight angle is how you change the point at which it triggers.

Troubleshooting

Symptom Likely cause Check or fix
Power LED dark VCC or GND not connected Check both before anything else
Output never changes Threshold screw turned too far Turn it back slowly while tipping the module
Output never changes, LED does Wrong pin, or wrong pin number in the sketch DO to D2, and match the sketch
Reads inverted Your board rests the other way Change tiltedReading to the other value
One movement, many counts The ball chattering Raise settleTime to 120 or 150
Triggers at the slightest knock Mounted near its switching angle Mount it further from the trigger point
Never triggers however far you tip Sensor mounted the wrong way up Turn the module over
Random triggering when still Vibration from something nearby Expected; this part cannot tell the difference

Next steps

  • Easier first: Read a Push Button, which is the same idea with a finger instead of gravity.
  • Related: Making a Button Behave, for the settling pattern used above.
  • Next project: add the active buzzer for a parcel alarm that sounds when a box is moved.

Sources and attribution

All text, wiring guidance and code in this tutorial are original Siratek work.

  • Arduino core documentation for digitalRead() and millis().
  • The board’s pin labels, indicator LEDs and comparator are described from the photographs on the Siratek product page, and should be confirmed against the part in hand.

No third-party tutorial text, diagrams, photographs or code has been reproduced here.

Product used in this tutorial: Tilt Sensor Module
Shop
Sidebar
0 Wishlist
0 Cart