Mercury tilt switch interfacing with pic microcontroller



Mercury tilt switch interfacing with pic microcontroller, In this tutorial we will see how to interface tilt switch or mercury tilt switch with pic microcontroller. If you are new to pic microcontrollers programming, I recommend you to read following articles to get understanding of pic microcontroller programming:



After reading above articles, you will get a decent idea about, how to interface any kind of sensor with pic microcontroller or any kind of microcontroller. So  now lets see what is mercury tilt switch ? and how  mercury tilt sensor works?

Introduction to mercury tilt sensor

mercury tilt switch is basically like an electrical switch which open turns on and turns off when  mercury tilt sensor is tilted at a certain angle. Mercury tilt switch has liquid mercury inside it and there are two electrodes as well. When we tilted it in a specific direction, liquid mercury makes the electrical connection between two electrodes and five volt signal appears at the output of  mercury tilt switch. Picture of  mercury tilt switch is shown below.



mercury tilt switch

As shown above it consists of two electrical contacts wrapped inside a glass and there is a also a liquid mercury inside a glass. when sensor is straight and liquid mercury is at the bottom side of the glass there will be no contact between electrical contacts means it will act like a open circuit. But when we tilt the sensor in any direction, liquid mercury goes to upper side of the glass and makes the contact between electrical contacts and it acts like a close circuit. So we need a extra components to interface with this switch to detect this open and close circuit condition with the help of pic microcontroller.

mercury tilt sensor module

As I mentioned earlier, we need some extra components to connect with mercury switch to detect open and close circuit states of the sensor. So we are using a mercury tilt switch module. Picture of module is shown below:

mercury tilt switch module

This module has on board resistor and indicator LED. one terminal of tilt switch is connected with 5 volt power supply and other terminal is connect with 100k ohm resistor and and other terminal of resistor is connect with ground. Complete circuit diagram is shown below

mercury tilt switch circuit

whenever tilt switch is closed, we get 5 volt at the output and otherwise we will get zero volt the output. So we simply need to read this output state with the help of pic microcontroller.  So lets see how we can do it.

Mercury tilt sensor interfacing with pic microcontroller

To interface this sensor with pic microcontroller, we simply need to use only one input port of pic microcontroller.  I have used pic16F877A microcontroller in this tutorial. Watch the video given below for more details.

Code for mercury tilt switch interfacing with pic microcontroller

// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;


void main(void)
{

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Tilt sensor" ); // Write text in first row

TRISB.B7=1; // PIN NUMBER 1 IS DECLARED AS A INPUT
PORTB.B7=0;
TRISE.B0=0; // PIN NUMBER 1 IS DECLARED AS A OUTPUT
PORTE.B0=1;
while(1) { // Endless loop


if( PORTB.B7==1)
{
PORTE.B0=0;
Lcd_Out(2,1,"Tilt DETECTED " );
}
else
{
PORTE.B0=1;
Lcd_Out(2,1,"Tilt not DETECTED" );
}
}
}



Add Comment