Resistor with Push Button

Why exactly do you need a 10K ohm resistor for a push button in a circuit like in this example? http://arduino.cc/en/Tutorial/Button

Is it because 5V going directly into the ground pin can damage it?

The resistor pulls the input to a logical HIGH. If the pin was disconnected, it would float and the values would be unreliable, at best. If it was connected directly to VCC, then a pushed down button would cause a short circuit between VCC and GND and is likely to damage something.

Alright that makes sense. Why use 10K ohms and not 1K or 330 ohms?

Why use any external resistor at all?
Use the internal pullup resistor:

pinMode ( input_pin, INPUT );
digitalWrite ( input_pin, HIGH ); // enables internal pullup

and then assuming a normally open contact button, check for a low to see if the button is pressed.

if ( digitalRead ( input_pin ) == LOW ){
// do some action
}

All you have to do is activate the internal pull up for the I/O pin and you won't need the resistor?

Yes. CrossRoads, to simplify that into just one line, couldn't you just use

pinMode ( input_pin, INPUT_PULLUP);

Thanks for the information about input pullups. It simplifies my breadboard, if I don't have to have those extra resistors.
:smiley:

schoolsterz123:
Alright that makes sense. Why use 10K ohms and not 1K or 330 ohms?

When the button is depressed, current flows. The lower the resistance, the more current flows, and more energy is wasted.

Alright that makes sense. Why use 10K ohms and not 1K or 330 ohms?

The resistor value is not critical in this application.

Lower reisitance values draw more current. At 330 Ohms, you are wasting about the same amount of power as it takes to light an LED. I wouldn't go much lower than that. This could be an issue with battery-operated circuits, especially if you have multiple pull-ups.

If the resistance is too low, the resistor can overheat, or it it could draw too much current and damage the switch.

If the resistance is too high, (maybe over 100k) you are likely to pick-up noise (the input could be momentarily pulled-down by noise) and you could get "false" button-presses. Or, the input might not get pulled-up at all.

why r the examples full with the use of external pullups, if there is an internal one?

lax123:
why r the examples full with the use of external pullups, if there is an internal one?

To teach you.

The internal pull-up resistor makes it so much easier. I would need to use this with any pushbutton or switch correct?

@ematson,
Typically one would have this code format

byte input_pin = 3; //

void setup (){
pinMode (input_pin, INPUT);
// or
pinMode (inpput_pin, OUTOUT);
}

thus the software knows that D3 is to be an Input or an Output.

I suppose if had this somewhere
byte INPUT_PULLUP = HIGH;
then what you suggested could work.

@lax123,
Many people can not grasp the concept of a Low being the active state, so pulling the pin Low and connecting to High as active is presented as an option.

@schoolsterz123,
Yes, internal pullup connected is needed for any pushbutton or switch, otherwise the pin floats and intermittent Highs & Lows are returned when the pin is read. This floating can also oscillate, consuming more power. Hence the need in power saving situations to ensure all unused pins are connected to +5 via pullup or to ground. +5 via pullup is safer, as the pin can still be used as output and go Hi or Lo and not damage anything. If connected to Gnd or +5 directly and the pin set to an output, the pin will likely fry if it was commanded Hi when grounded or Lo when connected to +5.

But you can set a pin like

 pinMode(pick_any_pin, INPUT_PULLUP);

without any variables. It says so here

and I have done so many times

Okay, that's a new one on me.

yea it is pretty new