Arduino IDE NodeMCU ESP-12E Digital Input Basics

Digital Input from a ESP8266 NodeMCU Development Board

There will be many occasions in your projects where you want measure a high or low input.  This high or low input may source from a switch or it may be a signal from another device.   This tutorial will show you how to use a GPIO pin on your NodeMCU development board to read high and low inputs.

This tutorial assumes that you know how to load a sketch to your NodeMCU development board using the Arduino IDE.   If you’re new to this and don’t know how,  you can read the NodeMCU Getting Started Tutorial found HERE.

Necessary Material

Amazon     eBay      BangGood      DealExtreme      ITead

 

ESP-12E NodeMCU High and Low Inputs

A high input is one that approaches the 3.3V supply input to the NodeMCU development board.  A low input is one that approaches ground.   The actual specifications are illustrated in the graphic below.

This will not be of too much concern to you in this tutorial as we are using a simple switch to detect the presence of a ground that is relatively close to the circuit board.   However,  it something that you will want to understand as you design your own circuits.   You can read more about this in the article found HERE.

The pinMode Command

In our setup(),  we use the pinMode command to tell the ESP8266EX what we want it to do.    The syntax is illustrated below:

The key thing to remember is that you do not specify the development board pin number, but the GPIO number of the EXP8266EX Chip.   The graphic below will help you to identify which number to use.  You may click on it to enlarge.


Another determination that has to be made is whether or not to use the INPUT_PULLUP or INPUT_PULLDOWN.

In this tutorial a switch will provide a ground to an input.  What we need to be concerned about is what that input will see when the ground is not applied.   While a ground is low,  the absence of that ground does not necessarily mean we see a high.   In order to ensure that we see a high when the switch is not pressed,  we will specify INPUT_PULLUP.

NodeMCU ESP-12E Arduinio IDE Digital Input Tutorial

Connect the Circuit

You may wish to power your NodeMCU another way.   You can read about it HERE.

Copy, Paste and Upload the Code

The code is real simple works as follows:

  • The input is read from switch pin  If switch is closed, it will read a low.
  • The LED output pin is set to the opposite of the switch pin.   If the switch is closed, the output pin will be set to a high.  A high will turn on the LED.
int switchPin = 0;
int LedPin = 16;
int switchValue;

void setup() { 
  pinMode(LedPin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop() {
  // Read the switch value
  switchValue = digitalRead(switchPin);
  /* Set the LED output pin the opposit of what is read on the switch
   * input pin  
   */
  digitalWrite(LedPin, !switchValue); 
   
}

Other Things to Try

This tutorial simple reflects that way I chose to respond to digital input from a switch.  Your circuit could just as easily been assembled differently.  For example:

  • You may choose to turn the light off with the switch.
  • You may have supplied a high with the switch.
  • You might want to turn on the LED simply by providing  a low.