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: http://amzn.to/2DyyYYa

Amazon Germany : http://amzn.to/2DyBtd1

Raspberry Pi

Banggood: https://goo.gl/wrcUS3

Amazon US: http://amzn.to/2Dw3oKT

Amazon Germany : http://amzn.to/2FUW5Sj

Female to Female Jumper Cable Wires 20cm

Banggood: https://goo.gl/E93MCh

Amazon US: http://amzn.to/2FISFmg

Amazon Germany: http://amzn.to/2pkj27N

Keypad

The key pad I used is shown below.

And I was curious to know whats inside which I have shown below.

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.

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.

Pin Mapping

Key pad = Rpi pin 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 .

After connecting them together it looks like this.

Hardware connection

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

from pad4pi import rpi_gpio import time def processKey(key): print(key) # Setup Keypad KEYPAD = [ ["1","2","3"], ["4","5","6"], ["7","8","9"], ["*","0","#"] ] ROW_PINS = [11,23,24,25] # BCM numbering COL_PINS = [17,27,22] # BCM numbering factory = rpi_gpio.KeypadFactory() keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS) keypad.registerKeyPressHandler(processKey) while 1: time.sleep(1) keypad.cleanup()

You can also check out the sources from here https://github.com/codelectron/codelectron_projects/blob/master/Rpi/keypad/keypad.py

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 also published at https://www.instructables.com/id/How-to-Interface-Keypad-With-Raspberry-Pi/