Skip to main content
ESPBoards

ESP32 KY-027 Magic Light Cup Module

The KY-027 is a motion detection module featuring a mercury tilt switch and an LED. It detects tilt or orientation changes and can be used in various interactive applications.

⬇️ Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

🔗 Quick Links

KY-027 Magic Light Cup Module Datasheet ButtonKY-027 Magic Light Cup Module Specs ButtonKY-027 Magic Light Cup Module Specs ButtonKY-027 Magic Light Cup Module Specs Button

🛒 KY-027 Price

KY-027 Magic Light Cup Module
Normally, the KY-027 Magic Light Cup Module costs around 2$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

ℹ️ 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.

⚙️ KY-027 Sensor Technical Specifications

Below you can see the KY-027 Magic Light Cup Module Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.

  • Type: module
  • Protocol: Digital
  • Operating Voltage: 3.3V to 5V
  • Dimensions: 36 x 15 x 14 mm
  • Weight: 2 g

🔌 KY-027 Sensor Pinout

Below you can see the pinout for the KY-027 Magic Light Cup Module. The VCC pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND pin is the ground connection and must be connected to the ground of your ESP32!

  • GND: Connects to ground.
  • +V: Connects to 3.3V or 5V power supply.
  • S: Signal output from the tilt switch; goes low when tilted.
  • L: LED control input; driving this pin high turns on the LED.

🧵 KY-027 Wiring with ESP32

Below you can see the wiring for the KY-027 Magic Light Cup Module with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.

  • GND: Connect to ESP32 GND.
  • +V: Connect to ESP32 3.3V or 5V.
  • S: Connect to a digital input pin on ESP32 (e.g., GPIO17).
  • L: Connect to a PWM-capable digital output pin on ESP32 (e.g., GPIO16).

🛠️ KY-027 Magic Light Cup Module Troubleshooting

This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.

❌ LED Not Lighting Up

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 L pin is correctly connected to a digital output pin on the microcontroller.
  • Check the tilt switch functionality by measuring the voltage on the S pin when the module is tilted.

⚠️ False Triggering

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.

💻 Code Examples

Below you can find code examples of KY-027 Magic Light Cup Module with ESP32 in several frameworks:

If you encounter issues while using the KY-027 Magic Light Cup Module, check the Common Issues Troubleshooting Guide.

Arduino Core Image

ESP32 KY-027 Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the KY-027 Magic Light Cup Module:

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.

Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.

ESP-IDF Image

ESP32 KY-027 ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the KY-027 Magic Light Cup Module, here's how you can set it up and read data from the sensor. Fill in this code in the main ESP-IDF file:

#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.

Update the I2C pins (I2C_MASTER_SDA_IO and I2C_MASTER_SCL_IO) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.

ESPHome Image

ESP32 KY-027 ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the KY-027 Magic Light Cup Module

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_output

This 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.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 KY-027 PlatformIO Code Example

Example in PlatformIO Framework

For PlatformIO, make sure to configure the platformio.ini file with the appropriate environment and libraries, and then proceed with the code.

Configure platformio.ini

First, your platformio.ini should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

ESP32 KY-027 PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the KY-027 Magic Light Cup Module:

#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.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 KY-027 MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the KY-027 Magic Light Cup Module with your ESP32.

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.

Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy.

Conclusion

We went through technical specifications of KY-027 Magic Light Cup Module, its pinout, connection with ESP32 and KY-027 Magic Light Cup Module code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.