I am trying to take input from twelve different buttons with my Raspberry Pi Zero W.
I have enough GPIO Pins for this, but not enough GND pins, so I decided to make some of the pins pulled low by default so that I could also make use of the 3v3 pins.
Even so, I fell two pins short. How could I use twelve buttons?
I considered using an always on GPIO pin as a 3v3 pin. Would this be safe? I read that using a GPIO pin that's off as GND is unsafe, that's why I am asking. If not, is there some HAT or something else I could do?
3 Answers 3
You can connect all the grounds to one pin. There is no need to have a separate ground for each GPIO.
-
-
Wait, so in what scenario may I need more than one GND? I ask because I am wondering why RPi even built in eight GNDs when you only need one.Xbox One– Xbox One2023年04月09日 21:31:46 +00:00Commented Apr 9, 2023 at 21:31
-
They provided the grounds for convenience. It might have been more convenient to provide more 5V and 3V3 power pins instead.joan– joan2023年04月09日 21:52:29 +00:00Commented Apr 9, 2023 at 21:52
-
1@XboxOne Convenience mainly, but a little bit also for signal isolation. For low speed GPIO it doesn't matter at all. But when you use IO ports like SPIO, serial or I2C you usually want to dedicate one GND solely for each, so their high speed switching doesn't interfere with the ground returns of other signals and vice versa. It is not strictly necessary, but generally a good idea because it increases reliability. But the grounds are all ultimately connected together on the RPi PCB.tylisirn– tylisirn2023年04月09日 21:54:10 +00:00Commented Apr 9, 2023 at 21:54
Key question: Do the buttons have to operate totally independently? Or are they all for input by one user or in some other way reasonably certain that only one will be pressed at a time?
If you only need one at a time (think: computer keyboard, telephone keypad) then the general solution to this problem, going back to the first 8-bit computers in the 1970s, is to use a keyboard matrix. For 12 buttons, that is a matrix of 3x4, so 7 GPIO pins instead of 12. 8 GPIO pins gets you 4x4 = 16 buttons, etc.
A quick search found an example of this here.
-
1Oh this is cool, yes only one will be pressed at a time. A keyboard matrix will help me reduce the number of wires I will need, but now that I know that I only need one GND, it doesn't relate to that issue. +1Xbox One– Xbox One2023年04月09日 21:29:25 +00:00Commented Apr 9, 2023 at 21:29
You could use a MCP3008 (MCP3008 and Raspberry Pi Zero) and a single analog input for as many buttons as you want (limited by the commercial values of resitors: https://fritzenlab.net/2024/04/01/analog-input-keypad-with-arduino/ ).
The "secret" I explain in the blog post is to make every button cause a different voltage to the AD converter, so that you can programmatically determine which was pressed.
-
You can also use a sledgehammer to crack a nut but it is overkill.Milliways– Milliways2024年04月26日 06:40:40 +00:00Commented Apr 26, 2024 at 6:40