Introduction:
In this tutorial, we will learn about the KY-020 module, what is a tilt switch sensor and we will build a simple project using the KY-020 module and an Arduino.
Tilt Switch Module KY-020:
The KY-020 Module will be our main component for this tutorial. This module has a tilt switch mounted on a breakout board with a resistor. Figure 1 shows the module as seen in fritzing.
Pin Out:
The KY-020 module has three pins.
Component Pin | Description |
---|---|
(-) | GND |
middle | +5V |
S | Signal |
What is a Tilt Switch?
A tilt switch is a switch with a small conductive ball inside. The ball opens the contacts when the switch body is tilted.
Project:
Arduino Tilt Indicator:
After learning about the KY-020 module and the tilt switch, it is now time to build a project using the module. Our project will light the built-in LED of the Arduino when the KY-020 module is tilted.
Components:
For this project, we need the following components:
- Arduino Uno board (1 pc.)
- KY-020 Tilt Switch Module (1 pc.)
- Jumper wires
Wiring Diagram:
Figure 2 shows the connection between the Arduino Uno and the KY-020 Tilt Switch Module.
The KY-020 module pins are connected to the Arduino Uno board as follows:
Component Pin | Arduino Uno board Pin |
---|---|
(-) | GND |
middle | +5V |
S | 10 |
Code:
Below is the Arduino sketch for our project. I have added comments to explain important parts of the code. Save the code as KY-020.ino and upload it to your Arduino board.
// Arduino and KY-020 module
void setup() {
pinMode(10, INPUT); // KY-020 is connected to pin 10
pinMode(13, OUTPUT); // Arduino built-in LED
}
void loop() {
if (digitalRead(10) == HIGH) digitalWrite(13, HIGH); // switch-on LED if module is tilted
else digitalWrite(13, LOW); // switch-off LED if module is not tilted
}
Project Test:
Apply power to your Arduino Uno board. The built-in LED should light when the KY-020 module is tilted.