KY-027 Magic light cup
The LED is turned on or off when there is a vibration.
- Arduino
- Raspberry Pi
- Raspberry Pi Pico
- Micro:Bit
The LED is switched on or off in the event of vibration. The trigger signal, which controls the LED, is additionally forwarded to the signal output.
Presistor:
Depending on the input voltage, series resistors are required.
Series resistor (3.3 V) [Red] | 120 Ω |
Series resistor (5 V) [Red] | 220 Ω |
Pin assignment
Code example Arduino
Pin assignment Arduino
Arduino | Sensor |
---|---|
Pin 10 | signal |
5 V | +V |
GND | GND |
Pin 13 | LED |
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
The LED is switched on or off in the event of vibration. The trigger signal, which controls the LED, is additionally forwarded to the signal output.
Presistor:
Depending on the input voltage, series resistors are required.
Series resistor (3.3 V) [Red] | 120 Ω |
Series resistor (5 V) [Red] | 220 Ω |
Pin assignment
Code example Raspberry Pi
Pin assignment Raspberry Pi
Raspberry Pi | Sensor |
---|---|
GPIO 23 [Pin 16] | Signal |
5 V | +V |
GND | GND |
GPIO 24 [Pin 18] | LED |
Programming example in the Python programming language.
# 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
To start with the command:
sudo python3 KY027-RPi.py
The LED is switched on or off in the event of vibration. The trigger signal, which controls the LED, is additionally forwarded to the signal output.
Presistor:
Depending on the input voltage, series resistors are required.
Series resistor (3.3 V) [Red] | 120 Ω |
Series resistor (5 V) [Red] | 220 Ω |
Pin assignment
Code example Micro:Bit
Pinout Micro:Bit:
Micro:Bit | Sensor |
---|---|
Pin 2 | LED |
Pin 1 | Signal |
3 V | +V |
GND | GND |
Sample program download
The LED is switched on or off in the event of vibration. The trigger signal, which controls the LED, is additionally forwarded to the signal output.
Presistor:
Depending on the input voltage, series resistors are required.
Series resistor (3.3 V) [Red] | 120 Ω |
Series resistor (5 V) [Red] | 220 Ω |
Pin assignment
Code example Raspberry Pi Pico
Pin assignment Raspberry Pi Pico
Raspberry Pi Pico | Sensor |
---|---|
GPIO26 | Signal |
3.3V | +V |
GND | GND |
GPIO18 | LED |
# Load libraries
from machine import Pin, Timer
# Initialization of GPIO26 as input | Initialization of GPIO18 as output
sensor = Pin(26, Pin.IN, Pin.PULL_DOWN)
LED = Pin(18, Pin.OUT, Pin.PULL_DOWN)
# Timer initialization
timer = Timer()
# Variables initialization
var = 0
# Function: LED is switched ON/OFF
def step(timer):
global var
print("vibration")
if var == 0:
LED.value(1)
var = 1
else:
LED.value(0)
var = 0
# Function: After triggering the interrupt the timer is triggered to execute the step() function.
def shake(pin):
# Debounce function: Set timer
timer.init(mode=Timer.ONE_SHOT, period=100, callback=step)
# Initialization interrupt
sensor.irq(trigger=Pin.IRQ_FALLING, handler=shake)