Fundamentally not understanding button wiring

Hi All - Just getting into Arduino and hoping someone can answer this in layman's terms!

I am struggling to understand the logic behind the wiring in the simple button diagram below. Can anyone explain why the connection from GND to resistor to Pin 2 is necessary?

My feeble brain would have thought that if you removed this connection it would still work i.e. when the button is open the voltage to pin 2 is zero (because essentially it is not connected to anything) and when the button is closed the voltage to pin 2 is 5v. This would give you the two input states you need to read (LOW and HIGH). So why is the connection from GND/Resistor to Pin 2 required? And what is the purpose of the resistor?

I'm sure the answer is staring me in the face but I am really struggling to get it! Thanks for any help :slight_smile:

Hi, the reason for the resistor is as you have deduced, to keep the input pin at GND when the switch is not closed.
If the resistor is removed then it will not necessarily be at GND potential due to its high input impedance and susceptibility to picking up noise, it could produce a potential that represents a HIGH or a LOW.
When the switch is closed then the pin will be HIGH, some current will flow through the resistor, but not enough to cause any concern.

Tom.. :slight_smile:

What you see in the diagram, is called a pull down resistor. If the resistor wasn't there, the button wire would have been a loose wire. Loose wires are confusing in electronics and can cause undefined values (suddenly jumping hi-low or spikes). The resistor makes sure, that if the button is not pressed, it will make contact to ground. On button press, the 5v wire takes precedence over de resistor and give a full signal on the pin. The resistor is required, otherwise you cause a short.

scifictus:
My feeble brain would have thought that if you removed this connection it would still work i.e. when the button is open the voltage to pin 2 is zero (because essentially it is not connected to anything)

Why would it be zero if it's not connected to anything?

Wires that aren't connected to anything usually have their voltage set by the surrounding radio waves - ie. fairly random.

OTOH that diagram isn't the way to do it. No external resistor is needed, AVR chips have built-in pullup resistors.

Thanks both - I think i get it - simply not having a connection to an input pin is not a reliable 'LOW' and so the circuit from GND>Resistor>Pin2 provides this.

One last question then - if GND is not supplying any current, and Pin2 is not supplying current (as it is an input pin) then exactly what current is the resistor resisting when the button is open?

scifictus:
One last question then - if GND is not supplying any current, and Pin2 is not supplying current (as it is an input pin) then exactly what current is the resistor resisting when the button is open?

None. It's just shunting the radio waves and noise in the wire away to GND.

Ideally we'd use a piece of wire for this, but we can't. If it was a piece of wire, GND would connect to +5V when you press the switch. That's not good...so we use a resistor instead.

So the resistor is not there as part of the 'open' set up (i.e. it is not doing anything when the button is open) but it is there so that when the button is closed the 5V current isnt sent to GND? Is that right? If 5V is connected to GND without a resistor is that a 'short circuit'?

scifictus:
So the resistor is not there as part of the 'open' set up (i.e. it is not doing anything when the button is open)

No. It's making sure the voltage at the pin stays very close to GND.

scifictus:
If 5V is connected to GND without a resistor is that a 'short circuit'?

Yes.

So - if I can just confirm my understanding - the resistor is performing two roles:

  • When the button is open: It is 'cleaning' the GND signal from static interference etc. that may give false readings, ensuring a reliable LOW input to Pin2
  • When the button is closed: It is ensuring that the 5V current doesn't go to GND which would cause a short circuit.
  • If the resistor wasn't there, and Pin2 was connected directly to GND, this wouldn't cause an issue when the button is open as being connected to GND should ensure pin2 gets a clean LOW signal (in this case the resistor is more of a fail-safe), but it would cause a problem when the button was closed as it would allow the 5v to get to GND and cause a short circuit.

Do I understand corretly now?

scifictus:

  • When the button is open: It is 'cleaning' the GND signal from static interference etc. that may give false readings, ensuring a reliable LOW input to Pin2

There is no "GND signal", there's just a piece of metal sticking out from the chip. Adding a piece wire to it creates a radio antenna.

The resistor is what creates the "GND signal".

Great - thanks...I think I am getting it...

So i assume then a wiring 'constant' is that whenever you connect to GND you add a resistor so that you negate the 'radio antenna' effects of that connection - in other words - you cant have a clean LOW signal from a wire connected to GND unless you also have a resistor on it?

scifictus:
Great - thanks…I think I am getting it…

So i assume then a wiring ‘constant’ is that whenever you connect to GND you add a resistor so that you negate the ‘radio antenna’ effects of that connection - in other words - you cant have a clean LOW signal from a wire connected to GND unless you also have a resistor on it?

Yes.

Almost … normally we connect the pin to +5V with thea resistor then use the switch to connect it to GND. There’s even a resistor inside the chip that you can use for this - use “INPUT_PULLUP” in your pinMode() functions. See: http://arduino.cc/en/Reference/PinMode

(I’m sure there’s a good electronics-engineer reason for using pullup instead of pulldown but I don’t know what it is).

Okaaaaaay....erm....I am not sure I really understand the difference between pull-up and pull-down resistors...perhaps I need to read more of my 'Dummies' book first!

In the alterntaive wiring scenario you describe (still using the standard resistor) I don't get how it would work as presumably if the resistor is between the input pin and the 5V then there would never be a current reaching the pin - so the pin would read LOW regardless of the button state? Or is this where the value of the resistor comes into play i.e. if it only resists a small amount then the pin would still read the remaining voltage as HIGH?

PS - really appreciate your patience here - already making a big difference to my understanding!

The difference between pullup and pull-down resistors is what state the wire tends toward when it is not being acted upon by the input. With a pullup resistor, the resistor is between the switch and 5v, and the wire tends towards 5v. Closing the switch completes the path to ground and the voltage drops to zero. You detect that the switch is closed by looking for 0v on the input pin. With a pull-down resistor everything is reversed.

With the Arduino, the simplest thing to do is to make your switch/button inputs INPUT_PULLUP. This will cause the Arduino to use an internal pullup resistor, which saves you wiring. So, basically, wire the circuit like this: input pin -> switch -> ground. Then:

pinMode(pinNum, INPUT_PULLUP);

if (digitalRead(pinNum) == LOW) {
  // button is pressed
}

fungus:
Why would it be zero if it's not connected to anything?

This is an incredibly unintuitive concept to the uninitiated.

scifictus:
In the alterntaive wiring scenario you describe (still using the standard resistor) I don't get how it would work as presumably if the resistor is between the input pin and the 5V then there would never be a current reaching the pin - so the pin would read LOW regardless of the button state? Or is this where the value of the resistor comes into play i.e. if it only resists a small amount then the pin would still read the remaining voltage as HIGH?

The pin will normally read HIGH with a pullup resistor. It reads LOW when you connect it to ground with a switch.

I was thinking about pullup vs. pulldown resistors and I think I know why the reason they use pullups in the AVR chip.

Imagine you connect your Arduino to another device/chip which uses a different voltage for “HIGH” (eg. 3.3V).

The other chip has no way of applying +5V to an Arduino pin, but it can apply GND.

Using pullup instead of pulldown makes everything much more universal - everything has GND, not everything has 5V.

And the pullup way provides a simple way of remembering which way's which, as was explained to me somewhere here by someone sometime...

Think of the switch as a momentary push-button.... when the switch button is not pushed, it (the button itself) is up or "high" and so is the connection; push the button in, it (the button) is down or "low" and so is the connection. I think that is amazingly cunning....

fungus:
Imagine you connect your Arduino to another device/chip which uses a different voltage for “HIGH” (eg. 3.3V).
The other chip has no way of applying +5V to an Arduino pin, but it can apply GND.

A chip running at 5v logic doesn’t require full 5v to read an input as “high”. Typically, something like > 2.5 volts will be read as high and < 0.8 volts will be read as low. But the issue you’re talking about, where two chips run at different logic levels, can easily be handled by doing level shifts between them. A 3.3v to 5v level shifter is a common IC.

But you’re kind of touching one one of the factors that goes into choosing pullup vs. pulldown resistors, and that’s a chip’s ability to sink vs. source current. With a pullup resistor, the chip must source current to ground when the button is pressed. With a pulldown resistor, the chip must sink current from the Vcc rail when the button is pressed. Some chips are not symmetrical in their ability to sink vs. source current, so if you had a chip that could sink a lot of current, but not source much, you would want to use pulldown resistors.

I struggled with this a lot at the start as well, particularly the "0" = "On" whereas "1" = "Off" part.

The "why pullup rather than pulldown" question always puzzled me too. I've read many different explanations but the most common one as far as I can tell, and the one that makes most sense to me is that, with modern electronics there is little to no technical advantage to using one over the other. However with older electronics it was cheaper and/or easier to implement common logic circuits with zero = on. While no longer applicable it has remained as a common convention, one I now use as well.

One other thing I'd note is that at the start I did think the pull-down resistor was a bit of a perfectionist, tick-box exercise. After all, what's the chance of some stray EMF creating enough current to push a pin over its high logic threshold??? I very quickly learned the error of my ways and to listen to the people who were a lot more experienced than me :slight_smile: