10k-ohm resistor in chapter 2 of arduino projects book

Hello there
I’m an absolute beginner trying to explain to my 13year old boy the chapter 2 of the arduinon projects book. http://arduino.cc/en/ArduinoStarterKit/Prj02
But I can’t explain the reason of the 10k-ohm.
I read that it connects the pin to the ground when button is open, sending LOW.
When the button is closed what happens? The power goes to the pin 2, must it be connected to the ground to send information to pin 2? But isn’t it connected to the ground trough the resistor when button is closed? Is there power in pin 2?
So can you give me more explanation about this resistor?
Thanks !

It is called a "pull-down" resistor. When the button is not pressed it keeps the input of arduino at 0V ("down"). Otherwise, the input of the arduino will float between 0-5V randomly, or it will stay at 5V when the internal pull-up is activated (pull-up is a resistor wired from input to +5V)..

Thank you Pito. So there is not a constant, but just a floating power in the open pin when it's just connected to the ground ? The only constant power would come from the 5v. Then do resistors act like buffers that provide a constant power?

No, there is No voltage on an input pin that is not connected to something. It only takes .000001Amp (1 microAmp, 1 uA) to change that input pin, so the merest of electrical disturbance can make it swing high & low - like the natural electrical field of a human body. The 10K resistor draws any current that might be collected down to Ground.

A better way to connect an input is to use internal pullup resistor, and wire the switch to connect the pin to Gnd when pressed. 2 benefits - no extra resistor is needed - and you can't accidentally short 5V out to something.

pinMode (pinX, INPUT_PULLUP);
or
pinMode (pinX, INPUT);
digitalWrite (pinX, HIGH); // enable pullup resistor

then
if (digitalRead(pinX) == LOW){
// switch is pressed, do something
}
else {
// action to take if switch is not pressed
}

See also 14.2.6 in the '328P datasheet: 14.2.6 Unconnected Pins If some pins are unused, it is recommended to ensure that these pins have a defined level. Even though most of the digital inputs are disabled in the deep sleep modes as described above, floating inputs should be avoided to reduce current consumption in all other modes where the digital inputs are enabled (Reset, Active mode and Idle mode). The simplest method to ensure a defined level of an unused pin, is to enable the internal pull-up. In this case, the pull-up will be disabled during reset. If low power consumption during reset is important, it is recommended to use an external pull-up or pull-down. Connecting unused pins directly to VCC or GND is not recommended, since this may cause excessive currents if the pin is accidentally configured as an output.

Thank you CrossRoads

So if I understand your solution is a software based pull-up or maybe use a resistor that exists inside the micro controller.

If i step back to my first question, in a closed button situation (regardless to the floating situation of an opened button) if we remove the 10k-ohm resistor and rather connect the wire to the ground, would it send "high" to the pin 2 or would it just shorcut the power to the ground?

If you short it to ground with the button pressed you will "crowbar" the supply voltage down to 0V and possibly weld the switch shut.

The issue is the infinite (almost) input resistance of CMOS inputs. At room temperature inputs have a resistance around 10^10 or 10^11 ohms, so that stray electric fields will dominate the voltage on the pin - just bringing your finger near will affect the pin voltage.

The resistor clamps the pin to 0V while the button is open, whilst also limiting the current that flows when the button is pressed. Any value from 100 to 100k will work (larger values may allow noise pick-up if the button is mounted on long wires). Low values waste power, so 10k is a reasonable compromise.

My take one your question about the switch:

When the switch is open, there is infinite resistance to +5v. Current will always take the path of least resistance which in this case is through the 10k resistor to ground. This holds the pin input low.

When the switch is closed (pressed) there is zero resistance to +5. Current will always take the path of least resistance which in this case is through the switch to +5 pulling the pin high.

There are times when pullup resistors are necessary and can't be implemented internally so it's a good concept to understand.

72mb: So if I understand your solution is a software based pull-up or maybe use a resistor that exists inside the micro controller.

There's no such thing as a software based pullup.

The internal resistor is the way to do it except in very special circumstances (I can't even think of one right now).

72mb: if we remove the 10k-ohm resistor and rather connect the wire to the ground, would it send "high" to the pin 2 or would it just shorcut the power to the ground?

It will short-circuit the power and bad things can happen - components can die, etc.

...and this is the reason that the internal resistor is preferred: There's almost no way to mess up and do bad things when you use the internal resistor and connect the pin to GND via a switch.

planecrazy29: My take one your question about the switch:

When the switch is open, there is infinite resistance to +5v. Current will always take the path of least resistance which in this case is through the 10k resistor to ground. This holds the pin input low.

When the switch is closed (pressed) there is zero resistance to +5. Current will always take the path of least resistance which in this case is through the switch to +5 pulling the pin high.

That's correct, but pull-down resistors are a bad idea - you should have as few wires going to the 5V supply as possible in your circuit. Bad things are far less likely to happen if you do that (plus, using internal components instead of external components means less chance to connect things wrong).

Don't believe me? Connect the two versions up on a breadboard (internal pullup vs. external pulldown) and compare them before you hit reply...

fungus: That's correct, but pull-down resistors are a bad idea - you should have as few wires going to the 5V supply as possible in your circuit. Bad things are far less likely to happen if you do that

Cargo-cult circuit design - the electrons simply don't care whether you use pull-down or pull-up, there's no reason there.

Where it does matter is when using remotely mounted switches, since you have to run +5 as well as signals and ground (you want ground for the cable shield).

With pull-ups you can omit the +5v wire and connect the switches to ground instead.

You also have the advantage that if the cable gets crushed or cut the supply won't get shorted out.

However if the circuit's all on one board these issues aren't relevant and the choice between pull-up or pull-down is arbitrary. The availability of on-chip pull-ups means pull-up is convenient, but there are no "bad things" that happen if you use pull-downs.