38
\$\begingroup\$

On newer Arduinos, one can set pinMode to one of three states: OUTPUT,INPUT, and INPUT_PULLUP.

This page says:

The Atmega chip on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-down resistors, you can use the INPUT_PULLUP argument in pinMode(). This effectively inverts the behavior, where HIGH means the sensor is off, and LOW means the sensor is on.

I'm rather sure that inverting the behavior isn't the only thing that it does, though.

What does INPUT_PULLUP do? What makes it different from INPUT, and how does one decide which one to use?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ INPUT_PULLUP was added with IDE 1.0.1. It is irrelevant which Arduino board you use, they all support PULLUPs. \$\endgroup\$ Apr 17, 2013 at 16:41

2 Answers 2

37
\$\begingroup\$

The default is INPUT which sets up the pin as an INPUT. If the pin is totally disconnected, it will randomly read HIGH and LOW. If you tie it to +5V or 0V, it will obviously read HIGH or LOW.

Internal to the Arduino, the Atmega chip has internal pullup resistors with a value around 20k ohm. (See the DigitalPins documentation for more details) These resistors can be optionally connected internally using INPUT_PULLUP. This is functionally (and electrically) equivalent to connecting a ~20k ohm resistor between the pin and +5V, the only difference is that it requires no external components and you can turn it on and off in software during the execution of your program.

So why pull-ups and not pull-downs? There are likely several reasons for it, but when wiring buttons or switches or anything "normally open", you only have to tie them to ground, you don't need to run +5V out to them. Since most boards are going to be designed with large ground pours for shielding reasons anyway, tying to ground is practically reasons.

Some more featured ICs like ARM chips have both pull ups and pull downs, but the 8-bit AVR line only comes with pull-ups. You just have to remember that HIGH is "open" and LOW is "closed".

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Upvote. FYI, according to the documentation the internal pull-ups are 20K, not that it makes much difference from 50K. arduino.cc/en/Tutorial/DigitalPins \$\endgroup\$
    – SDsolar
    Feb 26, 2017 at 23:03
14
\$\begingroup\$

Note that previous to 1.0.1, you could turn on pullups by using digitalWrite(). And you still can.

The code:

pinMode(13, INPUT);
digitalWrite(13, HIGH); // Turns internal pull-up on
digitalWrite(13, LOW);  // Turns internal pull-up off

This is an important distinction because INPUT_PULLUP obviously turns on the pull-up resistor. However, less obvious, is that starting with 1.0.1, simply calling INPUT forces the pull-up to be turned off. (Previously, the state of the pull-up stayed the same).

To see how a pin operates with and without pull-ups, watch the following videos.

Floating pin on an oscilloscope: http://www.youtube.com/watch?v=dBIBFLYCjMM

With Pull-Up enabled: http://www.youtube.com/watch?v=SAIw7LLVl-U

Full tutorial video on Pull-Ups (note that I made this before INPUT_PULLUP was added to the Arduino library): http://www.youtube.com/watch?&v=jJnD6LdGmUo

\$\endgroup\$
3
  • \$\begingroup\$ Very clever. Thank you for writing this. Upvote. \$\endgroup\$
    – SDsolar
    Feb 26, 2017 at 23:01
  • \$\begingroup\$ Two first videos are gone, but the third one is still there and does the job :-) \$\endgroup\$
    – Philippe
    Feb 7 at 12:51
  • \$\begingroup\$ I've re-enabled the first two, although the others cover it. Also search "AddOhms Pull Up" on youtube for a couple I did later. \$\endgroup\$ Feb 8 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.