NodeMCU ESP8266 button wiring problem Hello guys, this is my code and I am basically doing if-else statement.

int led = D2;   
int but = D1;    

void setup() {
  pinMode(led, OUTPUT);      
  pinMode(but, INPUT);   

}

void loop() {
  if (digitalRead(but) == HIGH){
    digitalWrite(led, HIGH);
  }else{
    digitalWrite(led, LOW);
  }


}


I have also attached the photo so that you can diagnose the situation.

The led show only glow if the button is pressed. But the led keeps glowing for longer time and I don't know why.
I think there is current leak somewhere in the circuit or the internal processing is slow.
Please help me guys.
Your button and pull-up configuration is not correct, so when the button is not being pressed, the pin D1 is floating, being pulled neither HIGH nor LOW.

Instead, use a pull-up resistor as shown. When the button is not pressed, D1 is connected to GND through a resistor. When the button is pressed, the 3.3V source has a direct path to D1.







simulate this circuit &ndash; Schematic created using CircuitLab