Pages: [1]   Go Down
Author Topic: Right method for activating internal pull-up resistors  (Read 965 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Norfolk UK
Offline Offline
Faraday Member
**
Karma: 69
Posts: 2557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
} 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.
Logged


0
Offline Offline
Shannon Member
****
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 538
Posts: 27142
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 69
Posts: 2557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


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

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
 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: