ESP8266 Arduino Core Interface Button
In this tutorial, we will learn how to interface a push button with ESP8266 using the Arduino Core. A push button is one of the simplest input devices, but it is essential in embedded systems to provide user interaction. By reading the button state, we can trigger actions such as turning on LEDs, sending data to the cloud, or controlling devices in IoT applications.
This example will read the button’s state (pressed or not pressed) and print it on the Serial Monitor.
Required Hardware
- ESP8266 NodeMCU Dev Kit (or ESP8266 with USB programmer)
- 1 × Push Button
- 1 × 10kΩ Resistor (for pull-down, if required)
- Breadboard & Jumper Wires
Required Software Tools
- Arduino IDE (latest version)
- ESP8266 Core installed in Arduino IDE
Circuit Connection
- Connect one side of the button to GPIO pin D2 (or GPIO4).
- Connect the other side of the button to GND.
- Use the ESP8266’s internal pull-up resistor in software, so no external resistor is strictly required.
Code
/*
http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Arduino-core/ESP8266-Arduino-Core-Interface-Button
Tested By : Arun (20170219)
Example Name : AEW_Button_DigitalRead.ino
*/
// Pin connected to the button
const int buttonPin = D2; // GPIO4
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
Serial.println("ESP8266 Button Interface Example Started...");
}
void loop() {
// Read button state
int buttonState = digitalRead(buttonPin);
// Since INPUT_PULLUP is used: LOW = pressed, HIGH = not pressed
if (buttonState == LOW) {
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
delay(200); // small delay for stability
}
Output on Serial Monitor
When you press and release the button, you will see the following messages in the Serial Monitor (set to 9600 baud):
Button Released Button Released Button Pressed Button Pressed Button Released
Explanation
- pinMode(buttonPin, INPUT_PULLUP) → Enables ESP8266’s internal pull-up resistor. This saves external resistors and wiring.
- digitalRead(buttonPin) → Reads the state of the pin (HIGH or LOW).
- Since we used
INPUT_PULLUP, the logic is inverted:- LOW = Button Pressed
- HIGH = Button Released
Applications of Button with ESP8266
Trigger an event like sending data to a cloud (MQTT, Blynk, Thingspeak).
Reset or reconfigure WiFi credentials.
Toggle LEDs, relays, or appliances in IoT projects.
Act as a manual override for automated systems.