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?
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.
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.
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.