KY-020 Tilt Switch Sensor module
Short description :
The KY-020 Tilt Switch Sensor module is a switch that reacts to movement. It closes the circuit when it’s tilted to the side as long as it is moved with enough force and degree of inclination to activate the ball switch inside.
Working with (Compatible)
Arduino, ESP32, Nodemcu, ESP8266, Raspberry Pi, and ......
ARDUINO IDE CODE
Example description :
when the module inclination degree changes. Tilt the module to turn the LED on/off.
int tiltPin = 2; // pin number for tilt switch signal
int ledPin = 13; // pin number of LED
int tiltState = 0; // variable for reading the tilt switch status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // set the LED pin as output
pinMode(tiltPin, INPUT); // set the tilt switch pin as input
}
void loop(){
// get the tilt switch state
tiltState = digitalRead(tiltPin);
Serial.println(tiltState);
// check if tilt switch is tilted.
if (tiltState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}