Home Code Tilt switch example

Tilt switch example

by shedboy71

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

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.

The module I used is part of a sensor and module kit that is commonly available on the internet, here is a picture of the module

mercury tilt switch

mercury tilt switch

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

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

 

Ultimate 37in1 Sensor Module Kit Set Basic for Raspberry Pi for Arduino

10PCS 3MM Mercury Switch Tilt Sensor Angle Sensor Unidirectional switch

Share

You may also like