Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

digitalWrite documentation should mention INPUT_PULLUP

Open matthijskooijman opened this issue 12 years ago • 4 comments

The digitalWrite documentation says:

If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal 20K pullup resistor (see the tutorial on digital pins). Writing LOW will disable the pullup.

However, I'd consider setting the pin mode to INPUT_PULLUP a better way of enabling the pullup, since the fact that writing HIGH to an input pin enables the pullup is really an implementation detail of the AVR (which is emulated in the SAM digitalWrite, though).

How about:

If the pin is configured as an INPUT, digitalWrite will enable (write HIGH) or disable (write LOW) the internal pullup on the input pin. However, the recommended way to enable pullups is to call pinMode with INPUT_PULLUP.

And to preserve (and expand) the information about the pullup's value, I'd suggest adding this to the pinMode page (and perhaps also to the digital pins tutorial):

The value of this pullup depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ. For the exact value, consult the datasheet of the microcontroller on your board.

Furthermore, I'd suggest the following two paragraphs should be moved from the digitalWrite page to the pinMode page, since they really don't have anything to do with using the pin as an output.

The pullup resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause. The remedy is to set the pin to an output with the pinMode() function. NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7 V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, use an external pull down resistor.

matthijskooijman avatar Jan 31 '14 12:01 matthijskooijman

I think you're right that the documentation should be updated to include INPUT_PULLUP, which was probably added after this page is written. One thing that might worth keeping in mind, though, is that some of these comments are intended for people that digitalWrite() to a pin without setting it as an output with pinMode(). On AVR, this will cause an LED to light up dimly, making them think something is wrong with the LED or the circuit as opposed to the code. So it might be good to keep a note about that on this page.

damellis avatar Jan 31 '14 12:01 damellis

Good point. Perhaps put this on the digitalWrite page instead, then:

If the pin is configured as an INPUT, digitalWrite will enable (write HIGH) or disable (write LOW) the internal pullup on the input pin. However, the recommended way to enable pullups is to call pinMode with INPUT_PULLUP. NOTE: If you connect a LED to a pin, use digitalWrite to set the pin HIGH and the LED only lights up dimly, you probably forgot to set the pinMode to output (and thus the digitalWrite will have enabled the internal pull-up resistor, which acts like a very big current-limiting resistor).

How's that?

matthijskooijman avatar Jan 31 '14 14:01 matthijskooijman

incorporated some of these notes to the digitalWrite page and the digital pins tutorial.

shfitz avatar Feb 03 '14 14:02 shfitz

Looking good, thanks!

matthijskooijman avatar Feb 03 '14 18:02 matthijskooijman