Esp8266 2-pin button not working correctly

Hi guys i have one of those 2 pin buttons and im trying to connect it to my nodemcu 8266, using this code:

//in setup:
pinMode(16, INPUT_PULLUP);
// in update:
  int but1 = digitalRead(16);
  if(but1 == HIGH){ }else{
    Serial.print("BUTTON Low");
  }

when i press the button once it goes from HIGH to LOW and stays on LOW forever unless i restart or replugg the 8266, any ideas?

one pin is connected to ground and the other to GPIO 16 on the esp8266

What does that mean ?

Please post your full sketch

what happens if you try this code?
(GPIO 16 not the best choice as D0 has a special status)

const byte buttonPin = 16;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
 }

void loop() {
  int buttonStatus = digitalRead(buttonPin);
  if (LOW == buttonStatus) {
    Serial.println("BUTTON IS LOW");
  }
}

that is the full sketch

I beg to differ. For a start, the code that you posted has no setup() or loop() functions

didnt change anything, should i use another pin? here is the pinout of my board:

yes connect to D4 and use this code

const byte buttonPin = D4;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
 }

void loop() {
  int buttonStatus = digitalRead(buttonPin);
  if (LOW == buttonStatus) {
    Serial.println("BUTTON IS LOW");
  }
}

ensure the wiring is D4 --- button ---- GND

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.