We will learn some fundamental Digital Input and Digital Output operations on the ESP8266 in this tutorial. The NodeMCU board was chosen to demonstrate ESP8266 NodeMCU Input Output operations with an LED as a Digital Output device and a Push Button as a Digital Input Device.

Components Required

  • ESP8266 NodeMCU-NodeMCU is an open source firmware for which open source prototyping board designs are available. The firmware is based on the eLua project, and built on the Espressif Non-OS SDK for ESP8266. It uses many open source projects, such as lua-cjson and SPIFFS
  • 5mm LED
  • Micro USB Cable
  • 330 Ω Resistor
  • 10 KΩ Resistor
  • Push Button
  • Breadboard
  • Breadboard Power Supply (optional)
  • Connecting Wires

Circuit Diagram

The following image shows the circuit diagram for demonstration of ESP8266 NodeMCU Input Output Operations.



#define buttonPin 4 /* GPIO4 (D2) for Push Button */
#define ledPin 5 /* GPIO5 (D1) for LED */
int ledState = LOW; /* Variable to hold the current state of LED Pin. Initialized to LOW */
int buttonState = LOW; /* Variable to hold current state of Button Pin. Initialized to LOW */
int buttonValue; /* Variable to store state of the Button */
int lastButtonState = LOW; /* Variable to hold the previous state of the Button. Initialized to LOW */
long lastDebounceTime = 0; /* Variable to hold the last time the LED Pin was toggled */
long debounceDelay = 50; /* Debounce Time */
void setup()
  pinMode(buttonPin, INPUT); /* Initialize Button Pin as Output */
  pinMode(ledPin, OUTPUT); /* Initialize LED Pin as Input */
void loop()
  buttonValue = digitalRead(buttonPin); /* Read the state of the Button into the variable: buttonValue */
  /* Reset the debounce timer after button press */
  if(buttonValue != lastButtonState)
    lastDebounceTime = millis();
  /* Use the button state after waiting for debouncing */ 
  if((millis() - lastDebounceTime) > debounceDelay)
    if(buttonValue != buttonState) /* Check if the button state has changed */
      buttonState = buttonValue;
      if(buttonState == HIGH) /* If the button state is HIGH, toggle the LED state */
        ledState = !ledState;
  digitalWrite(ledPin, ledState); /* Set the new state of the LED */
  lastButtonState = buttonValue; /* Store the present button state for next loop */ 


This is a simple project, where you press a button and the state of the LED will toggle i.e., if it was LOW previously, it will become HIGH and vice-versa. Now, let us go through the code and understand it’s working.

Initially, we define the pin numbers for LED and Push Button. I used “#define” macros but you can also use “const int” variables. GPIO5 or D1 is used for LED and GPIO4 or D2 is used for Push Button.


Then, we declared some variables to hold the status of the LED and Push Button (both current state and previous state of the button and the current state of the LED). Also, there are a couple of variables for Button Debounce implementation.


In the setup() function, you initialize the “ledPin” and output and the “buttonPin” as input using the pinMode() function with appropriate arguments (OUTPUT and INPUT respectively).


Coming to the loop() function, in the first line, you read the state of the button pin using digitalRead() function and store the value into buttonValue variable.


Next is the button debounce part of the code. Wait until the debounce delay is passed since the button is pressed and then use the button state value acquired previously. This will eliminate accidental presses and noise.


If the button state has changed and if it is equal to HIGH, then only toggle the state of the LED.


Finally, apply the new state of the LED using digitalWrite() function and also store the current state of the button.



A basic lesson to help you learn how to use the GPIO Pins on the ESP8266 NodeMCU. You learnt how to configure a NodeMCU GPIO Pin as an Input or an Output.