Python tools for the MCP23017 I/O Expander

I’ve been playing around with the MCP23017 I2C I/O Expander on my Raspberry Pi a bit more this week and as I mentioned in an update to the last post I’m now running this 3.2 kernel that has hardware I2C drivers for the Pi. The next step was finding a better way to control the expander than using i2c-tools.

Python isn’t something I’ve really used much before but there is a nice python-smbus module that makes interfacing with I2C devices relatively straightforward so that’s what I’ve used to knock up a couple of simple tools to control the MCP23017.

The first one is a command line tool that allows you to set any of the 16 IO pins as high or low, usage is: mcp23017.py -b <bank> -o <output> -s <high|low>

eg. to set GPA1 high it would be: mcp23017.py -b a -o 1 -s high

This gives a response of: Output GPA1 changed to high

The code is on GitHub here, other than a working I2C setup all you will need are the Python bindings for SMBus which are in the python-smbus package on Debian.

The second tool is a simple web interface built using WSGI, something I’ve never used before – it’s basically CGI for Python.

The web tool can be controlled through its own built in web form (very simple but good enough for demo purposes and can be built on) or via GET requests with optional JSON style responses which will allow it to be easily integrated into other projects.

eg. to set output GPA1 high: http://rpi/mcp23017.wsgi?bank=a&output=1&state=high&mode=json

This gives a JSON like response of: {“GPA1″:”HIGH”}

I want to extend this now to cater for input as well as output and to report on the current state of the outputs.

The code for this is also on GitHub and as well as the python-smbus module you will need apache with mod-wsgi, if you get permission errors make sure that the apache user (www-data on Debian) has permissions to read/write to the i2c bus dev entry, eg. /dev/i2c-0

UPDATE 2/6/12: I’ve created a plug in expander board using the Ciseco “Slice of Pi” and the MCP23017, see this post for details and a step by step guide to getting it working with the above Python tools including a link to a Raspberry Pi kernel/modules with I2C compiled in.

8 thoughts on “Python tools for the MCP23017 I/O Expander

  1. Dear Nathan,
    you made a great job with this, I’m working to realize the python driver reader. That seems to work but sometimes catch an exception. Have you got suggestions to debug/troubleshooting that ? I’m newbie on I2C and python too.

  2. Hello Nathan
    I thank you for a very interesting project in the MCP23017 I/O Expander.
    The Python software to control the outputs works great and I have used it to control various projects.
    I am looking for some guidance on setting the expander as an input device and wondered if you have any Python software that i could use, or if you could point me in the direction of where I may obtain assistance.

  3. Hey Nathan,

    I’m playing around with the MCP23017 and would like to control it using a web service (and preferrably python).
    So I stumbled upon your excellent page, found your wsgi script, but I have no clue how I can plug it into apache on my pi.
    I just put the script in /var/www but when I make the request as mentioned on your page, it just returns the source code from the wsgi script.

    Any pointers on what I’m doing wrong here? Am I missing some configuration?

    Thanks,
    Björn

  4. Hey Nathan,

    Thanks for the reply!
    I already did most of the stuff on your page.
    The clue to getting it to work for me was that I configured my apache to run on port 8787.
    This means that the /etc/apache2/sites-available/default file also needs to point to this port, and not to the default port (80).

    Thanks a million!

  5. Hi Nathan,

    Thanks for your code here – could you direct me on how to read values in from a sensor on a pin? the output is working great but what needs to be done to read INPUT?

    Still learning Python.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.