14

I couldn't find a proper answer on the net so I came here as a last resort. So I want to use the FLASH button as an input in the loop() method.

Is there a way the check if it's pressed or not?

ESP module with the flash button

0

5 Answers 5

17

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.

0
6

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();
}

0
3

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

2

I often use it as an input if I need a button input on my boards. After the initial boot sequence it is just another I/O pin.

2

The top answer only works for me when the Nodemcu is connected via a serial port and the "Serial Monitor" is turned on. (It was frustrating because it would always work whenever I connect up the serial monitor for debugging and then stop working when I disconnect it to run standalone). I think this is because the Nodemcu connects GPIO0 and GPIO16 to the UART's RTS and DTR signals. The buttons only work when those control lines are in the right states and that might not always be the case. See the third page in the schematic at https://github.com/nodemcu/nodemcu-devkit-v1.0/blob/master/NODEMCU_DEVKIT_V1.0.PDF

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.