In most examples I’ve seen of wiring buttons to GPIO inputs, the button is wired so that when closed, a circuit is completed between the pin and ground, producing a low input on the pin. A pull-up resistor is used to default the input value to high. Code on the Pi can then detect low as a button push.
I’ve been connecting the buttons to +3.3v so the signal is high as this seems to make more sense and keeps the code more logical but there must be a reason that most people favour connecting to ground. What are the advantages?
1Note: When I say ‘pull up resistor’ I meant either a physical external resistor or the internal pull-up resistor, configured in code. – howard10 Sep 10 ’13 at 9:15
One of the main reasons why wiring buttons and logic to GND is favoured (and then copied all over the internet) is because of power optimization.
- Pulling a pin LOW with resistor to GND costs 0 watts.
- Pulling a pin HIGH with resistor to +Vcc costs power.
On complex circuits or circuits that rely on batteries this power is very precious.
Other reasons include low EMF generation. On Wireless devices pulling logic high will cause unnecessary cross talk on extremely sensitive RF receivers. On such transceivers there is a GND plane used to filter noise and this is where all logic gets pulled down to. The processor then uses the GND plane to filter switching noises.
4How does pulling up or down with a resistor cost any power? The current is flowing to a very high impedance input on the Pi, which is going to reduce any current down to sub-microAmps, which is microWatts of power. – stefandz Aug 13 ’15 at 16:45
1OK, but just tell me if I am wrong. pulling a pin down, which is
0v = 0wattsusage, but pulling a pin up
>0watts– any kind, micro, milli, nano, doesnt matter. As is mentioned, on battery power every nano watt helps. But as already answered here… on USB it means nothing. Am I mistaken? – Piotr Kula Aug 13 ’15 at 16:48
2Not necessarily true. Digital inputs are high impedance to ground – true. But they are not just resistors. They are input gates, normally to FETs and these gates are non-ideal. They have leakage currents, and these leakage currents can be in either direction, in or out. Hence your pulldown could well consume a microwatt or two, just as much as a pullup could. – stefandz Aug 13 ’15 at 16:52
1If you had to design an embedded device that ran on 2AA batteries and the customer demands it runs for at least 12 months. And you have to pull down/up a GPIO. Which would you use to save the most power. (We talking micro management here) – Piotr Kula Aug 13 ’15 at 16:55
1It wouldn’t make a jot of difference. Since the leakage current can flow in either direction (and likely will) you’re not doing anything right or wrong by your choice of pullup or pulldown in a CMOS system. From ti.com/lit/an/szza036b/szza036b.pdf ” In a static dc state, a CMOS input sinks or sources only a minute amount of leakage current (a few µA).” Sinks or source – it can be either. I am bringing this up only because I believe your answer is misleading – @AdamDavis’ answer below is far more correct. – stefandz Aug 13 ’15 at 16:59
1I don’t think my answer is completely misleading, maybe just lacks expansion, as Adam has done. I admit I may have focuses around the battery aspect to much but from experience I believe what I wrote is more true than misleading. But I am no Electrical Engineer, just a practical hacker. – Piotr Kula Aug 13 ’15 at 17:09
1The power side is certainly incorrect – Adam’s answer explains that it is generally historical, and that having trailing wires with ground on them is safer (in terms of potential equipment damage) than wires with any voltage on them. I am an electronic engineer with experience in exactly the kind of “has to run for a year” scenario you talk about. I promise you I’m not trying to show off or beat you down (you clearly have masses of knowledge on a wide variety of subjects) – but the idea that pulling up or down has a power consumption implication is incorrect. – stefandz Aug 13 ’15 at 17:12
1I had to design a low power device just like that. And all I had to use a conclusion were tests I made during making various prototypes in various configurations. Keeping things pulled down saved power for me. Maybe it was specific to that one IC, maybe environmental, I don’t know. I cant change my answer now. You seem to know stack exchange, and I have learnt not to take down votes personally, especially if justified by a professional opinion. 🙂 – Piotr Kula Aug 13 ’15 at 17:20
1That’s cool – this discussion will be enough to feed the curious. Glad you take it in good faith – I’m new to the site, but keen because it provides a great balance between sharing and learning. VERY interested that pulldowns reduced your power consumption – but that’s a different discussion. – stefandz Aug 13 ’15 at 17:25
5For what it is worth, I did some measurements on this – and pulldown wins (for power consumption) but only just. 10k pullup to 3.3V = 9fW (yes femtowatts) – 10k pulldown to ground = 5fW. This is certainly not even in saving a bit of battery territory. – stefandz Aug 14 ’15 at 15:39
1Its possible that whatever I did had influence on the internal circuit, in some other kind of way, maybe something else. But yes.. those figures generally wont help which you calced, as the battery will discharge faster on its own than this would help save power 🙂 Its a strange situation for me since this got so many upvotes and was accepted…. and yet you just prooved, and I respect your tests, that its not really answering the question correctly, as Adam Davis Did. – Piotr Kula Aug 14 ’15 at 15:49
In the old days of TTL it took far more current to pull a pin low than to pull it high. So a pullup resistor could be higher resistance (and hence less power-wasting) than a pulldown resistor. It doesn’t matter with modern CMOS but old habits die hard.
Connecting the pin to ground with an internal pullup resistor means you use less parts. All you need is a button; don’t need an external resistor to limit current.
Strictly there is no need for a pull-up resistor, the BMC GPIO has internal pull-up resistors which are activated when programmed as an input, although it does no harm.
It is poor practice to connect a GPIO pin directly to either 3V3 or GND. The GPIO is bidirectional, and if programmed as an input this would cause no problems. On the other hand if programmed as an output will cause excessive current to be drawn.
Good (safe) design would use a series resistor ( 1 kΩ ) in series with the pushbutton to limit current. For the reasons made by Adam Davis, it is preferable to connect the pushbutton to ground, and locate the protective resistor close to the GPIO pin.
The only problem is that during boot phases 1-3 these pins are floating (GPU to CPU handover) then on Phase 4 during Linux Kernel boot the PINs are set to the correct state. This can cause serious issues with gates waiting for logic. So it is advisable to pull them down anyway so that the floating period during boot does not make your circuits go mental! – Piotr Kula Sep 11 ’13 at 15:51
There are complex historical reasons why electrical engineers typically pulled inputs high with resistors, and used switches to ground them.
However these reasons are not particularly relevant to the hobby use of the Raspberry Pi. Use whatever makes sense to you.
If you are making a commercial product, or want your design to be slightly better, you’ll choose pull-ups with a grounding switch for the following practical reasons:
- A long ground wire poses less EMI/EMC radiation risk than one connected to power
- Grounding something, and finding a ground point to connect to, is easier than a power line
- If the switch or wiring, typically placed some distance away from the circuit, becomes damaged and either shorts the wire or internal switch parts to the case or the user, no harm is done – it’s all at ground
I don’t think there is a reason to prefer one over the other on the RPi. Most people are probably just copying or porting circuits they have seen elsewhere.
When connecting up the circuit (with wires or PCB) it’s fine to just pick whichever is more convenient and translate it to the right meaning in your software.