Pages: [1] 2 3   Go Down
Author Topic: internal pullup resistors & digitalWrite  (Read 6605 times)
0 Members and 1 Guest are viewing this topic.
Seattle, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 248
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

.andy

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Mobile, AL
Offline Offline
Sr. Member
****
Karma: 0
Posts: 314
CAUTION!-Slow Learner
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

« Last Edit: February 27, 2010, 05:16:41 am by RoyK » Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

agreed
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
God Member
*****
Karma: 0
Posts: 710
Arduino is Genius
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 11, 2010, 09:52:33 pm by gbulmer » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13018
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 248
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I like it!

.andy
Logged

.andy

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13018
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: