ESP32 KY-017 Mercury Tilt Switch Module Pinout, Wiring and more
Overview
The KY-017 is a mercury tilt switch module that detects changes in orientation. When tilted beyond a certain angle, the internal mercury switch closes, sending a digital signal. It's suitable for projects requiring simple tilt or motion detection.
About KY-017 Mercury Tilt Switch Module
The KY-017 Mercury Tilt Switch Module is a sensor that detects tilt or inclination. It contains a mercury switch that closes the circuit when tilted to a certain angle, allowing current to flow. This module operates at a voltage range of 3.3V to 5.5V and provides a digital output signal. It’s commonly used in applications such as tilt detection, motion sensing, and orientation monitoring.
Where to Buy KY-017 Mercury Tilt Switch Module
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
KY-017 Datasheet and Technical Specifications
KY-017 Pinout Diagram
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.
KY-017 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 GPIO pin is correctly configured as an input.
⚠️ False Triggering
Issue: The sensor outputs a signal without being tilted.
Solutions:
- Ensure the module is mounted securely to prevent unintended movement.
- Check for external vibrations or movements that might affect the sensor.
- 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
KY-017 Code Examples
Arduino Example
int tiltPin = 10; // Tilt sensor connected to digital pin 10
int ledPin = 13; // LED connected to digital pin 13
int sensorValue = 0;
void setup() {
pinMode(tiltPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("KY-017 Tilt Sensor Test");
}
void loop() {
sensorValue = digitalRead(tiltPin);
if (sensorValue == LOW) {
digitalWrite(ledPin, HIGH);
Serial.println("Tilt detected");
} else {
digitalWrite(ledPin, LOW);
}
delay(1000);
}
This Arduino code sets up the KY-017 tilt sensor on digital pin 10 and an LED on digital pin 13. When the sensor detects a tilt (sensor output is LOW), the LED lights up, and a message is printed to the serial monitor. The status is checked every second.
ESP-IDF Example
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define TILT_SENSOR_PIN GPIO_NUM_4
#define LED_PIN GPIO_NUM_2
void app_main(void) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << TILT_SENSOR_PIN) | (1ULL << LED_PIN),
.mode = GPIO_MODE_INPUT_OUTPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
printf("KY-017 Tilt Sensor Test\n");
while (1) {
int sensor_value = gpio_get_level(TILT_SENSOR_PIN);
if (sensor_value == 0) {
gpio_set_level(LED_PIN, 1);
printf("Tilt detected\n");
} else {
gpio_set_level(LED_PIN, 0);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This ESP-IDF code configures GPIO4 as an input for the KY-017 tilt sensor and GPIO2 as an output for an LED. When the sensor detects a tilt (sensor output is LOW), the LED turns on, and a message is printed to the console. The status is checked every second.
ESPHome Example
binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
name: "KY-017 Tilt Sensor"
filters:
- delayed_on: 50ms
- delayed_off: 50ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Tilt detected!");
This ESPHome configuration sets up the KY-017 mercury tilt switch as a binary sensor on GPIO4. The internal pull-up resistor is enabled, and debounce filtering is applied to prevent false triggering. When the sensor detects a tilt, a log message is generated.
PlatformIO Example
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
PlatformIO Example Code
#include <Arduino.h>
#define TILT_SENSOR_PIN 4
#define LED_PIN 2
void setup() {
pinMode(TILT_SENSOR_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-017 Tilt Sensor Test");
}
void loop() {
if (digitalRead(TILT_SENSOR_PIN) == LOW) {
Serial.println("Tilt detected");
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(1000);
}
This PlatformIO code configures GPIO4 as an input with an internal pull-up resistor for the KY-017 tilt sensor. GPIO2 is used to control an LED. When the sensor detects a tilt (output is LOW), the LED turns on, and a message is printed to the serial monitor.
MicroPython Example
import machine
import time
TILT_SENSOR_PIN = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
LED_PIN = machine.Pin(2, machine.Pin.OUT)
while True:
if TILT_SENSOR_PIN.value() == 0:
print("Tilt detected")
LED_PIN.on()
else:
LED_PIN.off()
time.sleep(1)
This MicroPython script configures GPIO4 as an input with a pull-up resistor for the KY-017 tilt sensor. GPIO2 controls an LED. When a tilt is detected (sensor output is LOW), the LED turns on, and a message is printed to the console.
Conclusion
The ESP32 KY-017 Mercury 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.