Keyes KY-017 Arduino Mercury Tilt Switch: Tutorial

KY-017 Functional Description

KY-017 Is TiltedThe KEYES KY017 is a mercury tilt switch that allows you to detect the tilt of your object so that you can take the appropriate action.  It is a nice low cost alternative to a 6 axis accelerometer.

When strategically adjusted or situated, it can be very sensitive.

The key to using this switch successfully is in understanding the two operational states.

 

Where to Get a KY-017

You can find one from any of the vendors below

eBay

Amazon

DX Extreme

Keyes KY-017 Mercury Tilt Sensor Pinouts

The tilt switch only requires three connections to your Arduino.

KY-017 Tilt Switch PinoutsUnderstanding the Output States

Deciding what tilt means with respect to this device can be a little confusing at first.  The images are intended to clear up that confusion.

Not Tilted Position

Not Tilted in the context of this article means that the switch contacts have been shorted together by the mercury.  In this state, the output will be low and the LED will be on.

KY-017 Not TiltedTilted Position

When this article refers to the tilted position,  it means that the mercury is not connecting the switch contacts.  In this state, the LED is off, and the output is HIGH.

KY-017 Is TiltedKeyes KY-017 Schematic

In your project, you’re going to want to establish your own definition of what it means to be tilted.  This should be based on a little experimentation and an understanding of the circuit.  The schematic below is intended to aid in that understanding.

KY-017 Mercuy Tilt Switch Schematic
KEYES KY-017 Arduino Tilt Switch Tutorial

Connect the Mercury Tilt Switch to your Arduino

There are only three connections you need to make.

 KY-017 Arduino Tutorial Hook UpCopy, Paste and Upload the KY017 Arduino Sketch

The sketch below will detect a change in states of the tilt switch and send an output to your serial monitor.

// Henry's Bench
//KY-017 Mercury Switch Tutorial;

int tiltSwitch = 3; // Tilt Switch Input
int tiltVal; // variable to store tilt input
boolean bIsTilted ;// define numeric variables val

void setup ()
{
  Serial.begin(9600);
  pinMode (tiltSwitch, INPUT) ;// define the mercury tilt switch sensor output interface
}
void loop ()
{
  tiltVal = digitalRead (tiltSwitch) ;// read the switch value
  if (tiltVal == HIGH) // Means we've tilted
  {
    if (!bIsTilted){
      bIsTilted = true;
      Serial.println("** - TILTED - **");
    } 
  }
  else
  {
    if (bIsTilted){
      bIsTilted = false;
      Serial.println("not tilted");
    }    
  }
}

Verify Your Output

All that is left to do is verify the operation.   Open your serial monitor and alternate between point the sensor up and the sensor down.

Your output should look like the image below.

KY-017 Arduino Tutorial Output

Add a Comment

Your email address will not be published. Required fields are marked *