Go Down

Topic: internal pullup resistors & digitalWrite (Read 7077 times) previous topic - next topic

ahdavidson

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
.andy

retrolefty

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


KenH

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>

RoyK

#3
Feb 27, 2010, 11:14 am Last Edit: Feb 27, 2010, 11:16 am by RoyK Reason: 1
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.


Osgeld

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


agreed
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

merlin_jim

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...

gbulmer

#6
Mar 12, 2010, 03:48 am Last Edit: Mar 12, 2010, 03:52 am by gbulmer Reason: 1
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.

Coding Badly

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

mellis

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.


Ray Lloyd

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

mem

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)

Coding Badly

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.

Ray Lloyd

Quote
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.
Quote
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.

mem

Quote
it is  more consistent with previous code

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

Quote
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.

Go Up