This website was originally written in Ruby and hosted on the Raspberry Pi. It's now archived at GitHub for posterity!

WiringPi Version 2 With Extra Python

It's no secret that my WiringPi- libraries have been hideously out of date and for months. But some awesome work by Gordon Henderson has spurred me back into action.

First and foremost, those of you still loyal to the old WiringPi, and my old, well publicised and well used wrappers will be pleased to hear that I'm going to give them a little TLC and try to fix 'em up, push them to PyPi and have a nose through the various different issues reported to see if I can find any resolution.

Second, WiringPi 2. WiringPi 2 includes a great new feature, or I should call it a concept, which allows you to easily set up IO expanders and write to them as if they were a normal GPIO pin. No poking values into registers, of fussing about with the nuances of SPI or I2C. Just dial in the address of your MCP23017 or the pins of your shift register and use their pins like you would any other GPIO pins.

A simple example of this is as follows:

import wiringpi2

pin_base = 65
i2c_addr = 0x20
pins = [65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80]


for pin in pins:

Gordon dubs these brilliant little helpers "Extensions" and they make chaining a metric ton of IO expanders onto your Pi absurdly easy.

To demonstrate just how conceptually awesome this is, here's some of the code I wrote when first playing with the MCP23017:

def portA
        "0x%x" % ( ( @leds & FIRST_BYTE ) >> 8 )

def portB
        "0x%x" % ( @leds & SECOND_BYTE )

def ledOn( led )
        led = LEDMAP[ led ]
        @leds = @leds | led

def ledOff( led )
        led = LEDMAP[ led ]
        @leds = @leds & ~led

def updatePorts

        system 'i2cset -y 1 0x20 0x12 ' + portA + ' && i2cset -y 1 0x20 0x13 ' + portB


Ignoring the differences between Python and Ruby, this code is hideous. But I'm not going to complain, because it taught me a lot about how the MCP23017 chip actually works; and that's a good thing.

Anyway. Moving swiftly back to WiringPi 2. You can install it right now on your Pi with:

sudo pip install wiringpi2

If you have a hobbytronics IO expander or a quick2wire board with an IO expander you can get started with my example above and be rocking 16 additional IO pins with ease. Otherwise, WiringPi works pretty much how you'd expect for built-in GPIO. If you have trouble installing the Python version, tell me not Gordon!

Gordon has a new website for WiringPi inbound, you can find it here:

I have pushed the WiringPi2-Python source up to GitHub, which you can find here: Please note that it's early days for WiringPi 2, and my code is definitely not perfect, bug free and feature complete.

« Back to index Posted on 2013-03-27 by