I've seen a tutorial here that shows me how to do this:


But what I want to do is like this:

enter image description here

I want to have multiple buttons and distinguish between them. The graphic I painted depicts the desired layout of the buttons and how I propose arranging them and connecting them.

So my question is, is what I'm planning to do possible? And how may I distinguish between first- and second-row buttons in Python?

  • I don't understand what you are trying to achieve. It is not clear how the switches are meant to be connected. Could you explain in words?
    – joan
    May 10, 2019 at 17:01
  • I want to have multiple buttons and distinguish between them. The graphic I painted depicts the desired layout of the buttons and how I propose arranging them and connecting them.
    – user96931
    May 10, 2019 at 17:04
  • The diagram tells me nothing about what you are trying to do. Hopefully it will be clear to others.
    – joan
    May 10, 2019 at 17:05
  • I need to set up a bunch of buttons, have a python loop to scan through all of them and if one is pushed down, call a procedure to handle it.
    – user96931
    May 10, 2019 at 17:30

3 Answers 3


While it is possible to connect multiple buttons to the Pi's GPIO and read them in a python loop the second that second "schematic" will not work. Note that ABCDE and FGHIJ columns are connected for each numbered row. Placing the buttons as shown will make it impossible to distinguish between the two buttons on each line. In this particular case one would want to replicate the setup shown in the first image as posted in the question - placing all buttons on separate rows.

This is how breadboard holes are usually connected (blue lines), follow-up reading at electronicsclub.info. Note that the left and right columns (marked red and black) are all linked together and are usually used as power rails, i.e. connecting GND and VCC. Using them GND could be connected just once to the Pi's GPIO header and then distributed to the buttons at the breadboard.

enter image description here

source, image by John Hewes

  • So I'd need to use a seperate GPIO wire for each button?
    – user96931
    May 10, 2019 at 18:31
  • 2
    @user96931 using this simple approach: yes. There are ways to use less pins but that depends on your use-case which you do not shed too much light on. One way is called multiplexing: see e.g. here: raspberrypi.stackexchange.com/a/62439/19949
    – Ghanima
    May 10, 2019 at 18:39
  • Perfect. I'll try this approach then :)
    – user96931
    May 10, 2019 at 18:42
  • 1
    @user96931 additional reading on the topic of keypad matrices: blog.komar.be/how-to-make-a-keyboard-the-matrix There are some catches, especially if multiple buttons are pressed at once.
    – Ghanima
    May 10, 2019 at 19:11

What you need is a button array.
Example you have two rows of buttons

a A
b B
c C
d D
e E

a and A are connected to the same input GPOP b and B are connected to the same input GPIO and so on

But they way you tell which button is being pressed is to Power all the capital buttons, check the inputs. Remove power from Capital buttons, power the lower case row, check your inputs.

This is done if a fast loop so that you dont miss an input.

enter image description here

Image source https://siminnovations.com/wiki/index.php?title=File:Button_array_schematic.png



How to distinguish multiple GPIO Buttons?


Well, for a small number of buttons, say 8, you can use 8 GPIO pins, each of which entertains one button. But that is a big waste of GPIO pins. For more than 8 buttons, you can use GPIO extenders such as MCP23017, each of which adds 16 more GPIO pins, and greedy you can "easily" (see warning below) use 8 MCP23017s to get 8 * 16 = 128 buttons.

To distinguish 128 buttons in a row is a bit messy. So all these years the EE guys have been using the standard matrix wiring method to make their job easier. A simple example of 16 buttons or keys (actually "button" and "key" are the same thing), wired in 4 rows by 4 columns matrix (you can buy one from eBay for less than one US dollar!)

4x4 keypad

keypad schematic

Now of course you can use 8 Rpi/MPU GPIO pins to scan a 4 x 4 = 16 key keypad. But of course the clever EE guys would not waste 8 GPIO pins, so they use MCP23008 with 8 GPIO pins, so not bothering Rpi which would do other chores and only when interrupted by MCP23008, then diverted to check which key or keys are pressed. If you want 64 keys, then you can use MCP23107 with 16 GPIO pins to scan a 8 x 8 = 64 key keypad.

For "humble" newbies, I would recommend to start with only 4 Rpi GPIO pins, to scan a corner of the cheapy 4 by 4 keypad (yes, you don't need to scan all 16 pins, to make things newbie friendly)

To give you a very rough idea of the scanning thing, the flowchart below should be helpful.

scan flowchart

The flowchart shows a general scanning method for both using Rpi GPIO pins or MCP23008 pins.

If you are interested to learn all the details, including project background etc, you might like to read the following posts.

Connecting 32 magnetic sensors

More Digital Inputs

If you are a maker and picture guy, and prefer pictures than boring text, then let me show you a couple more pictures, ...

How to distinguish keys

distinguish keys

How to test one keypad key

How to check one key

How to test multiple keys pressed at the same time

One good thing about use matrix keypad is that individual key interrupt pins can be "logically" connected so that when one or more keys are pressed, only a combined single interrupt goes to Rpi (which is not wasting time busy looping, but doing other useful non keypad related thing) which can first take note of the interrupt event, then later when free, leisurely check which keys are pressed. A handy hardware troubleshooting tools is also displayed below.

led array

Warning to newbies - DON'T jump start with MCP23017!

For those brave, proud newbies who (almost always fail eventually! :)) want to jump start with a 64 key keypad using MCP23017, I would like give a severe warning - MCP23017 has a very steep learning curve, and is absolutely not for the faint of hearts. Just in case some curious newbie wants to have a look of the deceptively easy circuit, here is an oversimplified schematic.

Good luck to your multiple GPIO button project! :)

mcp23017 keypad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.