KY-004 Key Switch Module

View on Amazon
Overview
About KY-004 Key Switch Module
The KY-004 is a simple push-button switch module that provides digital input for microcontrollers. When pressed, it connects the signal pin to ground, sending a LOW signal to trigger an event. It operates within 3.3V to 5V, making it compatible with ESP32, Arduino, and other microcontrollers.
⚡ Key Features
- Push-Button Activation – Sends a LOW signal when pressed.
- Operating Voltage – Works with 3.3V to 5V, ensuring broad compatibility.
- Simple Digital Output – Easy integration with ESP32, Arduino, and similar boards.
- Versatile Applications – Used for reset functions, menu navigation, and input controls.
With its compact design and easy-to-use interface, the KY-004 is perfect for user input, interactive devices, and control applications. 🚀
Get Your KY-004
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
KY-004 Specifications
Complete technical specification details for KY-004 Key Switch Module
📊 Technical Parameters
KY-004 Pinout
The **KY-004** is a 3-pin tactile push-button switch module:
Visual Pinout Diagram

Pin Types
Quick Tips
**Interface**: Digital output (active low when pressed),🔘 **Button**: Tactile push-button switch
**Operation**: Signal pin pulled LOW when pressed,⚡ **Power**: 3.3V or 5V operation
**Pull-up**: Built-in pull-up resistor on module,🎯 **Applications**: User input, reset buttons, menu navigation, control interfaces
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 Pin (-) | Power | Ground connection | |
2 Pin (+) | Power | Power supply | 3.3V or 5V |
3 Pin (S) | Communication | Digital signal output | Goes LOW when button pressed (active low) |
Wiring KY-004 to ESP32
To interface the **KY-004** with an **ESP32** for button input:
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| KY-004 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 Pin (-) Required | GND | Ground | |
2 Pin (+) Required | 3.3V or 5V | Power supply | |
3 Pin (S) Required | GPIO4 | Digital input (any GPIO) |
**GPIO Selection**: Any digital GPIO pin works, GPIO4 is just an example
**Voltage**: Use 3.3V for ESP32 compatibility
**Debouncing**: Software debouncing recommended for clean button press detection
**Active Low**: Signal is LOW when pressed, HIGH when released
KY-004 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: Pressing the button does not produce any response.
Solutions:
- Ensure all connections are secure and correctly placed.
- Verify that the module is receiving the appropriate voltage (3.3V or 5V).
- Check the microcontroller's GPIO pin configuration in the code.
- Test the module with a multimeter to confirm functionality.
Issue: The module sends signals without the button being pressed.
Solutions:
- Ensure that the pull-up resistor is properly connected and functioning.
- Implement software debouncing to filter out spurious signals.
- Check for any short circuits or loose connections on the module.
Debugging Tips
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.
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-004 Programming Examples
Ready-to-use code examples for different platforms and frameworks
#define BUTTON_PIN 7
#define LED_PIN 13
void setup() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH); // Enable internal pull-up resistor
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("KY-004 Key Switch Module Test");
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
Serial.println("Button pressed");
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println("Button released");
digitalWrite(LED_PIN, LOW);
}
delay(100);
}This Arduino code sets up the KY-004 Key Switch Module on pin 7 and an LED on pin 13. When the button is pressed, the sensor outputs a LOW signal, turning on the LED and printing a message to the serial monitor.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define BUTTON_PIN GPIO_NUM_4
#define LED_PIN GPIO_NUM_2
void app_main(void) {
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(BUTTON_PIN, GPIO_PULLUP_ONLY);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
printf("KY-004 Key Switch Module Test\n");
while (1) {
int button_state = gpio_get_level(BUTTON_PIN);
if (button_state == 0) {
printf("Button pressed\n");
gpio_set_level(LED_PIN, 1);
} else {
printf("Button released\n");
gpio_set_level(LED_PIN, 0);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}This ESP-IDF code configures GPIO4 as an input for the KY-004 Key Switch Module and GPIO2 as an output for an LED. When the button is pressed (sensor outputs LOW), the LED turns on, and a message is printed to the console.
binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
name: "KY-004 Key Switch"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Button pressed!");This ESPHome configuration sets up the KY-004 Key Switch as a binary sensor on GPIO4 with an internal pull-up resistor. It includes filtering to debounce false triggers and logs when the button is pressed.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduinomain.cpp
#include <Arduino.h>
#define BUTTON_PIN 4
#define LED_PIN 2
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("KY-004 Key Switch Module Test");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.println("Button pressed");
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println("Button released");
digitalWrite(LED_PIN, LOW);
}
delay(100);
}This PlatformIO code configures the KY-004 Key Switch on GPIO4 and an LED on GPIO2. When the button is pressed, the LED turns on, and a message is printed to the serial monitor.
import machine
import time
BUTTON_PIN = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
LED_PIN = machine.Pin(2, machine.Pin.OUT)
while True:
if BUTTON_PIN.value() == 0:
print("Button pressed")
LED_PIN.on()
else:
print("Button released")
LED_PIN.off()
time.sleep(0.1)This MicroPython script configures the KY-004 Key Switch on GPIO4 and an LED on GPIO2. When the button is pressed (LOW signal), the LED turns on, and a message is printed to the console.
Wrapping Up KY-004
The ESP32 KY-004 Key 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.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the KY-004 into your ESP32 project and bring your ideas to life!








