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