Home Arduino Arduino and a mercury tilt switch

Arduino and a mercury tilt switch

by iainhendry

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.

tilt-switch

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

mercury-tilt-switch

 

Now lets connect this up to an Arduino

Connection

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

Code

A simple example, tilt the switch

[codesyntax lang=”cpp”]

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);
  } 
  else 
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

[/codesyntax]

 

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

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More