Monday, August 19, 2013

Python Matrix Keypad Package

I packaged up the Matrix Keypad code into a downloadable and installable PyPI package. Let me know if you have any issues but it seems download and install well with PIP and once you make the symlinks to the adafruit code you should be good to go.

Also if there is anyone that has a Beagle Bone that can help me port this over to it aswell I would appreciate it. I've had a few requests but I don't own one yet to do the work on. (unless someone ones to send me one :) )


Python Library for Matrix Keypads. Written and tested on a Model B Raspberry Pi. Supports both a 3x4 and 4x4 keypad included
Current Version:
Project Page:Project_Page
PyPI page:PyPI_Page



If the I2C Port expander MCP23017 or MCP23008 is being used, the Adafruit Python library for I2C and the MCP will need to be installed.
You can clone the whole library like so:
git clone
or the two files needed can be pulled out, &


You can use the source from just downloading the files or Install it as a library via PIP:
pip install matrix_keypad
After the install you will need to create links to the Adafruit I2C and MCP230xx code since they are not installed as packages.:
sudo ln -s [path to Adafruit python cod]/AdafruitMCP230xx/*.py /usr/local/lib/python2.7/dist-packages/matrix_keypad
Note: you will have to change the part in the brackets and maybe the path to the where the matrix keypad package is

Files Included



See the demo scripts included to see this all in action.
To call the library select which one you intend to use and use the correct line:
from matrix_keypad import MCP230xx
from matrix_keypad import RPi_GPIO
Then initialize and give the library a short name so it is easier to reference later. For the MCP version:
kp = MCP230xx.keypad(address = 0x21, num_gpios = 8, columnCount = 4
The variables here are the I2C address, then if you are using the MCP23017 or MCP23008 you have to put the number of GPIO pin avaialable (default is 8), Then the "columnCount" is 3 for the 4x3 keypads and 4 for the 4x4 keypads.
For the standard GPIO version you only have to reference the 'column count if you want to change it to the 4x4, it defaults at the 3x4:
kp = RPi_GPIO.keypad(ColumnCount = 4)
It is possible to just check to see if a digit is currently pressed.:
checkKeypad = kp.getKey()
Or a simple function to call the keypad library and loop through it waiting for a digit press
def digit():
    # Loop while waiting for a keypress
    digitPressed = None
    while digitPressed == None:
        digitPressed = kp.getKey()
    return digitPressed

Version History

Initial Scripts
Initial package build
Initial package build and push to PyPI
Updating the to demo selecting the 4x4 keypad
Moved Version Log in README
Updated README Links
Updated References to include the PiLarm code as the inspiration for the "" code
Updates to the code in both main libs to fix some indenting and other issues from coping the code from blogger to a text file.
Updates to the keypad picking section for the constants to make it actually work
Fixes to more indenting issues. :(
Updated main libs and the demo code.
Added install directions to handle the links to the adafruit code
Updated and to reflect new package name.
Updated README as well

Code References

Column and Row scanning adapted from Bandono's matrixQPI which is wiringPi based. is based on some work that Jeff Highsmith had done in making his PiLarm that was featured on Make.


  1. Do you have any instructions on how to get this to work on a recent version ? It seems the library has changed structure completely :-(


  2. Seriously plenty of awesome advice!
    Appreciate it! An abundance of posts.

    Also visit my site : 대구오피


  3. The the very next time I read a blog, Hopefully which it doesnt disappoint me around brussels.
    What i’m saying is, It was my choice to read, but I just thought youd have some thing interesting to convey.
    All I hear can be a couple of whining about something you could fix in the event you werent too busy in search of attention.

    Try to check my webpage - 휴게텔

  4. Great article I enjoy reading your thoughts. More power to your website. Please visit mine too 온라인카지노

  5. This is really interesting, You’re a very skilled blogger. 카지노사이트존