Why do I need a pull down resistor in the button example?

This question is probably extremely basic, but I just can't figure it out after reading a bunch of tutorials and explanation pages about it.

|500x428

If I understand the schema above correctly, the circuit can be in one of these two states:

1: Button is pressed, input pin reads a direct 5V from the Vcc and reads HIGH. 2: Button is not pressed, input pin reads 0V from ground(if it's not connected to the ground, the pin will read random noise from the environment?)

The second state confuses me a bit(probably because I don't quite understand the concept of ground). Why do we need a resistor between the ground and the second leg of the button? ground always reads 0V right? What is the purpose of the resistor?

(if it's not connected to the ground, the pin will read random noise from the environment?)

that's it ! if there were no link between pin and 0V (GND here), the input would be floating when button is not pressed, because of the very high input impedance. The resistor gives this link needed to make sure the input is at 0V in this case. A wire would not allow the pin to be HIGH when button pressed, as it would make a short between 5V and 0V

If you remove the resistor, when the switch is closed then you totaly short +5v to the 0V, so blowing something.

The resistor is there to prevent the short by limiting the current flowing; typically 1k to 10k in value.

That is called the Pull Down, you can also have the reverse, where the resistor is to the +5v rail, which is called Pulled Up.

Explained here http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

you might already know this but the input pins already have built in pull-up resistors which can be activated by adding the word PULLUP into your pinMode statement. Like this: -

pinMode("pin", INPUT_PULLUP);

as opposed to just: -

pinMode("pin", INPUT);

It just saves you the time taken to solder in a resistor and the little amount of money you would be spending on it.

In answer to your actual question tho... when the switch is open the resistor holds the voltage towards ground (which in this case is 0 Volts). When the switch is then closed the path towards 5 volts has less resistance than the path towards ground so the current is forced to flow in that direction instead. This is because electrons always flow through the path with the least resistance. without the resistor the reading would be 'floating'. Meaning it would be constantly fluctuating from 0 to 1 and back again. The resistor forces the reading to take a steady state.

By the way... the ground on your arduino is essentially just your 0 Volts or negative pole

Hi,

Why do we need a resistor between the ground and the second leg of the button?

Its not just between ground and the second leg of the button, it is between ground and the Input, that is what you have to look at.

Tom... :)

liamorourke: electrons always flow through the path with the least resistance.

So if I have a 1K and a 10K resistor in parallel, all the electrons will go through the 1K resistor? He he he. :)

liamorourke: electrons always flow through the path with the least resistance.

That's not true: current flows through all paths available to it, apportioned according to the resistances of each. If that was not the case, parallel circuits would have no meaning, since all the current would be taking only the easiest path and the other legs would be empty.

alnath: that's it ! if there were no link between pin and 0V (GND here), the input would be floating when button is not pressed, because of the very high input impedance. The resistor gives this link needed to make sure the input is at 0V in this case.

What do you mean with "link"? Because there is a physical link between the pin and GND without the resistor?

A wire would not allow the pin to be HIGH when button pressed, as it would make a short between 5V and 0V

So the only purpose of the resistor is to not short the arduino when the button is pressed? because the resistor makes sure that most of the current goes to the input pin and not the ground?

E=IR, Ohm's law. With the switch open, I=0. The difference between the resistor terminals is then E=0*R=0. So if one side of the resistor is connected to 0V, the voltage on the other side will be 0V.

So the resistor makes sure that there is 0V at the input when nothing else is connected.

ObiTewodros:
So the only purpose of the resistor is to not short the arduino when the button is pressed?

Yup… look at the pic in your opening post. Imagine the resistor is a piece of wire and close the switch. Direct short from ground top left past the black dot round to 5V bottom centre.

But as pointed out, by far the easiest way to do this is to use the built-in pullups. That reverses the logic of course. With the pulldown, button press = high; with pullup, button press= low, so you need to cater for that in the code. But the built-in resistor saves a lot of farting around with an external one…

aarg: So the resistor makes sure that there is 0V at the input when nothing else is connected.

Yeah but a bit of wire would do that too. Reason it's a resistor and not a wire, is to prevent a short-circuit between 5V and 0V when the switch is closed.

ObiTewodros: What do you mean with "link"? Because there is a physical link between the pin and GND without the resistor?

no, there is no physical link between the pin and GND without a resistor, the resistor you put creates this link .

JimboZA: Yeah but a bit of wire would do that too. Reason it's a resistor and not a wire, is to prevent a short-circuit between 5V and 0V when the switch is closed.

Well, again I cite Ohm, and I realize you know, so this is directed at the OP. In that case, with the switch closed, E=IR means 5V=I*0.001. Rearrange for I, 5V/0.001 = 5000 amperes. :) But of course the supply can't produce 5000A, so it shuts down.

liamorourke: you might already know this but the input pins already have built in pull-up resistors which can be activated by adding the word PULLUP into your pinMode statement. Like this: -

pinMode("pin", INPUT_PULLUP);

as opposed to just: -

pinMode("pin", INPUT);

It just saves you the time taken to solder in a resistor and the little amount of money you would be spending on it.

In answer to your actual question tho... when the switch is open the resistor holds the voltage towards ground (which in this case is 0 Volts). When the switch is then closed the path towards 5 volts has less resistance than the path towards ground so the current is forced to flow in that direction instead. This is because electrons always flow through the path with the least resistance. without the resistor the reading would be 'floating'. Meaning it would be constantly fluctuating from 0 to 1 and back again. The resistor forces the reading to take a steady state.

By the way... the ground on your arduino is essentially just your 0 Volts or negative pole

I did read some articles mentioning the built in pull-up resistor, I will try that. I can also use breadboard to avoid soldering the resistor right? Because that's what I did and it seems to work :P

At this stage and even later, breadboard is your friend! Saves a lot of time.

(As long as you know how the connectors inside work... that confuses people sometimes .....)

yeah u could use a breadboard as well.

It depends on whether you want what your building to be permanent or not. If you are just experimenting then breadboard is fine but if you're building something which you want to keep then you should try perfboard. That would of course involve soldering but it will be solid then.

Personally i'd just go for the internal pull-ups though. It'll save you loads of time.

So the only purpose of the resistor is to not short the arduino when the button is pressed? because the resistor makes sure that most of the current goes to the input pin and not the ground?

The resistance seen by an input is so high that almost no current flows down it. Where as the path to ground is always a low value. The resistor makes sure the power supply is not shorted out.

The example assumes that you need to see a HIGH at the port when the button in pressed. In order to do this with a N.O. switch, it is necessary to have a pull up when the switch is closed. The resistor is to pull the input low when the switch is open. As was mentioned, one can use the internal pullup resistor but then the state of the pin would be LOW when closed and HIGH when opened. The switch needs to have ground at one end, rather than +5V. This is thought to confuse beginner programmers but is a trivial thing to fix in the software. I believe that the example only confuses things more and is generally poor design, even if you choose to use an external resistor. Running +5V to, say a front panel, is an additional hazard in the wiring. Keeping everything at ground or isolated by a resistor makes better sense. Dwight