Push the button...

NodeMCU is not just an output device. It can accept triggers from its environment and act respectively, according to the program that it has on its memory.

In this example we 'll try to light on an LED every time we push a pushbutton. It is called so, because it connects its legs when someone push it for a moment and removes the connection when the button is not pressed anymore.

http://thumbs3.ebaystatic.com/d/l225/m/mdqVKBu1On7rLNet7EIPtHw.jpg

Our components will be arranged on the breadboard like below:

We notice here that we use D0 (pin 16) as an input pin to accept commands from the button and D1 (pin 5) as an output pin to send the signal to the LED for the ON-OFF operation.

In our case, when the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 3.3 volts, so that we read a HIGH.

As for the code for the above, it looks like that:

int ledPin = 5; // choose the pin for the LED
int inPin = 16;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == LOW) {         // check if the input is LOW (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

If you are wondering what are // characters, they are used as comments starting characters, so everything that follows in the same line is not interpreted. We can give notices to the other programmers about our ideas and explain our thoughts in a few words.

An other new structure that we notice in our code is the IF structure. With this, we can decide what actions we choose based on a codition.
So in our example, first we read the state of pin16 and if it is LOW (unpressed) then we turn the LED off (pin 5). If not, we turn the LED on.

So, copy the code in bold and paste it to Arduino IDE. Then upload your code to NodeMCU and.......play with the button!