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?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityThe 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.
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
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.
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