Saturday, November 10, 2012

Raspberry Pi with 16-bit I2C IO-expander MCP23017

I want to add some physical buttons to my Raspberry Pi. The most easy (and cheap) way is by just adding some push-buttons.

Since the GPIO ports are quite limited I purchased a MCP23017. This is a so-called 16-bits I/O expander which can be used over I2C. This gives me 16 extra inputs/outputs by using only two wires (four if you count VCC and GND), they are adressable so you can chain a lot of them and have even more ports.

I don't have any prior experience wiring and using those. Luckily there are a lot of people already using it, also with their Pi:

Having the datasheet open also helps a lot, some articles had the wiring a little wrong (SCL/SDA mixed up). There are a lot of other I/O expanders, but most of them are 8-bit. Also, that I saw other people using this on 3.3V was one of the reasons I bought this one.

My goal is to get this working with the linux gpio-keys module, so the pushbuttons act like real keys and work directly with all programs (e.g. XBMC). Unfortunately I can't seem to find a lot of information to make this work. Since I also want to make sure I understand what's happing I'm first trying to get the I/O-expander to do what I want using Python.

Really helpful Python-libraries I found are: (For the RPi GPIO's in general)

Also, this website which explains using the MCP23017 with a microcontroller helped me understand how to use this:

The expander also has built in pull-up resistors, so you don't need "real" ones between your buttons and the ports, saving some space.

I hope to get this working soon with the python-uinput mode to realize what I want.

Other related and helpful resources: