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)
