The Keyes Mercury Switch Module KY-017 is a simple and effective motion detecting module. When your autonomous vehicle or robot is tilted the mercury ball inside the tube will move to complete an electrical circuit which will in turn send a signal to your Arduino control system.  Commonly used in bike anti theft system where tilt is detected and alarm or SMS is sent to the owner.


  • Material: FR4
  • Length: 30mm
  • Width: 15mm
  • Height: 10mm
  • Weight: 1.50g
  • Operating Voltage: 5v DC

Arduino Connection With Tilt Switch

Figure 1: Arduino Connections with Tilt Switch

Arduino GND –> Pin – of module
Arduino 5+ –> Pin middle of module
Arduino 3 –> pin S of module

Figure 2: Mercury Tilt Switch

Arduino Code for Tilt Switch

//KY017 Mercury open optical module
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the mercury tilt switch sensor interface
int val ;// define numeric variables val
void setup ()
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (buttonpin, INPUT) ;// define the mercury tilt switch sensor output interface
void loop ()
  val = digitalRead (buttonpin) ;// read the values assigned to the digital interface 3 val
  if (val == HIGH) // When the mercury tilt switch sensor detects a signal, LED flashes
    digitalWrite (Led, HIGH);
    digitalWrite (Led, LOW);

The following sketch will light up the led on pin 13 of the Arduino when the module is tilted.