Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Using the Magic Light Cup Module KY-027 with Arduino

Contents

Introduction:

In this tutorial, we will learn about the KY-027 module, what is a mercury switch and we will build a simple project using the KY-027 module and an Arduino.

Magic Light Cup Moduel KY-027:

The KY-027 Module will be our main component for this tutorial. This module has a mercury switch and an LED mounted on a breakout board. Figure 1 shows the module as seen in fritzing.

Figure 1: KY-027 Magic Light Cup Module

Pin Out:

The KY-027 module has four pins.

PinDescription
GGND
(+)+5V
SFrom Mercury Switch
LFrom LED Anode (+)

What is a Mercury Switch?

A mercury switch is a type of switch wherein the contacts are closed by a blob of mercury. If the switch is tilted, the mercury will flow and will open the contacts.

Project:

Arduino Tilt Controlled LED Dimmer:

After learning about the KY-027 module and the mercury switch, it is now time to build a project using the module. Our project controls the brightness of the LED by tilting the module.

Components:

For this project, we need the following components:

  • Arduino Uno board (1 pc.)
  • KY-027 Magic Light Cup Module (1 pc.)
  • Jumper wires

Wiring Diagram:

Figure 2 shows the connection between the Arduino Uno and the KY-027 Magic Light Cup Module.

Figure 2: Connection Diagram

The KY-027 module pins are connected to the Arduino Uno board as follows:

Component PinUNO Board Pin
GGND
(+)+5V
S8
L9

Code:

Below is the Arduino sketch for our project. I have added comments to explain important parts of the code. Save the code as KY-027.ino and upload it to your Arduino board.

				
					// Android and KY-027 module
int brightness = 255;   // variable for LED brightness 0~255
void setup() {
  pinMode(8, INPUT);  // switch is connected to pin 8
  pinMode(9, OUTPUT); // LED is connected to pin 9
}
void loop() {
  if (digitalRead(8) == HIGH) {
    if (brightness < 255) brightness++; 
// increase brightness if mercury switch is On
  }
  else {
    if (brightness > 0) brightness--;
// decrease brightness if mercury switch is Off (module tilted)
  }
  analogWrite(9, brightness); // set LED brightness
}
				
			

Project Test:

Apply power to your Arduino Uno board. Slowly tilt the module and the LED’s brightness will be dimmer. Bring back to upright the module and the LED will slowly become brighter.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top