Arduino Uno Mercury Open Optical Module
This project utilizes an Arduino Uno to interface with a Mercury open optical module to detect the module's state (open or closed). The module outputs a signal that is read by the Arduino Uno, which then displays the status on the Serial Monitor.
How It Works
A mercury tilt switch is a sealed glass capsule containing a bead of mercury and two electrodes. Tilt the capsule one way and the mercury flows across both electrodes, closing the circuit; tilt it back and the connection opens. The KY-017 module packages this with a resistor and indicator LED.
Mercury makes a genuinely excellent contact — it is liquid, so there is no wear, no pitting and no oxidation, and the switching action is clean. That is why these switches were used for decades in thermostats and tilt alarms.
They are also hazardous. Mercury is toxic and the capsule is glass; a broken switch is a contamination problem, and these parts are restricted or banned in many jurisdictions under RoHS and similar regulations. Modern designs use a ball tilt switch instead, where a conductive ball rolls between contacts. It behaves almost identically and the KY-020 is a drop-in replacement.
Both types bounce as the mercury or ball settles, so the same debouncing a mechanical switch needs applies here — and the settling time is typically longer.
Components Needed
- Arduino Uno
- Mercury Tilt Switch Module module
- Arduino Uno
- USB cable for programming and power
- Arduino Uno
Wiring to the Arduino Uno
Connect S to D2, the middle pin to the 5 V rail and − to GND. Declaring the pin as INPUT_PULLUP gives a defined idle level whichever way the carrier is wired.
Orientation is the whole point of this sensor, so mount it deliberately. Note the angle at which it switches — typically 15–30° from its reference position — and fix it so that angle corresponds to the tilt you care about.
Handle the glass capsule gently and never force the leads close to the body. If a mercury capsule breaks, treat it as hazardous waste rather than sweeping it up.
| Module pin | Arduino Uno pin | Function |
|---|---|---|
| S (signal) | D2 | Switch state — HIGH or LOW depending on tilt |
| Middle / + | 5V | Supply |
| − (GND) | GND | Common ground |
Build and Upload
Open the Arduino IDE and create a new sketch.
Copy and paste the provided Arduino code into the sketch.
Upload the code to the Arduino Uno.
Open the serial monitor with a baud rate of 9600.
Observe the messages indicating whether the module is open or closed based on the sensor state.
Example Code
Debounced tilt detection, reporting only settled changes. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.
const int TILT_PIN = 2;
const unsigned long SETTLE_MS = 80; // longer than a dry contact — the bead rolls
int stable = HIGH;
int lastRead = HIGH;
unsigned long lastChange = 0;
void setup() {
Serial.begin(9600);
pinMode(TILT_PIN, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(TILT_PIN);
if (reading != lastRead) {
lastChange = millis();
lastRead = reading;
}
if (millis() - lastChange > SETTLE_MS && reading != stable) {
stable = reading;
Serial.println(stable == LOW ? "TILTED" : "upright");
}
}
Applications
A mercury tilt switch module turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Tilt and tip-over alarms on equipment, heaters and machinery
- Orientation detection in handheld and wearable projects
- Anti-theft triggers that fire when an object is lifted or moved
- Simple game controllers responding to board tilt
- Automatic shutoff for appliances that must not run on their side
Working with the Arduino Uno
The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.
With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.
The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.
| Arduino Uno characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A5 (six channels) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The state flickers around the switching angle — that is the bead settling. Lengthen the debounce window.
- It never changes — the switch is mounted at the wrong angle; rotate it and retest through its full travel.
- The output is inverted compared with expectations — carrier wiring varies, so simply swap the comparison.
- Readings are unstable with vibration present — tilt switches cannot distinguish vibration from tilt. Use an accelerometer such as the MPU6050 for anything that moves.
- Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Uno
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:
The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.
Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.
Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.
Notes and Practical Limits
Prefer the ball-type KY-020 over a mercury KY-017 for any new build. The electrical behaviour is the same, the code is identical, and it avoids both the toxicity and the regulatory problem entirely.
A tilt switch reports one axis crossing one threshold. For actual angle measurement in three dimensions, an accelerometer is the right part — it reports continuous orientation rather than a single binary transition.