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 Pin | ESP32 |
|---|---|
| One leg | GND |
| Other leg | GPIO 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.his a generic name, Iβll assume you’re referring to a standard Arduino button library that providesbutton.isPressed()or similar methods. If you meant a different specific library, let me know!
π Step 1: Install the Library
- InΒ Arduino IDE
- Go toΒ Sketch > Include Library > Manage Libraries
- Search forΒ
Button - You may see something like:
ButtonΒ byΒ J-M-LEasyButtonOneButtonΒ (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();
}
/////////////////////////////////////////////////////////////////

Leave a Reply
You must be logged in to post a comment.