ezButton and INPUT_PULLUP

Prompted by discussion in this thread, I opened the ezButton source code.

I was wondering if ezButton enables INPUT_PULLUP by default. Their tutorial text implies that it does, and certainly the diagram there shows no resistors (down or up) in their circuit.

But all the examples have this line:

ezButton button(7); // create ezButton object that attach to pin 7;

.... which do not explicitly enable INPUT_PULLUP there, so does the library do that under the hood I asked myself.

The library source has this line, but my C knowledge is too limited to decipher it. I suspect it means that if you don't actually say INPUT_PULLUP, then it pretends you did:

ezButton::ezButton(int pin): ezButton(pin, INPUT_PULLUP) {};

That line is followed by this block, where I see it does a pinMode(btnPin, mode); :

ezButton::ezButton(int pin, int mode) {
	btnPin = pin;
	debounceTime = 0;
	count = 0;
	countMode = COUNT_FALLING;

	pinMode(btnPin, mode); //<<<<<<<<<<<<<<<<<<<<<<<<<

	previousSteadyState = digitalRead(btnPin);
	lastSteadyState = previousSteadyState;
	lastFlickerableState = previousSteadyState;

	lastDebounceTime = 0;
}

Sure, if I had this in my code:

ezButton button(7, INPUT_PULLUP);

... I can see that their "mode" would explicitly be "INPUT_PULLUP", and so my question is, does this line in the library:

ezButton::ezButton(int pin): ezButton(pin, INPUT_PULLUP) {};

... force INPUT_PULLUP even if I don't ask for it?

To test that, I did a short sketch where I declared an ezButton with no explicit INPUT_PULLUP and then read the pin back in loop(). For as long as I let it run, the pin was high, but while that's indicative of the pin being held high by the pullup it's not definitive.

Test code:

#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup()
{
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  Serial.println("start....");
}

void loop()
{
  button.loop(); // MUST call the loop() function first
  Serial.println(digitalRead(7));
  delay(500);
}

Output: (It stayed as 1 as long as I cared to watch)

image

The ezButton library has 2 functions to set up a button. In C++ terms it is overloaded

		ezButton(int pin);
		ezButton(int pin, int mode);

So you can either take the default or set the pinMode explicitly by using the appropriate version

This line in the .cpp file

ezButton::ezButton(int pin): ezButton(pin, INPUT_PULLUP) {};

causes a call to the version without an explicit pinMode to call the other version with a default pinMode of INPUT_PULLUP

To summarise :
You can choose to specify the pinMode explicitly by using it in the function call or take the default of INPUT_PULLUP by not using a pinMode in the function call

1 Like

Great thanks: I thought something along those lines but wasn't sure of the mechanism, and didn't know the term "overloaded".

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