KY-027 MAGIC LIGHT CUP MODULE

Click Here to View Step by Step

KY-027 MAGIC LIGHT CUP MODULE


Short description :

The KY-027 Magic Light Cup module is a set of two boards. Each one has a LED and a mercury tilt switch.

Working with  (Compatible)

Arduino, ESP32, Nodemcu, ESP8266, Raspberry Pi, and ......

KY-027 MAGIC LIGHT CUP MODULE

KY-027 MAGIC LIGHT CUP MODULE

ARDUINO IDE CODE 

Example description :

The mercury switches in each module provide a digital signal that is used to regulate the brightness of the LEDs using PWM. Place the modules so the mercury switches on each other and are facing in the opposite directions. Tilting the modules will decrease the brightness on one module while increasing it on the other one, creating the illusion of light magically passing from one module to the other.

// 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
}