How to Interface NodeMCU with push button?

Image result for remote control leds

In the era full of technologies, we are in search of technologies which we have a full control of. So, we require a gadget that is a semi-automatic ,i.e., which is controlled manually as well as automatically. One component that is used to make our device controlled manually at just one click is a push button.

In this blog you will learn how to :

  1. Interface push button with NodeMCU
  2. Control different LEDs connected to NodeMCU

Now, let's get started. Firstly, the push button is a passive device that is used to switch between the active high and active low states. Now, let's understand what is this active high and active low state. Active low means the function gets done when input is in a low state. Active high means function gets done when input is in a high state. So, how does a push button switches between active high and active low states? In the figure shown below, you can see when the button is not pressed at that time you are getting the output as 1 whereas when we are pressing the button at that time we are getting the output as 0 as when the button is pressed it completes the circuit for discharging through the ground.

So, from the above figure it will be clear to you that when a button is pressed it completes the circuit and changes the path and also changes it's state.

In this way, we will be using the output coming from the above circuit and will send it to the NodeMCU to perform different tasks according to the present state (HIGH or LOW). Now let's interface the above circuit with NodeMCU and control the LED state, i.e., turning the LED ON or OFF. So, let's make the connection for the interfacing of NodeMCU with push button and LED. Now make all the connection as shown below.

So, these are the connections to interface NodeMCU with push button and LED. In the above circuit we are not connecting any external LED because we are using the in-built LED of NodeMCU which is connected to pin D4. For more info and a proper clarification you can also watch the video show below.

Now, our next step is to write a program to perform this task of controlling LED with a push button interfaced with NodeMCU. So the program is as shown below, Now just open Arduino IDE, create a new file and write the program written below:

void setup() {
// put your setup code here, to run once:
pinMode(D3,INPUT);
pinMode(D4,OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
int button=digitalRead(D3);
if(button==1){
digitalWrite(D4,LOW);
delay(100);
}
else{
digitalWrite(D4,HIGH);
delay(100);
}
}

After you are done with the code verify the code and upload it to NodeMCU to test your program. If in case you are facing difficulty while uploading the program the check this blog and try again.
How to use NodeMCU with Arduino IDE for programming?

I hope everything was clear to you that is shown you in the blog, but still if you are having any doubts or any query related to the topic then do leave comment below in the comment section and I'll try to resolve it at the earliest. Also you can suggest me to write a blog for you of your choice if you feel i can explain you well then please suggest in as well.

4 thoughts on “How to Interface NodeMCU with push button?”

Leave a Reply

Your email address will not be published. Required fields are marked *