Go Down

Topic: Right method for activating internal pull-up resistors (Read 1 time) previous topic - next topic

trurl

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)

Nick Gammon

How would it damage the pin? An input pin is high impedance.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Riva

pinMode(pin, INPUT_PULLUP); works on IDE v1.0 and up
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

Nick Gammon

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

Code: [Select]

} 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

MarkT

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.
[ I won't respond to messages, use the forum please ]

trurl

Nick Gammon:

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


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

Riva:

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

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Riva


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  :smiley-red:
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

retrolefty

Quote
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

Nick Gammon


* 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

trurl

Lefty:

Quote
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

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

Go Up