The LED is turned on or off when there is a vibration. The signal when the LED is on is given to a signal output which can then be read out.

Presistor:

Depending on the input voltage, series resistors are required.

The designation (Rf) refers to the required series resistor in front of the LED based on the applied voltage.

Rf (3.3V) [Red] 120Ω

[e.g. when used with ARM CPU core based microcontrollers like Raspberry-Pi]

Rf (5V) [Red] 220Ω

[e.g. when used with Atmel Atmega based microcontrollers like Arduino]

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
pin 10 signal
5V +V
ground GND
Arduino Sensor
Pin 13 LED +
ground LED -

This is an example program, which turns on the LED of the Magic-LightCup module (or an external LED like here), if a tilt is detected at the sensor.

int Led = 13 ;// Declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable
  
void setup ()
{
  pinMode (Led, OUTPUT) ; // Initialize output pin
  pinMode (Sensor, INPUT) ; // Initialize sensor pin
  digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor
}
  
void loop ()
{
  val = digitalRead (Sensor) ; // The current signal at the sensor is read out
  
  if (val == HIGH) // If a signal could be detected, the LED is switched on.
  {
    digitalWrite (Led, LOW);
  }
  else
  {
    digitalWrite (Led, HIGH);
  }
}

Example program download

KY027-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 23 [Pin 16] Signal
5V +V
ground GND
Raspberry Pi Sensor
GPIO 24 [Pin 18] LED +
ground LED -

Programming example in the Python programming language.

In this program example the LED lights up when a signal is detected at the tilt switch.

# Required modules are imported and set up
import RPi.GPIO as GPIO
import time
   
GPIO.setmode(GPIO.BCM)
   
# Here the two pins are declared, to which the sensor and the LED are connected,
LED_PIN = 24
Sensor_PIN = 23
GPIO.setup(Sensor_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
   
print ("Sensor test [press CTRL+C to end test]")
   
# This outputFunction will be executed when a signal is detected
def outputFunction(null):
        GPIO.output(LED_PIN, True)
   
# When a signal is detected the output function is executed
GPIO.add_event_detect(Sensor_PIN, GPIO.FALLING, callback=outputFunction, bouncetime=10) 
   
# main program loop
try:
    while True:
        time.sleep(1)
        #output is reset if the tilt switch is tilted to the other side again
        if GPIO.input(Sensor_PIN):
            GPIO.output(LED_PIN,False)
   
# Clean up after the program is finished
except KeyboardInterrupt:
    GPIO.cleanup()

Download sample program

KY027-RPi.zip

To start with the command:

sudo python3 KY027-RPi.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 2 LED
Pin 1 Signal
3V +V
Ground GND

This is a MakeCode example for Micro:Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the Raspberry Pi example than the Arduino example.

Sample program download

microbit-KY-027.zip