Thursday, 11 June 2015

HOW TO USE TILT SENSOR WITH ARDUINO UNO

HOW TO USE TILT SENSOR WITH ARDUINO UNO

In this example, let us see how a tilt sensor can be interfaced with an Arduino Uno. Through this article you can imagine some applications where you can use these type of sensors. Through this tilt sensor, you can sense a tilt by means of any movement of normal and abnormal in nature.

What is a tilt Sensor?
Tilt Sensor allow you to detect orientation or inclination. They are small, inexpensive, low-power and easy-to-use. If used properly, they will not wear out. Their simplicity makes them popular for toys, gadgets and appliances. Sometimes they are refered to as " mercury switches", "tilt switches" or "rolling ball sensors" for obvious reasons. 

In construction, they are usually made by a cavity of some sort(Cylindrical is popular, although not always) and a conductive free mass inside, such as a blob of mercury or rolling ball. One end of the cavity has two conductive elements (Poles). When the sensor is oriented so that the end is downwards, the mass rolls onto the poles and shorts them, acting as a switch throw.

The diagram shows such sensor.


The following wiring diagram shows the Tilt sensor to be connected to the Arduino Uno.


The following diagram also shows the 3D view of connections of Arduino Uno with Tilt sensor at Pin no.2 and Led at Pin no.3

 
  
                    The following shows the Arduino sketch. Load the sketch into the Arduino IDE and run it, now shake the tilt sensor, the led connected to the Pin no.3 will become on and off depending upon your tilt.

int SensorPin = 2;
int LEDPin = 3;
int LEDstate = HIGH;
int reading;
int previous = LOW;
long time = 0;
long debounce = 50;


void setup()
{
pinMode(SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
pinMode(LEDPin, OUTPUT);
}

void loop()
{
int switchstate;
reading = digitalRead(SensorPin);
if (reading != previous)
{
time = millis();

if ((millis() - time) > debounce)
{
switchstate = reading;
if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(LEDPin, LEDstate);
previous = reading;
}

After loading the above sketch, incline the tilt sensor and the LED connected on pin 3 will turn on. If you have any problem please send feedback.


No comments:

Post a Comment