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 |
Ground |
GND |
Arduino |
Sensor |
Pin 13 |
LED + |
Ground |
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
KY027-Arduino.zip
Code example Raspberry Pi
Pin assignment Raspberry Pi
Raspberry Pi |
Sensor |
GPIO 23 [Pin 16] |
Signal |
5 V |
+V |
Ground |
GND |
Raspberry Pi |
Sensor |
GPIO 24 [Pin 18] |
LED + |
Ground |
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
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 |
3 V |
+V |
Ground |
GND |
Sample program download
microbit-KY-027.zip