ESP8266 NodeMCU Digital Inputs and Digital Outputs (Arduino IDE)

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.

ESP8266 NodeMCU Read Inputs Control Outputs 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.

ESP8266 NodeMCU button pressed or not to turn LED on and off

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:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

ESP8266 NodeMCU Digital Input and Digital Output Schematic Circuit LED Pushbutton

Code

Copy the following code to your Arduino IDE.

// Complete Instructions: https://RandomNerdTutorials.com/esp8266-nodemcu-digital-inputs-outputs-arduino/

// 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);
  }
}

View raw code

How the Code Works

In the following two lines, you create variables to assign pins:

const int buttonPin = 4;
const int ledPin = 5;

The button is connected to GPIO 4 and the LED is connected to GPIO 5. When using the Arduino IDE with the ESP8266, 4 corresponds to GPIO 4 and 5 corresponds to GPIO 5.

Next, you create a variable to hold the button state. By default, it’s 0 (not pressed).

int buttonState = 0;

In the setup(), you initialize the button as an INPUT, and the LED as an OUTPUT. For that, you use the pinMode() function that accepts the pin you are referring to, and the mode: INPUT or OUTPUT.

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

In the loop() is where you read the button state and set the LED accordingly.

In the next line, you read the button state and save it in the buttonState variable. As we’ve seen previously, you use the digitalRead() function.

buttonState = digitalRead(buttonPin);

The following if statement, checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite() function that accepts as argument the ledPin, and the state HIGH.

if (buttonState == HIGH) {
  digitalWrite(ledPin, HIGH);
}

If the button state is not HIGH, you set the LED off. Just set LOW as a second argument to in the digitalWrite() function.

else {
  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:

ESP8266 NodeMCU Input Output Button Pressed LED On Arduino IDE

And turn off when you release it:

ESP8266 NodeMCU Input Output Button Not Pressed Arduino IDE

Wrapping Up

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.

Finally, if you want to learn more about the ESP8266, take a look at our resources:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

15 thoughts on “ESP8266 NodeMCU Digital Inputs and Digital Outputs (Arduino IDE)”

  1. Good article. One question: When I started programming microprocessors in the early days (40 years ago on the Motorola 6802), we spent ages debouncing the inputs but no one seems to bother with this these days. Is it still an issue, or is it, with this example, it doesn’t matter.

    Reply
    • Hi David.
      Depending on the application, you might need to debounce the inputs. In this particular case, it is not necessary.
      However, if you want an example for a debounce sketch, in your Arduino IDE, you can go to File > Examples > 2.Digital > Debounce
      Regards,
      Sara

      Reply
  2. Hi Sara,
    Interesting beginner’s article based on the standard features of the Arduino core.
    However the ESP family is not a good platform to start for a beginner. Specifically when it comes to digital I/O, not all pins are born equal on an ESP whereas the legacy Arduino platform is much more easier to apprehend.
    I believe it is important to explain that some pins have a specific usage. Some pins will have mandatory pull-up or pull-down on the board which will impact their usage.
    Things that can get a beginner very easily confused not understanding why your sample works wiht GPIO4 and 5 but doesn’t work with GPIO0, 2 or 15 (while on legacy AVR Arduino they all work the same).
    Cheers
    Keep going on and keep safe
    #stayathome

    Reply
  3. Hi,
    Just to inquire if possible to combine the W5500 LAN Ethernet module with ESP8266.
    Like all credential store using wifi manager then after that the communication server using LAN ethernet W5500.
    because i am looking for same example using ESP8266 with Ethernet LAN Module but cant find any..
    Thank you

    Reply
  4. Hi,
    Thank you for the example. Would it be possible to make a lesson that would explain how to have the switch in this example control an output on a RPi over MQTT?
    Thanks, rr

    Reply
  5. But what if I want to connect some normal modules and leds to NodeMCU? How can I extend the number of ports available on nodemcu then for general purpose input and output? With Arduino it wasn’t a problem but with Nodemcu I think the ports are just 2 that you mentioned.

    Reply
  6. Hi, I have a simple question about the circuit.
    Why did you choose a 330ohm resistor?
    Based on my formula I got a 65 ohm resistor.
    3.3V – 2V (red 5mm) / 20mA = 65ohm.
    Am I doing something wrong?

    Regarding the pullup/pulldown resistor. I have a 1Kohm resistor. Is that enough??:
    3.3V – 0V / 1Kohm = 3.3mA
    P = 10.89 mW

    Thanks in advance.
    Facundo.

    Reply
  7. Greetings and nice article!
    I love that there is ‘advanced beginners tutorial’ that is not using just besic android.
    This article says that you should use D0…D5 instead of just 1…5
    I had some problems when I used just numbers when pointing pinMode.

    instructables.com/Get-Started-With-ESP8266-NodeMCU-Lolin-V3/

    Reply
    • Hi.
      We recommend using the corresponding GPIO number instead of D0…D5.
      For example, from the pinout diagram, you know that D1 corresponds to GPIO 5, so in your code, simply use 5.
      I hope this is clear.
      Regards,
      Sara

      Reply
  8. Hello!

    I followed the tutorial and the LED does indeed light up when I press the button, but it doesn’t turn off when I release it. I have to touch the physical pins on the NodeMCU module for it to turn off. What am I doing wrong?

    Reply

Leave a Reply to Carlos Bruni Cancel reply

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.