• nithya7rns

Interfacing Tilt Sensor with Arduino Uno

Tilt sensors are one of the main components used in the construction of buildings and structures. Tilt sensors measure the angle of tilt or inclination with respect to the vertical plane. In a structure that is being constructed, the tilt sensors are used to check if the plane does not have any imbalances.


So let us know more about the sensor and its basics by building a project with Arduino to observe the working with small inputs. We shall begin with the Hardware requirements.


Hardware Requirements


All the components required
All the components required
  1. Arduino UNO

Develop a stronger concept in Arduino through this article:- What is Arduino?


Arduino UNO
Arduino UNO

Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be that from light-detecting sensors, flame sensors, temperature sensors, etc. The output function done by the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.

Buy Arduino UNO from here.


2. Piezo Buzzer

Piezo Buzzer
Piezo Buzzer

It is an electrical component that generates a beep sound on receiving an input. It works on the principle of #piezo crystal.


Buy Piezo buzzer from here.


3. LED

LED
LED

Light Emitting Diode is a commonly used light source. It is a semiconductor that emits light when current flows through it.


Buy LED from here.


4. Tilt Sensor


Tilt Sensor
Tilt Sensor

A tilt sensor is a device used to measure the #inclination and give digital output based on the angle of tilt. The sensor gives digital HIGH value when there is a tilt in the surface it is attached to. The original value of the sensor will be 0 (LOW).


Buy Tilt Sensor from here.


5. Jumper Wires

Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper Wires from here.


6. Breadboard


Breadboard
Breadboard

The breadboard is the basic component of any circuit building process. All components, be it input sensors or output display devices are connected to the power supply, microcontroller using wired connections through a breadboard. The holes in the breadboard are in series. There are various sizes like full-sized, half-sized, and mini breadboard.


Buy breadboard from here.


7. Resistor


220 Ohm Resistor
220 Ohm Resistor

Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The input power passes through these resistors and then to the sensors to avoid damage.

Buy Resistors from here.


Circuit Connections


Circuit Diagram
Circuit Diagram

The circuit connections are as follows.


Firstly, the Ground pin and 5V power supply pin of the Arduino are connected to two separate lines of the breadboard.


The #Tilt sensor has three terminals. The Ground terminal of the sensor is connected to the ground line of the breadboard. The second terminal is connected to the Digital PIN 4 of the Arduino. The VCC terminal of the sensor is connected to the power supply line on the breadboard.


The #buzzer has two terminals. The negative terminal of the buzzer is connected to the ground line of the breadboard and the positive terminal of the buzzer is connected to the Digital PIN 3 of the Arduino board.


The LED has two terminals. The negative terminal o the #LED is connected to the ground line on the #breadboard. The positive terminal of the LED is connected to the Digital PIN 2 of the Arduino by connecting the 220 Ohm resistor in series with the LED.


The #Arduino is connected to the Computer system interfaced through the USB cable and the code is uploaded.


Code


void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);
}

The setup( ) function here is going to declare the Modes of each of the PINs. PIN 2 is connected to the LED which is going to show us the result of the working of the sensor. Hence it is declared as OUTPUT using the pinMode( ) function.


The PIN 3 is connected to the Piezo buzzer which also shows the result of the Tilt sensor when there is an inclination. It is also declared as OUTPUT using pinMode( ) function. PIN 4 of the Arduino is connected to the input terminal of the Tilt sensor. The HIGH or LOW (1 or 0) value is obtained from the sensor. Hence it is declared as INPUT.

void loop() 
{
  if (digitalRead(4) == 1)
  {
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);
  delay(300);                      
  }

The loop( ) function contains an "if" condition which checks for the value of the input from the sensor. If the value is HIGH the PIN connected to LED goes HIGH and the PIN connected to Buzzer goes HIGH. A delay of 0.3 seconds is given after every input is received and the loop iterates again.

else
  {
    digitalWrite(2, LOW);   
    digitalWrite(3, LOW);
    delay(300); 
  }
}

If the input from the sensor is 0, there is no change in the LED and buzzer status. Hence using the digitalWrite( ) function, both the PINs are assigned the value LOW. A delay of 0.3 seconds is given using delay( ) function.


COMPLETE CODE


void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, INPUT);
}
void loop() 
{
  if (digitalRead(4) == 1)
  {
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);
  delay(300);                      
  }
  else
  {
    digitalWrite(2, LOW);   
    digitalWrite(3, LOW);
    delay(300); 
  }
}

For the complete code for this program, download the following document.


Tilt Sensor Code
.txt
TXT • 328B

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.


Working


Circuit when Power supply is given
Circuit when Power supply is given

The Tilt sensor is the Input device for this circuit. The sensor output is given as input to the circuit in terms of 0 and 1. Usually, the value '0' is assigned to the sensor. When there is an inclination in the surface to which the sensor is attached, the sensor value goes HIGH.


The change in this value is given to the LED and Buzzer which glows and gives a tone respectively when there is a change in the output from the #sensor. The status of the LED and the buzzer come back to normal, i.e. becomes LOW when there is no inclination in the surface and when the Tilt sensor output doesn't give the value 1.


Watch this video practical explanation of the circuit and code.


Video By - Kaushik Ram Selvaraj

A tilt sensor can also be used as a Tilt #switch in a few circuits that can trigger other devices and give output. For example, if we consider a Servo motor connected to a fan that is further connected to the circuit to maintain a temperature of a device and when there is a tilt in the surface where the particular device is kept, the tilt sensor acts as a switch and might trigger the motor and fan to switch ON.


This is just an example. There are various circuits in which the Tilt sensor can be used. Start exploring by #designing your own circuit.


SEE ALSO: