KY-027 magic light cup Tilt Switch Module with Arduino

How to use magic light cup KY-027 with Arduino and how the magic light cup module work are some questions that may arise in your mind while programming an Arduino.

Hello guys! Welcome back to my blog today. We are going to see about the magic light cup (KY-027) sensor.

What is Magic Light Cup Tilt Switch??

The magic light cup module is a combination of an LED module and a Tilt switch module. The Mercury ball inside the Tilt switch causes the LED to TURN OFF and TURN ON. The tilt switch consists of a mercury ball. it is sometimes called a mercury opening module

magic light cup module

A tilt switch is a switch that uses mercury as a conductive material to make electrical contact between two leads. A tilt switch consists of a tube inside which a mercury ball is used to make a contact between the lead terminals.

How does Magic Light Cup Tilt Switch Works??

When the tilt switch is placed at the vertical position, the mercury ball falls to the bottom of the tube, as is a good conductor of electricity it generates electrical contact between the lead terminal thus acting as a Closed circuit.

magic cup light

When you change the position of the switch in the horizontal direction the mercury ball which was at the bottom rolls towards the side direction and hence there is no contact between the lead terminal, therefore, it acts as an Open-circuit.

light cup module

The circuitry of the module is such that when the mercury ball is at the bottom of the glass the voltage at the signal pin will be LOW and the led module will be turned OFF. When we roll the mercury ball the voltage at the signal pin will be HIGH and the led module will be turned ON.

Parts Required

  • KY-027 (magic light cup Tilt Switch Module Arduino)
  • Jumper wires
  • LED
  • Breadboard

Schematic Diagram

light cup Arduino

Connect the ground pin of the tilt switch to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 8. Also, connect the positive lead of LED to Arduino pin 10.


int ledmodule=8;                   // Set led pin at Arduino pin 8
int tiltmodule=10;               // Set sensor pin at Arduino pin 10
int sensorvalue;                 // initialize variable to store sensor data
void setup()
 pinMode(ledmodule,OUTPUT);       // LED pin set to output
 pinMode(tiltmodule,INPUT);   // tilt sensor pin set to input
 Serial.begin(115200);        // set baud rate for Serial communication
void loop() 
 sensorvalue = digitalRead(tiltmodule); // read values from sensor pin and store in variable
  if (sensorvalue==HIGH)         // checking conditions
  digitalWrite(ledmodule,HIGH);   // turn LED on
  Serial.println("Rolling"); // Print message 
  digitalWrite(ledmodule,LOW);   // turn LED off
  Serial.println("Stable");       // Print message
 delay(20);                   // delay 

First, initialize pins of sensors & LED and variables. We have declared a variable name sensor value to read values from sensors. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT. Also, we configure the baud rate to 115200.

In a void loop, we continuously read values from the tilt switch and store values in variable name sensor value and we check here condition if it is HIGH or not. Normally by default sensor sends a low signal when the sensor is at a stationary position means the mercury ball is at the bottom and connects two leads to make a close circuit. And HIGH means mercury ball is moved away from two leads making the connection as open circuit, the sensor sends a HIGH signal to our Arduino which we printed as “Rolling”. Here we have written code to turn ON led when there is a movement of the mercury ball.

When the mercury ball is at the bottom of the tube connecting two leads then the sensor sends a LOW signal to Arduino which is printed as “Stable“ and the LED module connected to pin 10 will be turned OFF. We have introduced a delay of 20 milliseconds. The sensor output may differ depending upon the pull-up and pull-down onboard resistor. 

light cup module


magic light cup Arduino output

You can see output on the Serial monitor. When the tilt switch is at the stable position, the mercury ball is at the base then the output will be “Stable” and when we make a move to the sensor such that the mercury ball goes away from the base of the tube, then sensor output will be “Rolling”.

 Conclusion -

Today we learn about what is a magic light cup, how to use a magic light cup with Arduino & working principle of the magic light cup.

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu