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

by Utsource in Circuits > Arduino

16940 Views, 3 Favorites, 0 Comments

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

Screenshot_20190903-111633__01.jpg

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.

Things You Need

images(6).jpg
images(5).jpg
f_a1a5ca24f309414c9357347ad5fabf97.jpg

For this project you'll need following things :-
Arduino uno : https://www.utsource.net/itm/p/9221687.html


1x Tilt sensor :
1 x LED
1x Resistor ( from 220 ohm to 1k ohm : any value )
1x relay module :
https://www.mob.utsource.net/ic-datasheet/7708783
1x Breadboard
Few jumpers

Circuit

Tilt-sensitive-LED_bb-1.png
Connect everything According to as shown in the image and then we will be ready to upload th code.

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

Final Step

Screenshot_20190903-111623__01.jpg
Screenshot_20190903-111633__01.jpg
Screenshot_20190903-111608__01.jpg
Screenshot_20190903-111617__01.jpg
Tilt Sensor with Arduino

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 : https://youtu.be/wcgRUfgFVf0