Resistor: why?

Hi everyone,

I am reading the book "Getting started with Arduino". One of the first examples is to connect a pushbutton to the Arduino board. So there is a cable from 5V to the pushbutton; from the pushbutton to the digital input on the board and from the pushbutton via a resistor to the ground. Why is the resistor needed?

I don't understand why you need to go from the pushbutton to the ground AND to the digital input on the board?

It is a pulldown resistor. Without the resistor, when the pushbutton is released the arduino input pin is disconnected, leaving it free to pick up noise and return unexpected values. This resistor keeps the value at 0 when the pushbutton is released, but when it is pressed, the arduino pin goes to 1 since it is getting voltage from 5v

The world is not a perfect place. If you just connect an input pin to one end of a pushbutton you can't be 100% sure of its state, because electrically it's not connected to anything. Static electricity, line noise, even cosmic radiation will cause stray readings.

You can see this yourself by writing a small program that just loops on "Serial.println(digitalRead(INPUT_PIN))" with nothing connected. It will randomly print 1s and 0s; for extra fun try touching the pin and you'll see even your body's capacitance will affect the readings.

So, you tie the input pin to ground. This way you're guaranteed consistent readings of 0 when the button is not pressed.

The resistor is to limit the current flow when the button is pressed. Look at your schematic and see what would happen with no resistor. When you press the button you'll be connecting +5V directly to ground, shorting out your circuit. Probably not what you want...

Good luck!

PS Google something like "how does a pullup resistor work" for much better explanations etc.

If you measured the pin with the switch open, it would not be at 5v... but it won't necessarily be at 0v either- pins "float" at an undetermined voltage if not forced to 5 or 0v. So you need to connect the pin to ground when the switch is open.

BUT if you just put the other side of the switch directto 0v, when you closed the switch you would short circuit the 5v straight to ground and fry the board. So the resistor provides a load in that circuit to reduce the current that would other wise be there from the short.

Keep in mind that there is a way to wire a simple push button switch to a input pin without requiring a external resistor, but still staying away from a 'floating input pin' condition. You wire one side of the switch to ground (not +5vdc) and the other side of the switch to the digital input pin. Then you activate the optional software enabled internal pull-up resistor for that digital pin number. Then in your sketch logic when the switch is pressed it will return a 0 and when released will return a 1.

Lefty

Signal from a pushbutton without the resistor:
YouTube Video: YouTube

Signal from a pushbutton with a resistor:
YouTube Video: YouTube

You do not need to be an an engineer to understand that the first one is "bad" and the second one is "good".