Introduction:
This experiment involves detecting the presence or absence of a cup using a KY-027 Magic Light Cup module with a Raspberry Pi Pico. The module consists of a tilt switch and LED, allowing it to sense orientation changes. When the cup is tilted, the LED can be controlled to indicate the presence or absence of the cup. This experiment aims to understand the working of tilt switches and demonstrate LED control based on the switch's position.
Scope:
This experiment applies to simple detection systems in which the presence or absence of an object needs to be sensed. It is relevant for projects requiring basic tilt or orientation-based detection, such as those in smart containers, toys, and interactive displays.
Prerequisite:
3.1. Hardware:
Pico microcontroller
KY-027 Magic Light Cup Module: A module with a tilt switch and LED for detecting the tilt state.
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
Tilt Switch Output: Connect the tilt switch pin to GPIO 16 on the Raspberry Pi Pico.
LED Control: Connect the LED control pin to GPIO 18 for PWM control.
KY-027 Pin | Raspberry Pi Pico Pin |
Tilt Output | GPIO 26 |
LED Input | GPIO 18 |
VCC | 3.3V |
GND | GND |
UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).

Working Principle:
The KY-027 module uses a mercury tilt switch to detect the cup’s orientation. When tilted, the tilt switch connects, allowing current to flow and signaling the presence of a cup. This signal triggers the LED to indicate either half or full brightness using PWM on the Pico. If the cup is upright, the LED is dimmed to half brightness; if tilted, it glows fully, indicating the cup's presence.

The Magic Light Cup module switches the LED on or off when a shock is detected. If a vibration is detected, the LED is activated or deactivated and the corresponding control signal is forwarded to a signal output. This signal can be read out to monitor the status of the LED. The module is ideal for applications where the reaction to movement or vibration is used, such as interactive lights, toys or alarm systems. By forwarding the trigger signal to the signal output, other systems or microcontrollers can detect the vibrations and react accordingly.
Application code:
MicroPython:
from machine import Pin, PWM
import time
# Define GPIO pins
TILT_SWITCH = 26 # Tilt switch
LED = 18 # LED (PWM)
# Set up tilt switch and PWM for LED
tilt_switch = Pin(TILT_SWITCH, Pin.IN, Pin.PULL_UP)
pwm_led = PWM(Pin(LED))
pwm_led.freq(1000) # 1 kHz frequency
def set_brightness(pwm, brightness):
pwm.duty_u16(brightness)
while True:
if tilt_switch.value() == 0: # Tilted
set_brightness(pwm_led, 65535) # LED full brightness
else: # Upright
set_brightness(pwm_led, 32768) # LED half brightness
time.sleep(0.1) # Stability delay
Embedded C:
#include "pico/stdlib.h"
#include "hardware/pwm.h"
// Define GPIO pins
#define TILT_SWITCH 26
#define LED_PIN 18
int main() {
// Initialize GPIOs
gpio_init(TILT_SWITCH);
gpio_set_dir(TILT_SWITCH, GPIO_IN);
gpio_pull_up(TILT_SWITCH);
gpio_set_function(LED_PIN, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(LED_PIN);
pwm_set_freq_duty(slice_num, 1000, 32768); // 1kHz frequency, half brightness initially
while (1) {
if (gpio_get(TILT_SWITCH) == 0) {
pwm_set_gpio_level(LED_PIN, 65535); // Full brightness if tilted
} else {
pwm_set_gpio_level(LED_PIN, 32768); // Half brightness if upright
}
sleep_ms(100);
}
}
Hardware Image:

Output:
Tilted State: The LED glows at full brightness, indicating the cup's presence.

Upright State: The LED dims to zero brightness, indicating the cup's absence.

Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)
project(tilt_led)
pico_sdk_init()
add_executable(tilt_led
main.c
)
target_link_libraries(tilt_led pico_stdlib hardware_pwm)
pico_add_extra_outputs(tilt_led)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)