Danny van den Brande
Published © CC BY-SA

Arduino - Mercury Switch TILT ALARM With the KY-027 Set

Hello world! Today I made a example on what you could to with the Mercury Switch. Also known as the Magic Light cup in the Arduino world.

BeginnerProtip1 hour1,085
Arduino - Mercury Switch TILT ALARM With the KY-027 Set

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
KY-027 Mercury switch Module SET
×1
KY-019 2 x 1 channel relay
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

Schematic

Code

KY-027_Light_Cup_Module_example.ino

Arduino
This is a simple example on how to use the KY-027
/*
 * Author: Danny van den brande, Arduinosensors.nl. 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 ()
{
tiltLeft();
tiltRight();
delay(25);
} 

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

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

Credits

Danny van den Brande

Danny van den Brande

36 projects • 108 followers
"Hello world."

Comments