internal pullup resistors & digitalWrite

This has been discussed at length over on the teachers and developers lists, but I'm repeating it here just to see if folks not following those discussions might want to add their thoughts.

The issue is that the Arduino board provides internal pullup resistors on I/O pins that can be enabled instead of having to wire one in on a circuit. But to enable this capability, you must issue a digitalWrite command on the pin.

This is, at best, very obscure and counter-intuitive to the novice who might be struggling to even understand the concept of a pullup resistor. And, at worst, it can cause execution problems, which I can not even tease out of my own memory, that are very difficult to debug.

So, this is a vote for taking the opportunity of a good conceptual cleanup on that for uno punto zero.

.andy

Sure. Maybe have a core function called enablePullup(pin). If would have to be smart enough to not do anything if the pin's mode is already set as an output pin, or state that it also changes or defines the pin as an input as well as set the pull-up. I guess you would need a disablePullup(pin), or maybe pullUp(pin,state).

Lefty

Unless it causes a problem with the pullup resistors set, just have the IDE by default set ALL digital pullups. By defining a pin as a INPUT or OUTPUT would it become active without having to write anything else.

"IF" doing so can cause execution problems, then maybe we should leave as is? Sure would save several lines of code just to define as INPUT, then digitalWrite (HIGH) for every unused pin.

Ken H>

Automatic inclusion of pullups would not be good. There are too many cases where one would not want them.

Why not simply add another mode parameter option to pinMode -- e.g. pinMode(pin, PULLUPINPUT) or such?

It would only take a #define for "PULLUPINPUT" and a simple change in pinMode() in wiring_digital.c to do the digitalWrite to connect the pullup.

That wouldn't break anything and would be easy to add to the documentation. A few words and maybe a simple illustration could (should) also be put in the documentation to explain the pullup concept to novices.

Automatic inclusion of pullups would not be good. There are too many cases where one would not want them.

agreed

yeah - don't enable pullups by default, please! I have a lot of carrier boards that might do bad things to an arduino if that happened...

Agree. I teach children and beginers using Arduino's, and I would like to introduce people to using the internal pull-up resistors early on, soon after digitalWrite.

I sometimes get a smart a$$ who starts in with "processor X has pull-up resistors built in, why doesn't an Arduino ...", and the opaqueness of the current mechanism is not helpful.

pullUpResistor(pin, ON) pullUpResistor(pin, OFF) would be fine, rather than enablePullUp(pin), disablePullUp(pin)
I don't want pullUps enabled by default as things will break.
Also, if pull-ups are enabled by default, I don't get the errors which give me a chance to explain that switches (floating inputs) need to be pulled to +V or Ground, which seems to be a handy thing to understand, and I lose the chance to talk about pull-down inputs, which people sometimes see in projects, and which seem more logical.

I like the way it's done with the Teensy. pinMode has a third option: INPUT_PULLUP.

What do you think of something like:
pinMode(9, INPUT, true);
to enable pull-up resistors,
pinMode(9, INPUT, false);
to make sure they're disabled, and:
pinMode(9, INPUT);
which would leave the pull-up resistor unaffected?

Something about INPUT_PULLUP just seems a bit clunky.

I like it!

.andy

me 2
Will like it twice as much if we can set the option for outputs als :)o

The benefit of pinMode(9,INPUT_PULLUP) is that its more obvious what the code does than pinMode(9, INPUT, true);
If the user was not familiar with pulls-ups than context sensitive help on INPUT_PULLUP would get him relevant information.

Or, if you prefer the three argument version, you could define PULLUP as true and use: pinMode(9, INPUT, PULLUP)

I agree with mem. The way I see it, digital pins really do have three "modes": input, output, and input w/ pullup.

The alternative is more versatile but, if the versatility is really that important, why not add a new function that allows the two operations to be arbitrarily combined. "pinControl" seems like a good name for such a function.

The benefit of pinMode(9,INPUT_PULLUP) is that its more obvious what the code does than pinMode(9, INPUT, true);
If the user was not familiar with pulls-ups than context sensitive help on INPUT_PULLUP would get him relevant information.

Or, if you prefer the three argument version, you could define PULLUP as true and use: pinMode(9, INPUT, PULLUP)

I like the three element, method best, it is more consistant with previous code) If we #DEFINE PULLUP 1 every one can have it their way. Personally I like rembering TRUE and FALSE best another variable has more charactors to potentially mistype and screw up the case with.

I agree with mem. The way I see it, digital pins really do have three "modes": input, output, and input w/ pullup.

The alternative is more versatile but, if the versatility is really that important, why not add a new function that allows the two operations to be arbitrarily combined. "pinControl" seems like a good name for such a function.

There are more than 3 modes. Pins can be defined as output can benefit from a pullups too.

it is more consistent with previous code

Hi Ray, could you clarify what previous code the three element version is consistent with?

Pins can be defined as output can benefit from a pullups too.

Pull-ups can only be enabled on inputs. Because of the way internal pull-ups are implemented on the controller chip, an output pin whose value is low cannot have pull-ups enabled.

My thoughts were to keep the pin variable consistant, to what its purpose has been in the past. A third variable would indicate that we were doing something different than what we had done in the past.

I have programmed other Atmel chips on their platform and I could have sworn that I set output pullups... I might be wrong, but it does seem that we can't do it on the mega 168/328. Sorry for the confussion but I was quite sure they could be set.

Because the outputs can't be set it may just make more sense to set up the function for the 2 variable inputs as suggested.

Coding Badly, I like your way of putting it. We could think of pins as having three modes: OUTPUT, INPUT, and PULLUP. In this case, would INPUT mode explicitly disable the pullup?

In this case, would INPUT mode explicitly disable the pullup?

I would think so. Otherwise, how would you disable them? :-?

In this case, would INPUT mode explicitly disable the pullup?

I also think this is easy to understand:

INPUT (input mode without pull-up)
INPUT_PULLUP (input mode with pull-up on)
OUTPUT (output mode)

If INPUT turns off the internal pullup, the risk is that existing code will break.

I believe there are two cases that would fail if INPUT disables the pullup...

  1. The user assumed pinMode would not alter the pullup.

  2. The user first called digitalWrite( pin, HIGH ) to enable the pullup then called pinMode( pin, INPUT ) to ensure the pin is configured as an input.

I've searched and searched the web site and the forum and I cannot find a single instance of either of these cases. I cannot find any existing code that would break.

I agree with the two above. INPUT should disable the internal pullup. That appears to be consistent with what users currently expect.