Right method for activating internal pull-up resistors

This is the method I've seen everywhere:

pinMode(pin, INPUT);
digitalWrite(pin, HIGH);

What happens if pin already is under a non-ground voltage? Between the pinMode and the digitalWrite the current can freely flow in and damage the pin, right?

Isn't more correct the reverse approach? (putting the pin HIGH and then changing the mode to INPUT)

How would it damage the pin? An input pin is high impedance.

pinMode(pin, INPUT_PULLUP); works on IDE v1.0 and up

Just for interest, INPUT_PULLUP does it in the order: set-input-mode, set-pin-high:

	} else if (mode == INPUT_PULLUP) {
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;        // set to INPUT
		*out |= bit;         // output HIGH
		SREG = oldSREG;

Comments added by me.

Most times the setup for pins is done in setup(), and at that point all pins are in INPUT mode anyway (after reset). If you have a signal that must be kept pulled-up across resets (and from power-up to sketch starting) then a physical pull-up resistor is called for.

Nick Gammon:

How would it damage the pin? An input pin is high impedance.

doh! so the pull-up is just for avoiding floating values.

Riva:

pinMode(pin, INPUT_PULLUP); works on IDE v1.0 and up

One line of code less. Nice.

BTW, even if the pins default to INPUT, IMHO it is a good practice to explicitly set the mode. Sort of self-documenting software.

MarkT: Noted.

My circuit has some normally-open buttons. I was concerned about the possibility of having them pressed before the pin was configured. I see that there is no reason for worrying.

Thanks all.

INPUT_PULLUP was added in 1.0.1:

ARDUINO 1.0.1 - 2012.05.21

  • Added INPUT_PULLUP option for pinMode(). The INPUT mode now explicitly
    disables the pullup resistors.

CrossRoads:
INPUT_PULLUP was added in 1.0.1:

ARDUINO 1.0.1 - 2012.05.21

  • Added INPUT_PULLUP option for pinMode(). The INPUT mode now explicitly
    disables the pullup resistors.

Thanks for correcting me :blush:

My circuit has some normally-open buttons. I was concerned about the possibility of having them pressed before the pin was configured. I see that there is no reason for worrying.

Some people wanting 'fool proof' button circuits will wire a series 200 ohm resistor from the I/O pin to the button switch as a extra safety measure, say from a human programming error where you set a pin to output by mistake (say setting it HIGH) and then the poor user presses the button wired to ground and poof goes the I/O pin. RuggedCircuits uses that in their ruggedized version of a arduino board.

Lefty

CrossRoads:

  • Added INPUT_PULLUP option for pinMode(). The INPUT mode now explicitly
    disables the pullup resistors.

Ach! I'd forgotten about that. So reversing the order won't work at all now.

Lefty:

Some people wanting 'fool proof' button circuits will wire a series 200 ohm resistor from the I/O pin to the button switch as a extra safety measure, say from a human programming error where you set a pin to output by mistake (say setting it HIGH) and then the poor user presses the button wired to ground and poof goes the I/O pin. RuggedCircuits uses that in their ruggedized version of a arduino board.

My brain-fart was caused by a tutorial giving precisely that advice:

http://www.ladyada.net/learn/arduino/lesson5.html

Whats this 100? resistor all about? There's a 100? resistor we use to connect the input pin to either HIGH or LOW voltage. Why is it there? Well, lets say you accidentally set P2 to be an OUTPUT type pin, but then you connected it to 5V. If you write a LOW to the pin (0V) but its connected to HIGH (5V), you've basically caused a short circuit at that pin. This isn't very good for the pin and could damage it! The 100? resistor acts as a buffer, to protect the pin from short circuits.

(I'm not blaming the tutorial, but my faulty memory.)