Tilt Sensor Interfacing with Arduino.

Tilt sensor with Arduino

A tilt sensor is a device that is used to detect the orientation of an object and is used in a number of applications mainly in security and aviation systems.In this tutorial I will sow how this sensor can be interfaced with Arduino.

How the tilt sensor works.

The sensor module we are using is the SW-520D which is a roller-ball type tilt sensor. It contains a metallic ball enclosed in a casing that keeps on changing contacts with two contact pins.

When the tilt sensor is in its upright position, the metallic ball bridges the two contact pins and completes the circuit. When the sensor is tilted beyond the sensitivity range, the contacts moves away, and thus opens the circuit.

The sensor also contains a LM393 comparator for producing a digital output D0 from the sensor. This output depends on the orientation of the SW-520D switch to determine whether it is turned on or off. When the switch turns OFF, DO outputs high level voltage and when the switch turns ON, DO outputs low level voltage.

Interfacing the tilt sensor with Arduino.

It is easy to connect the sensor to Arduino as illustrated in the schematic below. The VCC pin of the sensor is connected to 5V terminal of the Arduino and the GND is connected to ground. The DO pin is connected to any digital pin of the arduino.

Tilt sensor with Arduino with alarm and LED

We also include a buzzer and an led to act as an alarm system that will be activated whenever a tilt is detected.

Code for the tilt sensor activated alarm system.

This code is mainly for reading the state of the sensor’s digital output D0. Whenever the tilt sensor is inclined beyond a particular angle the Output of tilt sensor gets HIGH.

This output is read through Pin 2. Therefore, whenever the Pin 2 is HIGH, the LED turns on and the Buzzer sounds.

#define led 8      // led at pin 3
#define tiltSensor 2  //tilt sensor at pin 5
#define buzzer 6      // buzzer at pin 6

int sound=250;         // set buzzer sound
int val = 0; //variable for reading the tilt switch status

void setup(){
   pinMode (led,OUTPUT);
   pinMode (tiltSensor,INPUT);
void loop(){
   val = digitalRead(tiltSensor); //read state of tilt sensor 
   if (val == HIGH){
      tone(buzzer,sound);            // buzzer sounds
   else {