Introduction: Arduino Tilt Sensor Tutorial | Interfacing Tilt Ball Switch Sensor With Arduino

A Tilt Sensor switch is an electronic device that detects the orientation of an object and gives its output High or Low accordingly. Basically, it has a ball inside it which moves and makes the circuit. So tilt sensor can turn on or off the circuit based on its orientation.
In this project, we are interfacing Tilt sensor with Arduino UNO & We are controlling a LED according to the output of the tilt sensor. Whenever we tilt the sensor LED will be turned on.

Step 1: Things You Need

For this project you'll need following things :-
Arduino uno :

1x Tilt sensor :
1 x LED
1x Resistor ( from 220 ohm to 1k ohm : any value )
1x relay module :
1x Breadboard
Few jumpers

Step 2: Circuit

Connect everything According to as shown in the image and then we will be ready to upload th code.

Step 3: Code

Now we come to coding part open your arduino ide and copy the following code & upload it to arduino uno :
Code :-

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);

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);


Step 4: Final Step

We completed everything so when you change the orientation of your tilt sensor LED will be ON & when taking it back to its previous orientation will turn it off as you can see in images & video. And even instead of LED you can attach a relay module & your AC bulb will turn on whenever you will change the orientation of your tilt sensor.

Video link :