NodeMCU - use flash button as input in loop()

The Flash button on NodeMCU is connected between io 0 and ground. You can use it as a button. Set pinMode(0, INPUT_PULLUP) and you will read io 0 LOW if the button is pressed.

NodeMCU schema

To put the module in flashing mode, first the Flash button must be held and the Reset button pushed, only then should the Flash button be released. Normally, the reset to flashing mode is done automatically by esptool before uploading over USB. This uses the circuit connected to serial RST and DTS lines.


I recommend you EasyButton library, you can use pin 0 (D3) in NodeMCU, if you want use a external button module, you can wire your button between D3 pin and GND.

https://github.com/evert-arias/EasyButton

#include <EasyButton.h>

// Arduino pin where the button is connected to.
#define BUTTON_PIN 0

// Instance of the button.
EasyButton button(BUTTON_PIN);

// Callback function to be called when the button is pressed.
void onPressed() {
    Serial.println("Button has been pressed!");
}

void setup() {
    // Initialize Serial for debuging purposes.
    Serial.begin(115200);
    // Initialize the button.
    button.begin();
    // Add the callback function to be called when the button is pressed.
    button.onPressed(onPressed);
}

void loop() {
    // Continuously read the status of the button. 
    button.read();
}


Just to complete the other answers, here is sample Lua code to help you get started

inpin=3 -- Select input pin - GPIO0 

function pressed()
    print("PRG Pressed")
end

gpio.mode(inpin,gpio.INT,gpio.PULLUP)
gpio.trig(inpin,"down",pressed)

When you press the "Flash" or "PRG" button "PRG Pressed" is printed. You can perform additional magic in the function.

Above sample code adapted from this blog post

I am using this board from MakerFocus