Introduction
Similar to a tilt switch, a mercury switch is used to detect slight inclinations of a large angle. A mercury switch (also known as a mercury tilt switch) is a switch which opens and closes an electrical circuit through a small amount of liquid mercury.
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.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Mercury switch module
– Several jumper wires
Experimental Principle
Since the LED has been attached to pin 13, just connect pin S to D2 of the SunFounder Uno board. When the mercury switch inducts tilt signals, the LED will be on. Otherwise it will be off.
Note: Mercury is harmful to human body and environment. Thus, please BE CAREFUL when using a mercury switch in case of glass breaking. It should also be properly handled if it’s no longer used.
Experimental Procedures
Step 1: Build the circuit
Mercury Switch Module SunFounder Uno
S——————————————- D2
– —————————————— GND
+ —————————————— 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, tilt the switch and the LED attached to pin 13 on the SunFounder Uno board will light up.
Code
const int buttonPin = 2; // the number of the Mercury Switch pin const int ledPin = 13; // the number of the LED pin// variables will change: int buttonState = 0; // variable for reading the Mercury Switch statusvoid setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the Mercury Switch pin as an input: pinMode(buttonPin, INPUT); }void loop(){ // read the state of the Mercury Switch value: buttonState = digitalRead(buttonPin);if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } } |