Project

โ— Beginner ยท KY-004 Push Button Module

Reading Button Presses with KY-004 on ESP32

Overview

Learn how to use the KY-004 push button module with an ESP32. This project is useful for manual controls, reset buttons, menu navigation, and simple input projects.

Instructions

Step by step

1. Connect the KY-004 VCC pin to 3.3V. 2. Connect the KY-004 GND pin to GND. 3. Connect the KY-004 S pin to D15. 4. Run the code and open the Serial Monitor. 5. Click or toggle the KY-004 button in the simulator. 6. When the button is pressed, the output should print "Button pressed". 7. When the button is released, the output should print "Button released".

Code

Source code

code
const int buttonPin = 15;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);

  Serial.println("KY-004 Push Button Test");
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    Serial.println("Button pressed");
  } else {
    Serial.println("Button released");
  }

  delay(500);
}

Related

More projects with this sensor