Introduction: How to Interface Keypad With Raspberry Pi

I wanted to make a sound board using Raspberry Pi and was looking for a solution to have multiple buttons. Some of the options were Arcade button and USB number pad until I scrambled my mini garage and found out that I have a keypad. I bought it long time ago for another project and now it seems like perfectly matching the need.

Now there are plenty of keypad available in the market, the specification that I am using is a 12 keys 7 pin keypad. Before going further in to the technical details of the keypad below are the my affiliate links for the hardware required.

Key Pad

Banggood: https://goo.gl/4QN1CT

Amazon US: https://goo.gl/4QN1CT

Amazon Germany : https://goo.gl/4QN1CT

Raspberry Pi

Banggood: https://goo.gl/4QN1CT

Amazon US: https://goo.gl/4QN1CT

Amazon Germany : https://goo.gl/4QN1CT

Female to Female Jumper Cable Wires 20cm

Banggood: https://goo.gl/4QN1CT

Amazon US: https://goo.gl/4QN1CT

Amazon Germany: https://goo.gl/4QN1CT

The key pad I used is shown below.

Step 1: Keypad Specs

Basically the key pad is nothing but a interconnection of rows and column where each interconnection is a key. So with 4 cols and 3 rows you get 12 keys. For more clear picture have a look at the below schematics. The key pad is following the same schematics.

If each col and row are connected to GPIO pins then you can detect which key is pressed by going narrowing down the row and column. for example if Col 2 and Row 2 are giving signal then Key ‘5’ is pressed. With the given information lets connect the pins to the raspberry Pi. The Pi that I used is version 1 and it has less number of GPIOs but the pinout of raspberry is partly backward compatible so the pin numbering and the code works fine in all the Pi versions with minor changes .
Now you need to connect the following way.

Key pad = Rpipin

Col 1 = 17

Col 2 = 27

Col 3 = 22

Row 1 = 11

Row 2 = 23

Row 3 = 24

Row 4 = 25

The pin number of the Rpi represents the BCM number you have a look at the pin reference. There is also an interesting website which provides Rpi pinout reference http://pinout.xyz .

Step 2: Connection and Code

Now that we have done the connection lets come to the coding part. I am using python for the sound board project so I want to configure that get the keypad working in python.

There are many libraries which are available to interface a keypad with a RasberryPi, I chose this one pad4pi was very handy and easy to start.

Installation of pad4pi

pip install pad4pi

You can also check out the sources from here https://github.com/codelectron/codelectron_projec...
Code Explanation:

ROW_PINS = [11,23,24,25] # BCM numbering

COL_PINS = [17,27,22] # BCM numbering

The main connection between the hardware and the library is the pins and that is defined above.

KEYPAD = [

[“1″,”2″,”3”],

[“4″,”5″,”6”],

[“7″,”8″,”9”],

[“*”,”0″,”#”] ]

The keypad table is just a reference to what you have in your hardware

keypad.factory = rpi_gpio.KeypadFactory()

keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS) keypad.registerKeyPressHandler(processKey)

Here you create a keypad factory where you get a empty factory which you use to create a keypad of that table with the number of rows and columns. Finally register a interrupt handler to be called whenever a key is pressed.

This article is originally published at http://codelectron.com/how-to-interface-keypad-wit...

and shared here at Instructable