Guide for the Tilt Sensor (inclinometer) with Arduino

This post shows how to use the tilt sensor module with the Arduino. The tilt sensor is many times referred to as inclinometer, tilt switch or rolling ball sensor. Using a tilt sensor is a simple way to detect orientation or inclination.

Introducing the Tilt Sensor Module

The tilt sensor module is the one in the following figure.

dsc09409

The tilt sensor allows to detect orientation or inclination. It detects if the sensor is completely upright or if it is tilted.

This makes it very useful to be used, for example, in toys, robots and other appliances whose working methodology depends on inclination.

How does it work?

The tilt sensor is cylindrical and contains a free conductive rolling ball inside with two conductive elements (poles) beneath.

tilt-sensor-how-it-works

Here’s how it works:

  • When the sensor is completely upright, the ball falls to the bottom of the sensor and connects the poles, allowing the current to flow.
  • When the sensor is tilted, the ball doesn’t touch the poles, the circuit is open, and the current doesn’t flow.

This way, the tilt sensor acts like a switch that is turned on or off depending on its inclination. So, it will give digital information to the Arduino, either an HIGH or a LOW signal.

Where to buy?

You can go to Maker Advisor and find the sensor’s best price.

Pin wiring

Wiring the tilt sensor to you Arduino is pretty straightforward. You just need to connect one pin to an Arduino digital pin and GND to GND.

If you connect the sensor like so, you need to activate the arduino internal pull-up resistor for the digital pin to which your sensor is connected to. Otherwise, you should use a 10kOhm pull up resistor in your circuit.

pin-wiring_bb

Example: Tilt sensitive LED

This is just a simple example for you to start put hands on your tilt sensor.

In this example, an LED will be turned off if the sensor is upright, and will be turned on if the sensor is tilted.

Parts required

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

For this example, you just need to add an LED to the schematics in the “Pin Wiring” section.

tilt-sensitive-led_bb

Code

To complete this example, upload the following code to your Arduino board.

/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */
 
 
int ledPin = 12;         
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH;   // the previous reading from the tilt sensor
    
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void setup(){
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop(){
  sensorValue = digitalRead(sensorPin);
  // If the switch changed, due to noise or pressing:
  if (sensorValue == lastTiltState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    lastTiltState = sensorValue;
  }
  digitalWrite(ledPin, lastTiltState);

  Serial.println(sensorValue);
  delay(500);
}

View raw code

Demonstration

In the end, this is what you’ll have.

tilt-sensor-gif

Wrapping up

I hope you’ve found this post useful.

If you would like to know more about Arduino sensors, make sure you check some of the following guides:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

3 thoughts on “Guide for the Tilt Sensor (inclinometer) with Arduino”

    • Ok, you asked this question a while ago.

      However, I‘ve build an measuring sensor based on an ESP8266 and an 3-Axis accelerometer MPU-6050. Works like a charm…

      Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.