ESP32 KY-020 Tilt Switch Module
Overview
The KY-020 is a tilt switch module that detects changes in orientation. It provides a digital signal output, making it suitable for applications like motion detection, security systems, and tilt-based controls.
About KY-020 Tilt Switch Module
The KY-020 Tilt Switch Module is designed to detect orientation or inclination. It contains a metallic ball that connects or disconnects the circuit depending on the module’s tilt angle. When tilted, the ball moves, closing or opening the circuit, which can be read as a digital signal by microcontrollers like the Arduino or ESP32. This module operates at voltages between 3.3V and 5V, making it versatile for various projects.
Where to Buy
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
Technical Specifications
Pinout Configuration
The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32.
Pin (-):
Connects to ground (GND).Pin (middle):
Connects to VCC (3.3V to 5V).Pin (S):
Outputs the digital signal; connect to a digital input on your microcontroller.
Troubleshooting Guide
Common Issues
❌ No Response from Sensor
Issue: The sensor does not output any signal when tilted.
Solutions:
- Verify that all connections are secure and correctly placed.
- Ensure the module is receiving the appropriate voltage (3.3V to 5V).
- Check if the microcontroller's digital input pin is correctly configured.
⚠️ False Triggering
Issue: The sensor outputs a signal without being tilted.
Solutions:
- Ensure stable mounting to prevent unintended movements.
- Check for loose connections or interference from nearby electronic components.
- Implement software debouncing to filter out spurious signals.
Debugging Tips
🔍 Serial Monitor
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
⚡ Voltage Checks
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
Code Examples
Arduino Example
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 Arduino code sets up the KY-020 tilt switch on digital pin 10. It reads the digital value corresponding to the tilt state and prints a message to the serial monitor when an inclination is detected.
ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define TILT_SWITCH_PIN GPIO_NUM_4
void app_main(void) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << TILT_SWITCH_PIN),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
printf("KY-020 Tilt Switch Test\n");
while (1) {
int level = gpio_get_level(TILT_SWITCH_PIN);
if (level == 0) {
printf("Inclination recognized\n");
vTaskDelay(pdMS_TO_TICKS(1000)); // 1s delay
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
This ESP-IDF code configures GPIO4 as an input with an internal pull-up resistor for the KY-020 tilt switch. It continuously checks the pin state and prints a message when an inclination is detected.
ESPHome Example
binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
name: "KY-020 Tilt Switch"
filters:
- delayed_on: 50ms
- delayed_off: 50ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Inclination recognized!");
This ESPHome configuration sets up the KY-020 tilt switch as a binary sensor on GPIO4 with an internal pull-up resistor enabled. Debouncing filters are applied to avoid false triggers. When the sensor detects an inclination, a log message is recorded.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#define TILT_SWITCH_PIN 4
void setup() {
pinMode(TILT_SWITCH_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-020 Tilt Switch Test");
}
void loop() {
if (digitalRead(TILT_SWITCH_PIN) == LOW) {
Serial.println("Inclination recognized");
delay(1000);
}
delay(100);
}
This PlatformIO code configures GPIO4 as an input with a pull-up resistor for the KY-020 tilt switch. It detects inclination changes and prints a message when a tilt is recognized.
MicroPython Example
import machine
import time
TILT_SWITCH_PIN = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if TILT_SWITCH_PIN.value() == 0:
print("Inclination recognized")
time.sleep(1)
time.sleep(0.1)
This MicroPython script configures GPIO4 as an input with an internal pull-up resistor for the KY-020 tilt switch. It continuously checks the sensor state and prints a message when an inclination is detected.
Conclusion
The ESP32 KY-020 Tilt Switch Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.