KY-004 Key Switch Module
The KY-004 is a key switch module equipped with a tactile push-button. It provides a digital output signal when pressed, making it ideal for user input applications in various electronic projects.

On this page
KY-004 pinout
The KY-004 is a 3-pin tactile push-button switch module:
| Pin | Type | Description | Notes |
|---|---|---|---|
| Pin (-) | Power | Ground connection | |
| Pin (+) | Power | Power supply | 3.3V or 5V |
| Pin (S) | Communication | Digital signal output | Goes LOW when button pressed (active low) |
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
Wiring the KY-004 to ESP32
To interface the KY-004 with an ESP32 for button input:
| KY-004 pin | ESP32 pin | Purpose |
|---|---|---|
| Pin (-) | GND | Ground |
| Pin (+) | 3.3V or 5V | Power supply |
| Pin (S) | 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 code examples
KY-004 Arduino example
Copy#define BUTTON_PIN 4 // KY-004 signal pin (GPIO4, matches the wiring above)
#define LED_PIN 2 // Onboard LED on most ESP32 dev boards
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button pulls the line to GND when pressed
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
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);
}The sensor's signal pin connects to GPIO4 (matching the wiring above) and is read as a digital input with the internal pull-up enabled. On the ESP32, pull-ups are enabled with INPUT_PULLUP - the old AVR trick of writing HIGH to an input pin does nothing here. Pressing the button pulls the line LOW, mirrored on the onboard LED (GPIO2).
KY-004 ESP-IDF example
Copy#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.
KY-004 ESPHome example
Copybinary_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.
KY-004 PlatformIO example
Copy[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino#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.
KY-004 MicroPython example
Copyimport 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.
KY-004 specifications
About the KY-004
The KY-004 is nothing more than a small tactile push-button pre-wired to a 3-pin header. Press it and the signal pin is pulled to ground (active-low); release it and, because the module includes a pull-up resistor, the pin floats back HIGH on its own with no extra wiring needed on the ESP32 side.
No debounce circuitry is included, so like any mechanical switch expect a few milliseconds of contact bounce on both press and release - fine for a simple “is it pressed” poll, but worth filtering in software (a short delay or a debounce library) if a project is counting presses rather than just reading state. Joy-IT rates the switch itself for roughly 100,000 press cycles at 3.3-5V logic; it’s the same tactile button used across dozens of other kit modules under a different silkscreen.
KY-004 troubleshooting
No Response from KY-004 Module
›
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.
False Triggering of Button Press
›
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.
Where to buy the KY-004

Resources
Similar sensors





