In this tutorial, we will learn some basic Digital Input and Digital Output operations on ESP8266. I chose the NodeMCU board for demonstrating ESP8266 NodeMCU Input Output operations using LED as a Digital Output device and a Push Button as a Digital Input Device.
ESP8266 NodeMCU Input Output Operations
If you looked at the ESP8266 NodeMCU Pinout, you will realize that not all the GPIO Pins of the NodeMCU Board can be used as simple GPIO Pins foe Digital Input and Output operations.
Out of the 17 GPIO Pins, 8 are already used for other operations like UART, SPI Flash. So, you are left with 9 GPIO Pins. Even in that 9, some of them are used while Boot up process while some have a default state at the time of boot up.
So, you have to be extremely careful in selecting the right GPIO Pins. For example, if you select a GPIO Pin to drive a Relay and if its state at boot is HIGH, then the relay will be turned on during the boot up. This situation may or may not be useful for all the applications.
As a safe bet, I used GPIO4 and GPIO5 i.e., D2 and D1 on NodeMCU for demonstrating this project. These pins do not have any alternate functions and can be used freely without any doubt.
To configure a GPIO pin either as Input or Output, you have to use the pinMode() function. Once the pins is setup, you can read from an input pin using digitalRead() function and write to an output pin using digitalWrite() function.
Components Required
- ESP8266 NodeMCU
- 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.
Code
The code for the project is given below. You don’t have to download any additional libraries.
#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 */ | |
} |
Working
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, I 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 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.
Sample Output Video
The following gif is a small video snippet of the output.
Conclusion
A simple tutorial to understand the ESP8266 NodeMCU Input Output operations of the GPIO Pins. You learned how to setup a GPIO Pin of NodeMCU as either an Input or as an Output.