Nothing happens on button press

I am trying to do something I thought would be simple.... All I want to do is register a change (High OR low, doesn't matter to me) on the press of external button.

I was able to get an IFTTT event working on startup, but this approach required resetting the board, and reconnecting to Wifi every time, which took too long. So all I want to do is run it with an IF statement in my loop instead.

After I could not get a button to work, I tested my sanity (and my code) by chucking an analogue pot on there and said if below x value, then do a thing, else don't do the thing - and Yep, this was fine.... But with the button on a digital pin.... I just cant seem to get any setup to work!

I have been through countless forum posts, I have tried every pin, I tried pullup with no resister approach, the 10k ohm resister approach, different buttons... I just cant NOT do this simple thing...

  • I am using Lolin Node Mcu v3 (but will need to do this on a D1 mini when it arrives in the mail)
  • I want to run the board from USB power
  • Happy to go with a resister if needed, or not...

Can someone please help me with this most simple of wiring set ups? Because apparently, I'm stupid!

[cough]... you haven't shown us your circuit...

I have been through countless forum posts

Did you encounter this one?:

1 Like

As above... you will get a lot more help if you...

  • Please post your code.
  • Please post a picture / photo of how everything is connected.

My apologies, I have tried so many different approaches.. I didn't know what to post... but this is the most recent attempt...

int pin = 0;

 void setup()
 {
   pinMode(pin, INPUT);
   digitalWrite(pin, HIGH);
   Serial.begin(9600);
  }

int buttonStatus = 0;

 void loop() //if deep sleep is working, this code will never run.
 { 
  // print out the value
  Serial.println(digitalRead(pin));
  delay(500);
 }

You can preferably post pictures here. Not interested in links...
pin = 0; That looks bad.
pinMode(pin, INPUT)... Seldomly good.

Please post images in line, like this

Looks like you missed the basic button tutorials. Search phrase "arduino button".

It is best that the button connects the input pin to GND.
Use pinMode(buttonpin, INPUT_PULLUP); to set the default state to HIGH.

And use a different pin than the serial console RX for the button.

Then it's likely you didn't follow the advice to read the forum guidelines, because that question is answered there.

When you read it, you will also see that off site links are not the way to post diagrams...

Generally don't use pins 0 & 1... they are used for Serial.
EDIT. Ignore that I see you are using "Lolin Node Mcu v3"... but GPIO may still have another special use on that uC.

Better to use INPUT_PULLUP then look for LOW when the button is pressed. Then you don't need an external resistor.

Can you show us how things are connected.

Apologies for lack of Forum Decorum, I have replaced the link with the uploaded image.

I have now gone basics, as suggested and looked at the button example provided with the IDE...

I still get no change in my serial output.

This is the wiring converted to how I think it should be for the Lolin board...

and this is my code


const int buttonPin = 5;  // the number of the pushbutton pin
const int ledPin = 2;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    Serial.println("high");
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(500);
  } else {
    // turn LED off:
     Serial.println("low");
    digitalWrite(ledPin, LOW);
    delay(500);
  }
}

Yep, my bad, I have updated the link. Thanks for the feedback.

Have you tried bypassing the switch, i.e. orange wire to blue wire direct?

Edit - Does the LED change state?

Button connections are possibly wrong. Depending on the orientation, the button simply connects the power supply to ground via the resistor.

Use INPUT_PULLUP as you have been advised, have the button connect the switch to GND when pressed, and remove any connection to Vcc.

Use your multimeter to determine which orientation is correct.

Capture

1 Like

So, I am now going Pin 5 switch and switch to GND
with this code:

const int buttonPin = 5;  // the number of the pushbutton pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  digitalWrite(buttonPin, HIGH);
  Serial.begin(9600);
  
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    Serial.println("high");
    delay(500);
  } else {

    Serial.println("low");
    delay(500);
  }
}

And the serial output is always High...

I was able to confirm with my multimeter that on a particular setup, the 2 pins on the same side showed 0 when pressed but greater than 0 when not pressed...

I did also try, as per previous setup, bypassing the switch and connecting blue to orange directly... and the serial out, still does not change.

Im sure the board is OK, as I have done other tutorials successfully. I must be doing something wrong still....

Hi,
Can you please post an image of your project, so we can see your component layout?

Do you have a DMM?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Pin naming can sometimes be a problem with ESP boards.

D5 is GPIO14.
try
const int buttonPin = D5;
or
const int buttonPin = 14;
Leo..

1 Like

THANK YOU!!!! SO SIMPLE!!!!!

I just had to use "D5" not "5"

ARGHH!H!H!H!!

If you use INPUT_PULLUP you can makes everything a lot simpler.

That is...

  • you can get rid of the external resistor.
  • connect one side of the button to GND.
  • connect the other side (diagonally opposite recommended) to the GPIO pin.

As others have said, be careful about the pin assignment. D2 and GPIO2 are not the same thing.

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