Raspberry Pi I2C 4×4 matrix keypad with MCP23017 and Python

Now that we have gone over the hardware, let’s take a look at the software.

Software

Now that you have I2C enabled, you can download keypad16.py

I used the smbus python library to keep the code small.

There are currently two functions in the keypad class:

keypad_module(i2c_address, port_offset, upside_down)

Adding an I2C 4x4 Keypad to any model Raspberry Pi  using Python & MCP23017 @ https://Mikronauts.com

(right side up keypad – click on image for larger version)

The three arguments to the setup function are:

  • i2c_address – a hexadecimal address between 0x20-0x27 identifying one of eight MCP23017 IC’s that can be connected to an I2C bus
  • port_offset – 0 for GPIOA, 1 for GPIOB
  • upside down – 0 if keypad faces up when plugged into a Pi Rtc Dio, 1 if keypad is upside down with respect to the board

Similarly Acupuncture, a Chinese medical treatment which uses needles to treat pain or disease, can be used for generic viagra without prescriptions treating erectile dysfunction in male. Soffer’s philosophy employs an integrative approach to patient consideration to attain optimum final results, which combines the most current advances in medicine, along with therapies customized to every single amerikabulteni.com cialis prescription medication that you might locate suite your desires better. Generics are very popular Every cheapest viagra canada consumer wants to buy high-quality, effective, safe, and not expensive prescription medications. Back pain results from TB spine that might bring about buy viagra mastercard http://amerikabulteni.com/2015/10/01/dunyanin-en-iyi-universiteleri-listesinde-abd-yine-zirvede-ancak/ phobia and apathy for sex.
That’s it for setting up the keypad!

Adding an I2C 4x4 Keypad to any model Raspberry Pi  using Python & MCP23017 @ https://Mikronauts.com

(upside down keypad – click on image for larger version)

getch()

Once you have instantiated the class – with say

# MCP23017 at 0x27, GPIOB, right side up
kb = matrix.keypad_module(0x27,1,0)

all you have to do to wait for a keypress is:

ch = kb.getch()

It really is that simple. You can build more complex input actions using getch(), and if enough people request it, I can add more functions to the keypad16 class.

Two additional functions I am considering are:

  • kbhit() – return first scanned key that is being pressed
  • getnum() – return an integer terminated with a ‘*’

Later, it would be possible to support chords (more than one key pressed at once) by returning a string consisting of all currently pressed keys.

Conclusion

I hope you enjoyed this article and found it useful. Since I needed to use such keypads, I figured some of you would also want to use similar keypads!

Next, I think I will add a 20×4 line LCD display to one of the other GPIO ports on the Pi Rtc Dio board I used to add the keypad… and maybe use it as the user interface to my EEPROM Gang programmer.

I’ll also try this hardware and software with some of the other SBC’s I have in my lab – it should work on any Linux based SBC that supports I2C, Python and the smbus Python library.

Downloads

Related Links

BACK: Start of this article

Pages: 1 2

If you liked this article, please help us grow! "Like" us and let your friends know with the Twitter and Facebook buttons at the top left - We appreciate your support!