Back to Tutorial

Button with ESP32


🧠 What is a Push Button?

A push button is a simple mechanical switch that connects two points in a circuit when pressed.

  • Normally Open (NO):Β Most common type β€” the circuit is open (OFF) when the button is not pressed.
  • When pressed:Β The circuit closes, allowing current to flow.

🧾 Components Needed

  • ESP32 board
  • Push button
  • 10kΞ© resistor (for pull-down, optional if using internal pull-up)
  • Jumper wires
  • Breadboard

πŸ”Œ Wiring Push Button to ESP32

Option 1: Using Internal Pull-Up (Recommended)

Button PinESP32
One legGND
Other legGPIO 14 (or any digital pin)

No resistor needed β€” we’ll use the internal pull-up.


🧠 Working Logic (with pull-up):

  • ButtonΒ not pressed: Pin readsΒ HIGHΒ (connected internally to 3.3V)
  • ButtonΒ pressed: Pin readsΒ LOWΒ (connected to GND)

βœ… Basic Code (Button with ESP32)

#define BUTTON_PIN 14 // Connect push button to GPIO14
void setup()
{
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
}
void loop()
{
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW)
{
Serial.println("πŸ”˜ Button Pressed");
}
else
{
Serial.println("β­• Button Released");
}
delay(200); // debounce delay }

🟒 Optional: Control an LED with Button

#define BUTTON_PIN 14
#define LED_PIN 2 // Onboard LED (optional external)
void setup()
{
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
} void loop()
{
if (digitalRead(BUTTON_PIN) == LOW)
{
digitalWrite(LED_PIN, HIGH); // Turn on LED
}
else
{
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}
USING Button.h Library

βœ… Button library by J-M-L or others

These libraries are often named just Button.h, and are designed to simplify:

  • Debouncing
  • Single press
  • Long press
  • Multi-click detection

βœ… Step-by-Step: Using Button.h Library with ESP32

πŸ“Œ Note: Since Button.h is a generic name, I’ll assume you’re referring to a standard Arduino button library that provides button.isPressed() or similar methods. If you meant a different specific library, let me know!


πŸ“š Step 1: Install the Library

  1. InΒ Arduino IDE
  2. Go toΒ Sketch > Include Library > Manage Libraries
  3. Search forΒ Button
  4. You may see something like:
    • ButtonΒ byΒ J-M-L
    • EasyButton
    • OneButtonΒ (also common and good)

Pick one based on features. Let’s continue with the simple “Button.h” by J-M-L version.


🧰 Hardware Setup

  • Button one side β†’Β GND
  • Button other side β†’Β GPIO 14Β (or any digital input)
HOW TO OPERATE

/////////////////////////////////////////////////////////////////

#include "Button2.h"

/////////////////////////////////////////////////////////////////

#define BUTTON_PIN  12
#define LED 2
/////////////////////////////////////////////////////////////////

Button2 button;

/////////////////////////////////////////////////////////////////

void pressed(Button2& btn) {
    Serial.println("pressed");
    digitalWrite(LED, HIGH);
}
void released(Button2& btn) {
    Serial.print("released: ");
    digitalWrite(LED, LOW);
    Serial.println(btn.wasPressedFor());
}

void setup() {
    pinMode(LED, OUTPUT);
  Serial.begin(115200);
  delay(50);
  Serial.println("\n\nButton Demo");

  button.begin(BUTTON_PIN);
 
  Serial.println();

  button.setReleasedHandler(released);
  button.setPressedHandler(pressed);


}

/////////////////////////////////////////////////////////////////

void loop() {
  button.loop();
}

/////////////////////////////////////////////////////////////////

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial