KY-027 Magic Light Cup Module

View on Amazon
Overview
About KY-027 Magic Light Cup Module
The KY-027 Magic Light Cup Module consists of a mercury tilt switch and an LED. When the module is tilted, the mercury tilt switch closes, activating the LED. This module is ideal for projects that respond to tilt or orientation changes, such as interactive lights, toys, or simple alarm systems.
Get Your KY-027
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
KY-027 Specifications
Complete technical specification details for KY-027 Magic Light Cup Module
📊 Technical Parameters
KY-027 Pinout
The **KY-027** is a 4-pin magic light cup module with integrated tilt switch and LED:
Visual Pinout Diagram

Pin Types
Quick Tips
**Interface**: Digital tilt switch + LED control,⚖️ **Sensor**: Mercury tilt switch (sealed glass tube with mercury droplet),💡 **LED Output**: Onboard LED controllable via L pin
**Signal**: Active low - S pin goes LOW when tilted,⚡ **Power**: 3.3V to 5V operation
**Mercury Warning**: Contains mercury - handle with care, dispose properly,🎯 **Applications**: Interactive lights, magic cup games, tilt-activated systems, orientation alarms
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 GND | Power | Ground connection | |
2 +V | Power | Power supply | 3.3V or 5V |
3 S | Communication | Signal output from tilt switch | LOW when tilted (active low) |
4 L | Communication | LED control input | Drive HIGH to turn on LED |
Wiring KY-027 to ESP32
To interface the **KY-027** with an **ESP32** for tilt detection:
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| KY-027 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 GND Required | GND | Ground | |
2 +V Required | 3.3V or 5V | Power supply | |
3 S Required | GPIO17 | Digital input for tilt switch (any GPIO) | |
4 L Optional | GPIO16 | LED control output (any GPIO) |
**Tilt Detection**: Read S pin - LOW when tilted, HIGH when upright
**LED Control**: Write HIGH to L pin to illuminate onboard LED
**Magic Cup Effect**: Use two modules to create interactive light transfer effect
**Pull-up**: Internal pull-up recommended on S pin for stable readings
**Mercury Safety**: Do not break or incinerate module - contains toxic mercury
**Power LED**: Separate power indicator LED always on when powered
**Tilt Angle**: Typically activates at 15-30° from vertical
KY-027 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The LED does not turn on when the module is tilted.
Solutions:
- Ensure the module is properly powered with the correct voltage.
- Verify that the
Lpin is correctly connected to a digital output pin on the microcontroller. - Check the tilt switch functionality by measuring the voltage on the
Spin when the module is tilted.
Issue: The LED turns on without tilting the module.
Solutions:
- Ensure there is no external vibration or movement causing the tilt switch to activate.
- Check for loose connections or wiring issues that might cause intermittent signals.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
KY-027 Programming Examples
Ready-to-use code examples for different platforms and frameworks
int led = 13; // LED output pin
int shock_sensor = 10; // Sensor input pin
int value; // Temporary variable
void setup() {
pinMode(led, OUTPUT); // Initialize output pin
pinMode(shock_sensor, INPUT); // Initialize sensor pin
digitalWrite(shock_sensor, HIGH); // Activate internal pull-up resistor
}
void loop() {
value = digitalRead(shock_sensor); // Read current signal from sensor
if (value == HIGH) {
digitalWrite(led, LOW); // Turn on LED
delay(200);
} else {
digitalWrite(led, HIGH); // Turn off LED
}
}This Arduino code reads the state of the tilt switch on the KY-027 module. When a tilt is detected, it turns on the onboard LED connected to pin 13. The internal pull-up resistor is activated for the sensor input pin.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define TILT_SENSOR_PIN GPIO_NUM_17
#define LED_PIN GPIO_NUM_16
void app_main(void) {
gpio_set_direction(TILT_SENSOR_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(TILT_SENSOR_PIN, GPIO_PULLUP_ONLY);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
printf("KY-027 Tilt Detection\n");
while (1) {
int sensor_value = gpio_get_level(TILT_SENSOR_PIN);
if (sensor_value == 0) {
gpio_set_level(LED_PIN, 1); // Turn on LED
} else {
gpio_set_level(LED_PIN, 0); // Turn off LED
}
vTaskDelay(pdMS_TO_TICKS(200));
}
}This ESP-IDF code initializes the tilt sensor input and LED output pins. It continuously monitors the tilt sensor state and controls the LED accordingly. When a tilt is detected (sensor output low), the LED is turned on; otherwise, it is turned off.
binary_sensor:
- platform: gpio
pin:
number: GPIO17
mode: INPUT_PULLUP
name: "KY-027 Tilt Sensor"
filters:
- invert:
on_press:
then:
- light.turn_on: led
on_release:
then:
- light.turn_off: led
light:
- platform: binary
id: led
output: led_output
output:
- platform: gpio
pin: GPIO16
id: led_outputThis ESPHome configuration sets up the KY-027 tilt sensor as a binary sensor on GPIO17. When a tilt is detected, it turns on an LED connected to GPIO16. The LED turns off when the tilt is no longer detected.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduinomain.cpp
#include <Arduino.h>
#define TILT_SENSOR_PIN 17
#define LED_PIN 16
void setup() {
pinMode(TILT_SENSOR_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-027 Magic Light Cup Module Test");
}
void loop() {
int sensor_value = digitalRead(TILT_SENSOR_PIN);
if (sensor_value == LOW) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Tilt detected - LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("No tilt detected - LED OFF");
}
delay(200);
}This PlatformIO code configures GPIO17 as an input with a pull-up resistor for the KY-027 tilt sensor and GPIO16 as an output for the LED. When a tilt is detected, the LED turns on, and a message is printed to the serial monitor.
import machine
import time
TILT_SENSOR_PIN = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_UP)
LED_PIN = machine.Pin(16, machine.Pin.OUT)
while True:
if TILT_SENSOR_PIN.value() == 0:
LED_PIN.on()
print("Tilt detected - LED ON")
else:
LED_PIN.off()
print("No tilt detected - LED OFF")
time.sleep(0.2)This MicroPython script configures GPIO17 as an input with a pull-up resistor for the KY-027 tilt sensor and GPIO16 as an output for the LED. When a tilt is detected, the LED turns on, and a message is printed every 200ms.
Wrapping Up KY-027
The ESP32 KY-027 Magic Light Cup Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the KY-027 into your ESP32 project and bring your ideas to life!








