KY-020 Tilt switch
Depending on the tilt, a switch short-circuits the input pins.
- Arduino
- Raspberry Pi
- Raspberry Pi Pico
- Micro:Bit
This module contains a tilt switch that closes a circuit depending on its orientation. Inside the switch is a small ball that connects the contacts when the tilt is changed. When the module is tilted, the ball moves and short-circuits the input pins, generating a signal. This simple but effective mechanism enables the detection of tilt changes and is ideal for applications such as motion detection, security and alarm systems or as a switching mechanism in various projects. The tilt switch is easy to integrate and provides a reliable way to detect motion and tilt.
Technical Data | |
---|---|
Operating voltage | 3,3 V - 5 V |
Dimensions | 19 x 15 x 5 mm |
Pin assignment
Arduino | Sensor |
---|---|
Pin 10 | signal |
5 V | +V |
GND | GND |
Code example
To load the following code example onto your Arduino, we recommend using the Arduino IDE. In the IDE, you can select the appropriate port and board for your device.
Copy the code below into your IDE. To upload the code to your Arduino, simply click on the upload button.
int tilt_switch = 10; // Declaration of the sensor input pin
int value; // Temporary variable
void setup () {
pinMode(tilt_switch, INPUT); // Initialization sensor pin
Serial.begin(9600); // Initialization of the serial monitor
Serial.println("KY-020 Inclination test");
}
void loop () {
// The current signal at the sensor is read out
value = digitalRead(tilt_switch);
// If a signal could be detected, this is displayed on the serial monitor.
if (value == LOW) {
Serial.println("Inclination recognized");
delay(1000); // 1s break
}
}
This module contains a tilt switch that closes a circuit depending on its orientation. Inside the switch is a small ball that connects the contacts when the tilt is changed. When the module is tilted, the ball moves and short-circuits the input pins, generating a signal. This simple but effective mechanism enables the detection of tilt changes and is ideal for applications such as motion detection, security and alarm systems or as a switching mechanism in various projects. The tilt switch is easy to integrate and provides a reliable way to detect motion and tilt.
Technical Data | |
---|---|
Operating voltage | 3,3 V - 5 V |
Dimensions | 19 x 15 x 5 mm |
Pin assignment
Raspberry Pi | Sensor |
---|---|
GPIO 24 [Pin 18] | Signal |
3.3 V [Pin 1] | +V |
GND [Pin 6] | GND |
Code example
This is an example program where a console output is generated as soon as a signal is detected.
from gpiozero import Button
import time
# The input pin to which the sensor is connected is declared here.
# The button object uses the internal pull-up resistor of the Pi.
sensor = Button(24, pull_up=True)
print("Sensor test [press CTRL+C to end the test]")
# This output function is executed when a signal is detected
def ausgabeFunktion():
print("Signal recognized")
# When a signal is detected (falling signal edge), the output function is triggered
sensor.when_pressed = ausgabeFunktion
# Main program loop
try:
while True:
time.sleep(1)
# Clean-up work after the program has been completed
except KeyboardInterrupt:
print("Program terminated")
This module contains a tilt switch that closes a circuit depending on its orientation. Inside the switch is a small ball that connects the contacts when the tilt is changed. When the module is tilted, the ball moves and short-circuits the input pins, generating a signal. This simple but effective mechanism enables the detection of tilt changes and is ideal for applications such as motion detection, security and alarm systems or as a switching mechanism in various projects. The tilt switch is easy to integrate and provides a reliable way to detect motion and tilt.
Technical Data | |
---|---|
Operating voltage | 3,3 V - 5 V |
Dimensions | 19 x 15 x 5 mm |
Pin assignment
Micro:Bit | Sensor |
---|---|
Pin 1 | Signal |
3 V | +V |
GND | GND |
Code example
pins.setPull(DigitalPin.P1, PinPullMode.PullUp)
basic.forever(function () {
serial.writeLine("" + (pins.digitalReadPin(DigitalPin.P1)))
if (pins.digitalReadPin(DigitalPin.P1) == 0) {
serial.writeLine("Tilt angle reached")
} else {
serial.writeLine("Tilt angle not reached yet")
}
serial.writeLine("_____________________________________")
basic.pause(1000)
})
Sample program download
This module contains a tilt switch that closes a circuit depending on its orientation. Inside the switch is a small ball that connects the contacts when the tilt is changed. When the module is tilted, the ball moves and short-circuits the input pins, generating a signal. This simple but effective mechanism enables the detection of tilt changes and is ideal for applications such as motion detection, security and alarm systems or as a switching mechanism in various projects. The tilt switch is easy to integrate and provides a reliable way to detect motion and tilt.
Technical Data | |
---|---|
Operating voltage | 3,3 V - 5 V |
Dimensions | 19 x 15 x 5 mm |
Pin assignment
Raspberry Pi Pico | Sensor |
---|---|
GPIO26 | Signal |
3.3V | +V |
GND | GND |
Code example
This is a sample program that counts up and outputs text serially when a signal is detected at the sensor.
To load the following code example onto your Pico, we recommend using the Thonny IDE. All you have to do first is go to Run > Configure interpreter ... > Interpreter > Which kind of interpreter should Thonny use for running your code? and select MicroPython (Raspberry Pi Pico).
Now copy the code below into your IDE and click on Run.
# Load libraries
from machine import Pin, Timer
# Initialization of GPIO as input
sensor = Pin(26, Pin.IN, Pin.PULL_DOWN)
# Create timer
timer = Timer()
# Variables initialization
counter = 0
print("KY-020 Inclination test")
# Function for counting steps
def step(timer):
global counter
counter = counter + 1
print("Inclination recognized:", counter)
# Function that is executed on inclination
def shake(pin):
# Set debounce function timer
timer.init(mode=Timer.ONE_SHOT, period=100, callback=step)
# Initialization of the interrupt
sensor.irq(trigger=Pin.IRQ_FALLING, handler=shake)