Arduino and a mercury tilt switch


A mercury tilt switch is a switch which opens and closes an electrical circuit when it is tilted at certain angles. When it is tilted a small amount of the liquid metal mercury makes contact with metal electrodes to close the circuit.


Mercury switches have one or more sets of electrical contacts in a sealed glass envelope which contains a bead of mercury. The envelope may also contain air, an inert gas, or a vacuum. Gravity is constantly pulling the drop of mercury to the lowest point in the envelope. When the switch is tilted in the appropriate direction, the mercury touches a set of contacts, thus completing the electrical circuit through those contacts.

Tilting the switch the opposite direction causes the mercury to move away from that set of contacts, thus breaking that circuit. The switch may contain multiple sets of contacts, closing different sets at different angles, allowing, for example, single-pole, double-throw (SPDT) operation.

Here is an example of a module that is available which can be used in various projects



Now lets connect this up to an Arduino


You connect this as follows

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

You can of course use a pin of your own choosing for the output


A simple example, tilt the switch

const int tiltPin = 7;     // the number of the tilt switch pin
const int ledPin =  13;      // the number of the LED pin
// variables will change:
int tiltState = 0;         // variable for reading the tilt switch status
void setup()
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the tilt switch pin as an input:
  pinMode(tiltPin, INPUT);
void loop()
  // read the state of the tilt switch value:
  tiltState = digitalRead(tiltPin);
  // check if the tilt switch is activated.
  // if it is, the tiltState is HIGH:
  if (tiltState == HIGH)
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // turn LED off:
    digitalWrite(ledPin, LOW);


10pcs / bag 3MM tilt switch mercury switch original authentic way

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.