2

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?

Xbox One
  • 139
  • 9

3 Answers3

6

You can connect all the grounds to one pin. There is no need to have a separate ground for each GPIO.

joan
  • 71,852
  • 5
  • 76
  • 108
1

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.

1

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.