In this getting started guide you’ll learn how to read digital inputs like a button switch and control digital outputs like an LED using the ESP8266 NodeMCU board with Arduino IDE.

Prerequisites
We’ll program the ESP8266 using Arduino IDE. So, make sure you have the ESP8266 boards add-on installed before proceeding:
ESP8266 NodeMCU Control Digital Outputs
First you need set the GPIO you want to control as an OUTPUT. Use the pinMode() function as follows:
pinMode(GPIO, OUTPUT);
To control a digital output you just need to use the digitalWrite() function, that accepts as arguments, the GPIO (int number) you are referring to, and the state, either HIGH or LOW.
digitalWrite(GPIO, STATE);
Take a look at the ESP8266 GPIO Reference Guide to learn which GPIOs are more suitable to use as outputs.
ESP8266 NodeMCU Read Digital Inputs
First, set the GPIO you want to read as INPUT, using the pinMode() function as follows:
pinMode(GPIO, INPUT);
To read a digital input, like a button, you use the digitalRead() function, that accepts as argument, the GPIO (int number) you are referring to.
digitalRead(GPIO);
Take a look at the ESP8266 GPIO Reference Guide to learn which GPIOs are more suitable to use as inputs.
Project Example
To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.

Schematic Diagram
Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 5 (D1) and the pushbutton to GPIO 4 (D2).
Parts Required
Here’s a list of the parts to you need to build the circuit:
- ESP8266 Boards
- 5 mm LED
- 330 Ohm resistor
- Pushbutton
- 10k Ohm resistor
- Breadboard
- Jumper wires

Code
Copy the following code to your Arduino IDE.
/*
Welcome to Gnd_To_Vcc!!
/*
// set pin numbers
const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
// variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(ledPin, HIGH);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
}
}
Uploading the Code
Before clicking the upload button, go to Tools > Board, and select the board you’re using. In my case, it’s the NodeMCU 1.0 (ESP-12 E Module). If you don’t know your ESP8266 model, you can select “Generic ESP8266 Module”.
Go to Tools > Port and select the COM port the ESP8266 is connected to. Then, press the upload button and wait for the “Done uploading” message.
Demonstration
After uploading the code, test your circuit. Your LED should light up when you press the pushbutton:
And turn off when you release it:
Conclusion
With this getting started guide, you’ve learned how to read digital inputs and control digital outputs with the ESP8266 using Arduino IDE.
If you want to learn how to read analog inputs, or output PWM signals, read the following guides:
You may also find useful taking a look at the ESP8266 GPIO Reference that shows how to use the ESP8266 GPIOs and its functions.