Multiple push button on one GPIO pin

General GBZ-related chat goes here. Share ideas, tips and tricks, or ask questions that don't fit into the hardware/software help forums.
Post Reply
User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Multiple push button on one GPIO pin

Post by 1461748123 » Thu Jun 02, 2016 1:53 am

Hello guys! I'm now facing a problem that I only have 4 gpio pins left and I've to wire them up to 12 buttons..
Any ideas on how to accomplish that? :) I really don't want to use teensy since it will take one of my usb port..

SP33
Posts: 25
Joined: Tue May 17, 2016 5:41 pm
Has thanked: 3 times
Been thanked: 24 times

Re: Multiple push button on one GPIO pin

Post by SP33 » Thu Jun 02, 2016 2:24 am

Only two things I can think of are using something similar a keypad setup where you have rows and columns and each button is detected according to whether a row and column is shorted.. but you would still need more pins, the other option is using an MCP23017 (16 inputs) or MCP23008 (8 inputs) which hook up to SDA, SCL, Power and Ground on the pi which will give you 16 or 8 extra inputs. I am using this approach just because I did not want the inputs all wired to the pi so I made a separate board with an MCP23017 which connects to the buttons. I will probably make a tutorial showing how I did everything sometime.

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: Multiple push button on one GPIO pin

Post by 1461748123 » Thu Jun 02, 2016 2:31 am

SP33 wrote:Only two things I can think of are using something similar a keypad setup where you have rows and columns and each button is detected according to whether a row and column is shorted.. but you would still need more pins, the other option is using an MCP23017 (16 inputs) or MCP23008 (8 inputs) which hook up to SDA, SCL, Power and Ground on the pi which will give you 16 or 8 extra inputs. I am using this approach just because I did not want the inputs all wired to the pi so I made a separate board with an MCP23017 which connects to the buttons. I will probably make a tutorial showing how I did everything sometime.
Thanks for the reply! If I can use SAD and SCL it will be prefect, but I can't... it was taken by DPI already. The only thing I left is 6 BCM pins, and 2 of them have been use for pwm audio output, so I'm only left with 4 normal BCM pins.. :(

SP33
Posts: 25
Joined: Tue May 17, 2016 5:41 pm
Has thanked: 3 times
Been thanked: 24 times

Re: Multiple push button on one GPIO pin

Post by SP33 » Thu Jun 02, 2016 2:38 am

1461748123 wrote:Thanks for the reply! If I can use SAD and SCL it will be prefect, but I can't... it was taken by DPI already. The only thing I left is 6 BCM pins, and 2 of them have been use for pwm audio output, so I'm only left with 4 normal BCM pins.. :(
I don't recall what DPI is but I believe you can connect multiple i2c interfaces, they will each get their own address. So you would use the MCP23017 and connect your DPI device (assuming it's an i2c device) to the SDA and SCL lines on the MCP23017 (or vice versa) instead of the pi and they will both be recognized separately.

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: Multiple push button on one GPIO pin

Post by 1461748123 » Thu Jun 02, 2016 2:52 am

SP33 wrote:
1461748123 wrote:Thanks for the reply! If I can use SAD and SCL it will be prefect, but I can't... it was taken by DPI already. The only thing I left is 6 BCM pins, and 2 of them have been use for pwm audio output, so I'm only left with 4 normal BCM pins.. :(
I don't recall what DPI is but I believe you can connect multiple i2c interfaces, they will each get their own address. So you would use the MCP23017 and connect your DPI device (assuming it's an i2c device) to the SDA and SCL lines on the MCP23017 (or vice versa) instead of the pi and they will both be recognized separately.
DPI is for vga output, I use them to directly connect to a tft screen.
The problem is, when I use the i2c pins for DPI, I've to switch them to alternative function, in this case v-Sync and h-sync, which means they do not function as a i2c pins anymore.. :(
Have to find another solution...

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: Multiple push button on one GPIO pin

Post by 1461748123 » Thu Jun 02, 2016 2:50 pm

kite wrote:Take a look at this project https://hackaday.io/project/10207-gameg ... done-right which has 12x buttons on 4x GPIOs without the need for any ICs.. You may have to join the Discord chat and ask for the code that makes it possible, as I can't see it shared on the project page..

Another way is a multiplexer, or a number of shift registers and just clock through all the pins
Thanks! That helps a lot! I know how to do that now :D

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: Multiple push button on one GPIO pin

Post by 1461748123 » Fri Jun 03, 2016 12:08 am

kite wrote:Take a look at this project https://hackaday.io/project/10207-gameg ... done-right which has 12x buttons on 4x GPIOs without the need for any ICs.. You may have to join the Discord chat and ask for the code that makes it possible, as I can't see it shared on the project page..

Another way is a multiplexer, or a number of shift registers and just clock through all the pins
After checking their wiring, I found out that they didn't connect any button to ground, which means the input pin will take 3.3v directly. I'm not sure but can gpio pins take up to 3.3v? I thought it will just kill it.

User avatar
1461748123
Posts: 143
Joined: Wed May 04, 2016 8:36 pm
Has thanked: 9 times
Been thanked: 17 times

Re: Multiple push button on one GPIO pin

Post by 1461748123 » Fri Jun 03, 2016 10:20 am

kite wrote:
1461748123 wrote:After checking their wiring, I found out that they didn't connect any button to ground, which means the input pin will take 3.3v directly. I'm not sure but can gpio pins take up to 3.3v? I thought it will just kill it.
3.3v won't kill it, the Pi IO is 3.3v logic so it's perfect.. however the schematic doesn't connect any button to 3.3v..

That schematic WON'T work with a 'common ground' PCB.. basically that schematic works that BTN1 is set to INPUT PULLUP, and then BTN2-4 are set to HIGH.. then BTN2 is set to low (then BTN1 is read, if HIGH then SW1 isn't pressed, if LOW then SW1 IS pressed).. then repeated for BTN3 and BTN4 (toggling low and doing a read on BTN1, to check SW2 and SW3).. then it will set BTN2 to INPUT PULLUP.. and repeat.. then again for BTN3 and then BTN4..

It is a way to read 12x buttons with 4x GPIO, so it isn't simple.. hence asking for the code as it is ALL in the code.. but as you can see there is a hardware setup to do.. to be honest it is sounding a lot simpler to use a Teensy :P
Thanks! After talking to the author I'm pretty sure what to do at this point. The diagram he made have some slight mistake, it should have diode for each button to prevent ghosting (which is a problem that occurs when you have multiple buttons pressed down). I will make a separate post at the hardware section for further discussion :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest