Arduino Mercury Switch ky-027 TILT ALARM

Hello world! Today i made a example on what you could to with the Arduino Mercury Switch ky-027. Also known as the Magic Light cup in the arduino world (still not sure why they call it that) Anyways it is a mercury switch with Leds attached to it, nothing more. This example shows you how to make a tilt alarm. When you tilt left or right the Lamps will flash.

Ky-027 tilt alarm mercury switch, Arduino Mercury Switch ky-027

Parts list: (Note: The KY-027 sensors come in pairs, you need 2.)
Arduino uno.
KY-027 Mercury switch Module SET(Sold & Known as the Magic Light Cup)
KY-019 2 x 1 channel relay (You can use a 2 channels relay to.)
Jumper wires MM/FF/MF.
Lamp socket + lamp and power cable. (Optional)


 * Arduino Mercury Switch ky-027
 * Author: Danny van den brande, BlueCore Tech.
 * This is a simple example on how to use the KY-027 Magic Light 
 * Cup/Mercury Switch.
 * We are going to use it as some kind of alarm to switch on lamps 
 * when tilting left or right.
int Relay1 = 2;
int Relay2 = 3;
int MagicLed1 = 5;
int MagicLed2 = 6;
int MagicCup1 = 7;
int MagicCup2 = 4;
int MagicCupState1 = 0;
int MagicCupState2 = 0;
int brightnessLed1 = 0;
int brightnessLed2 = 0;

void setup ()
pinMode (Relay1, OUTPUT);
pinMode (Relay2, OUTPUT);
pinMode (MagicLed1, OUTPUT);
pinMode (MagicLed2, OUTPUT);
pinMode (MagicCup1, INPUT);
pinMode (MagicCup2, INPUT);

void loop ()

void tiltLeft(){
  MagicCupState1 = digitalRead (MagicCup1);
if (MagicCupState1 == HIGH && brightnessLed1 != 255)
brightnessLed1= 255;
digitalWrite(Relay1, HIGH);
else{brightnessLed1 = 0;}
analogWrite (MagicLed1, brightnessLed1); 
digitalWrite(Relay2, LOW);

void tiltRight(){
  MagicCupState2 = digitalRead (MagicCup2);
if (MagicCupState2 == HIGH && brightnessLed2 != 255)
brightnessLed2 = 255;
digitalWrite(Relay2, HIGH);
else{brightnessLed2 = 0;}
analogWrite(MagicLed2, brightnessLed2);
digitalWrite(Relay1, LOW);

Danny van den Brande

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *