Skip to Content

Use Tilt Switch Sensor With Arduino

Use Tilt Switch Sensor With Arduino

A tilt switch is a device you can use to detect a tilt. 

In this tutorial, we will learn about how a tilt switch works and I will share some Arduino tilt switch project examples, including a step-by-step connection guide and code that you can use. 

Tilt switches are similar to push button switches.

Tilt switches are used in toys, security devices, robotics, alarm systems, and motion detection applications.

They are inexpensive, small, and easy to use. 

By the end of this article, you will have all the knowledge you need to work with tilt switches in your own project.

Let’s get started!

Components Needed To Build Arduino Tilt Switch Sensor Project

Hardware Components

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

How A Tilt Switch Sensor Works

In this section, we will understand the intrinsic properties of the tilt switch.

We will see how they work, different types of tilt switches, their advantages and more. 

In the below image, you can see a tilt sensor mounted on a PCB.

By using the sensor as a switch, you can generate either a high voltage (logic 1) or a low voltage (logic 0).

How A Tilt Switch Sensor Works

Mercury Tilt Switches

A basic tilt switch in the past was built using Mercury as a contact material.

Mercury Tilt Switches

Mercury is a liquid metal at room temperature.

When you tilt the mercury switch sideways, it will either move away from the contacts (Switch condition: Open) or provide an electrical connection path between two contacts (switch condition: Close). 

Nowadays, mercury is not used. Mercury is a heavy metal and hazardous to the environment. 

There are other techniques used in tilt switches instead of mercury based solutions.

Metal Ball Tilt Switch

One commonly found tilt switch is the one with metal balls inside a capsule.

The balls will either make or break the switch based on the orientation.

Metal Ball Tilt Switch
Source: https://www.instructables.com/TILT-SWITCH-USING-ARDUINO-UNO-R3/

The tilt switches can last for more than 100,000 operations.

The Arduino code stays the same for the mercury switch and the metal ball-based tilt switch. 

mercury switch and the metal ball-based tilt switch

I have taken an extract from a metal ball-based tilt switch datasheet.

You should consider a few important parameters before choosing the tilt switch.

  • Contact Arrangement: SPST
  • Sensor Angle: 0˚ to -10˚ scope inside, terminal to  disconnection electric current
  • Minimum Voltage: 1 VDC 10mA (resistive load) 
  • Maximum Voltage:  5 VDC 1mA (resistive load) 
  • Operating Life: 100,000 cycles 
  • Insulation Resistance: 1000 M Ω min. @ 100 VDC 
  • Dielectric Strength: 500 V AC (50-60 Hz) 
  • Operating Temperature: –20ºC to +70ºC

There are tilt switches which avoid the mechanical switch. Mechanical switches will have wear and tear.

Over a lifetime, the reliability and the number of operations specified in the datasheet will be lower. 

Photo Interrupter Tilt Switch 

A photo interrupter is another kind of tilt switch. The photo-type interrupter provides the same feature as a metal ball tilt switch.

Photo Interrupter Tilt Switch

The light will be interrupted when you tilt the sensor causing the detection.

You can either have a microcontroller to process the signal or an additional circuit. 

Applications Of The Tilt Switch

You can find the tilt switch in many applications. Tilt switches are inexpensive, straightforward to use, don’t consume power and are easy to handle.

 Let us see a few examples of applications where you can use tilt switches:

  1. Toys – The low price and ease of use make the tilt switch a favourable part of toys. You can use the tilt switch to close the circuit when a toy is shaken or thrown. Since the circuit gets activated only when someone shakes the toy, the circuit consumes no power the rest of the time. 
  1. Inclination detection – You can use tilt switches to detect inclination (maximum allowed inclination) in all four directions. To achieve four-directional incline detection, you can place four tilt switches. 
  1. Pet entry and exit indication  – You can play a sound whenever the pet leaves or enters the room via the small gate in the door. You can detect the movement using a tilt switch every time the pet moves the gate.
  1. Security Applications – You can also use the tilt switches to trigger an alarm if someone opens the gate. You can creatively program Arduino to either raise the alarm (during night time) or a beep (day time) to alert the owner.
  2. Fall detection – You can use the tilt switch to detect if a person has fallen or equipment has fallen (during shipment or handling).

Step-By-Step Instructions To Connect The Tilt Switch With Arduino UNO

In this section, we will connect the tilt switch to the Arduino UNO. The connections are simple to follow.

If you have any questions, please feel free to post them in the comments section.

I will get back to you at the earliest. Let us start.

Once you complete the connections, refer to the code section below.

The example code helps you to get started with your project faster.

How To Connect The Tilt Switch To The Arduino UNO?

Below is the step-by-step connection guide to complete the Arduino and the tilt switch sensor IC module. 

Step 1: Start with the GND connections

Start with the GND connections

You can choose any GND pins on the Arduino to complete the GND connections.

It is a good practice to connect the GND pins first. 

Whenever I make connections, I will start with the ground because keeping ground common from the beginning phase of circuit building avoids unintentional current flow in other paths. 

Step 2: Connect the S pin

Connect the S pin

The S pin on the tilt sensor modules is the data signal. The S pin will either be a one or a zero depending on the position of the tilt switch.

Some tilt switch modules will provide 5 V when the switch is open, while the others will provide 0 V.

The product datasheet or the technical specification sheet is the key. 

Refer to the datasheet to understand more about the relation between the tilt switch and the ‘S’ pin.

Not all tilt switch modules behave in the same way. I am choosing the A0 pin of the Arduino UNO.

You can choose any of the free GPIO pins available. 

Tip: Do not forget to update your code about the pin you plan to use for signal detection.

Step 3: Connect the Power Supply

Connect the Power Supply

The Arduino UNO runs at 5 V. Hence you connect 5 V to the power pin.

If you are using a 3.3 V device (for example, Arduino Mini), then you should connect 3.3 V to the power pin of the tilt sensor module. 

Step 4: The complete Connection

The complete Connection

Congratulations on completing all the necessary connections to test your tilt switch module.

The next section will examine the Arduino code examples for the tilt switches.  

Arduino Code Example For The Tilt Switch Project

In this section, you will find the sketch required to build a basic tilt switch sensing project.

Project 1: Tilt switch to toggle an LED

The tilt switch and the push button switch are very similar. Both provide similar output for the Arduino UNO.

You can use the code generated for a push button with the Arduino.

int ledPin = 13;
int inPin = 7;
int value = 0;
 
void setup()
{
  // initializes digital pin 13 as output
  pinMode(ledPin, OUTPUT);
     
  // initializes digital pin seven as input         
  pinMode(inPin, INPUT_PULLUP);         
}
 
void loop()
{
  value = digitalRead(inPin);   // reads the value at a digital input
  digitalWrite(ledPin, value);          
}

Brief code walkthrough 

The tilt switch is connected to Arduino pin 7.

int inPin = 7;

The LED is connected to Arduino pin 13. Arduino UNO has an onboard LED, which is connected to Pin 13.

Hence, you don’t have to use another additional LED for this project. 

int ledPin = 13;

The below line defines the LED pin as a general-purpose output pin.

   pinMode(ledPin, OUTPUT);

In the loop function, you can read the inPin status using the digitalRead() function.

The function will either return a one or a zero based on the tilt switch condition.

  value = digitalRead(inPin);   // reads the value at a digital input

Project 2: Tilt switch project to detect movement of the breadboard

In this project, you will check whether anyone has tilted the circuit.

If the circuit is not tiled, the green LED will be On. If the circuit is tilted, the red LED will be turned ON. 

When you turn the board, the switch turns off. The Arduino detects the open switch condition and turns on the red LED.

The red LED is connected to Pin 7 of the Arduino.

The green LED is connected to Pin 6 of the Arduino.

int tiltPin = 2;
int tiltVal;
int redPin = 7;
int greenPin = 6;
 
void setup() {
  // put your setup code here, to run once:
  pinMode(tiltPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  digitalWrite(tiltPin, HIGH);
  Serial.begin(9600);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  tiltVal = digitalRead(tiltPin);
  Serial.println(tiltVal);
  if (tiltVal == 0) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
  }
  if (tiltVal == 1) {
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);

  }
}

FAQs About The Tilt Sensor And Arduino Projects

I have included a list of the most frequently asked questions about the Arduino and the tilt projects.

The sensor interface and the basic principle are easy to understand.

Yet, there can be questions still not answered in the article. 

Please post the questions you have in the comments.

I will try to answer them at the earliest.

1) What is the tilt sensor in the Arduino?

The tilt sensor is similar to a push button switch. When you tilt the sensor, the switch will get closed.

You can use the tilt sensor to detect the inclination, vibration, movement, etc.

2) How do I program a tilt switch?

The tilt sensor is a passive sensor with two pins. When you tilt the sensor, the two pins get shorted.

You program the Arduino as if you are interfacing it with a push button.

You can convert the tilt sensor to an active high or an active low switch. 

You can refer to the Arduino code section in the article to learn to program the tilt sensor. 

3) What type of sensor is a tilt switch?

The tilt sensor is a type of inclinometer sensor. The sensor works with gravity and a simple mechanism to detect tilt or motion.

The tilt switch we have discussed in this article.

The tilt switch makes an inferior sensor, but you can combine multiple tilt switches with better accuracy.

4) How do you test a tilt switch?

Tilt switches are easy to test. To test a tilt switch:

  1. Connect one pin of the tilt switch to the positive terminal of the multimeter.
  2. Connect the other pin to the negative terminal of a multimeter.
  3. Keep the multimeter in continuity mode.

The switch will be open if you tilt the sensor into one position. The tilt switch should get closed if you tilt on the other side. 

The multimeter should buzz, indicating the switch’s closure. 

5) What is a tilt switch used for?

Tilt switches are used for a variety of applications.

Tilt switches can be useful in angle detection, valve rotation angle, gate handle opening and closing detection, maximum tilt detection in aerial vehicles, and more. 

You also see the tilt switches in the toys. When you throw them or kick them, they will glow.

I have used tilt sensors to detect vibration and tilt. I wired them together with a LED and a battery.

So, whenever someone toppled it, it would blink the LEDs.

Conclusion

This article covered the basic working principle, types and applications of tilt switches.

We also understood how to mitigate the bouncing issues associated with the tilt switch. 

You should now be able to connect the tilt switch to your Arduino board easily, and make use of the example code I have provided.

I hope you enjoyed this article on the tilt switch. The concept is simple and widely used. 

Please share your feedback and suggestions to improve the article and be user-friendly.

Please do not forget to share the article with your fellow Arduino enthusiasts.