NodeMCU with Key Button Module

I am trying to connect Key Button Module with NodeMCU. The key button module has 3 PINs - VCC, OUT and GND. I connected VCC to 3V3, GND to GND and OUT to D2 of nodemcu. Button purchased here - https://robu.in/product/red-electronic-building-blocks-big-key-button-module-high-level-output/

I have written a small program to blink LED ON/OFF on pressing and releasing a button.

int buttonPin = D2;
int val;
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  pinMode(D4, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = digitalRead(buttonPin);
  if (val == LOW) {
    Serial.println(val);
    int count = 0;
    digitalWrite(D4, LOW);
  } else {
    digitalWrite(D4, HIGH);
  }
}

When I press and release the button, LED is not blinking. I am not sure what is going wrong in this simple example. Appreciate any help.

Thanks.

Try printing val before testing its value
Does it change if you hold the button in ?

Whatever the problem is, it's not in your code. I loaded it into a handy NodeMCU and as you'd expect, when D2 was grounded, the onboard LED was on, and when D2 was pulled up to 3.3V, it was off.

I added print statements. The value doesn't change on pressing the button.

Thank you for confirming, My nodemcu is working fine, I tried without this button.

Can you measure the voltage between the OUT pin and GND pin of the button when it is pressed and released ?

How have you connected the button to the NodeMCU, directly or via breadboard ? Check all of the connections carefully

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