How to interface Ky-017 mercury tilt switch with Arduino

How to use mercury tilt switch KY 017 with Arduino and how to tilt switch work are some questions that may arise in your mind while programming an Arduino.

Hello guys! Welcome back to my blog today. We are going to see about the tilt switch Arduino

What is Tilt Switch??

tilt sensor Arduino

A tilt switch is a switch that uses mercury as a conductive material to make electrical contact between two leads. A tilt switch consists of a cylindrical tube inside which a liquid mercury ball is used to make electrical contact between two unconnected lead terminals. As long as mercury remains away from the leads, leads remain disconnected and no current will flow. Once the tube is moved over a certain angle such that the mercury ball comes in contact with the two leads then the current flows and an electrical connection is made. 

inclination sensor

Mercury tilt switches are electrical switches that allow the flow of current between two leads based on their alignment & physical movement. Mercury tilt is used to detect inclinations or alignment of an angle. These are low cost, low power consumption & easy to use mercury switches. Mercury balls are less sensitive to slight vibration due to their thick & dense nature Also mercury ball does not get fragment that’s make the switch less reliable.

How do tilt switches work??

When a tilt switch is placed at the vertical position, gravity helps the mercury ball falls to the bottom of the tube, as is a good conductor of electricity it generates electrical contact between the lead terminal thus acting as a Closed circuit.

tilt switch sensor

When you change the position of the switch in the horizontal direction the mercury ball which was at the bottom rolls towards the side direction and hence there is no contact between the lead terminal, therefore, it acts as an Open-circuit.

To understand we will take a circuit Example

When the tilt switch is at 90 degrees, the mercury ball will be at the bottom so the connection will be closed, and the led will glow, when the position of the tilt switch is changed, mercury changes its original location, ball rolls another side and contact breaks thus LED will turn off. The advantage of using a tilt switch is very low cost but mercury is very toxic in nature so use it carefully to avoid breaking glass tubes and spilling the mercury. If the tube is broken then throw it away. Mercury can affect humans, animals as well as plants when they are exposed to it. Mercury is considered to be a very hazardous substance. Mercury has to be handled safely but accidental ingestion or skin contact can cause serious injuries. Improper disposal of mercury led to widespread contamination of the environment.

tilt angle sensor

Application of tilt switch 

  •  You can tilt the switch in your robotic car when your car rolls upside-down then the tilt switch will stop a motor or it will use in the self-balancing robot.
  • Cranes with hydraulic leveling.
  • You can install it on your bicycle or motorcycle or bike when they meet an accident or slip on the road the position of mercury changes and turn on the SMS system to notify your family, friends, or nearby hospital to inform about your accident to get immediate help.
  • Use drones for giving information about horizontal and vertical inclination.
  • Robotics
  • In video games the joystick for capturing physical movements.
  • Level detection for dry substances such as grains, and sand granules. When material in a tank reaches a certain level it will change the actual position of the tilt sensor and the filling machine will stop.   

Parts Required

  • KY-017 (tilt switch module Arduino)
  • Jumper wires
  • LED
  • Breadboard 

Schematic Diagram

tilt switch Arduino

Connect the ground pin of the tilt switch to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 8. Also, connect the positive lead of LED to Arduino pin 11.

Code -

int ledpin=11;                   // Set led pin at Arduino pin 11
int tiltsensor=8;               // Set sensor pin at Arduino pin 8
int sensorvalue;                 // initialize variable to store sensor data
void setup()
{
 pinMode(ledpin,OUTPUT);       // LED pin set to output
 pinMode(tiltsensor,INPUT);   // tiltsensor pin set to input
 Serial.begin(115200);        // set baudrate for Serial communication
}
void loop() 
{
 sensorvalue = digitalRead(tiltsensor); // read values from sensor pin and store in variable
 //Serial.println(sensorvalue);
  if (sensorvalue==HIGH)         // checking conditions
 {
  digitalWrite(ledpin,HIGH);   // turn LED on
  Serial.println("Rolling"); // Print message 
 }
 else{
  digitalWrite(ledpin,LOW);   // turn LED off
  Serial.println("Stable");       // Print message
 }
 delay(20);                   // delay 
}


First, initialize pins of sensors & LED and variables. We have declared a variable name sensor value to read values from sensors. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT. Also, we configure the baud rate to 115200.

In the void loop, we continuously read values from the tilt switch and store values in variable name sensor value and we check here condition if it is HIGH or not. Normally by default sensor sends a low signal when the sensor is at a stationary position means the mercury ball is at the bottom and connects two leads to make a close circuit. And HIGH means mercury ball is moved away from two leads making the connection as open circuit, the sensor sends a HIGH signal to our Arduino which we printed as “Rolling”. Here we have written code to turn ON led when there is a movement of the mercury ball.

When the mercury ball is at the bottom of the tube connecting two leads then the sensor sends a LOW signal to Arduino which is printed as “Stable“ and the LED connected to pin 11 will be turned OFF. We have introduced a delay of 20 milliseconds. The sensor output may differ depending upon the pull-up and pull-down onboard resistor.  

OUTPUT -

angle sensor Arduino

Now we used the serial monitor to observe the output. . To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M)You can see output on the Serial monitor. When the tilt switch is at the stable position, the mercury ball is at the base then the output will be “Stable” and when we move the sensor such that the mercury ball goes away from the base of the tube, then the sensor output will be “Rolling”.

 Conclusion - 

Today we learn about what is Arduino tilt switch, how to use a tilt switch with Arduino, the Application of the tilt switch & the working principle of the reed switch, etc.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu