As we mentioned, the version 0.4  beta boards have got minor defects.

Interface board: The track that connects P6 on the 10-way female to header pin 22 (SOC 25) was deleted by mistake. This meant that reading from the pin always returned a 0 and writing to the pin had no effect.

P6 is the only pin affected: everything else seems to be fine.

The missing connection can be restored by soldering a short length of wire between the underside of the 26-way IDC header and appropriate connection to the diode array.

Here’s a photo of a patched board, seen from below:

The patch wire required to replace the missing track

Fixing the missing track

This is covered as the last step of the assembly guide (link below).

The port extender also has a defect: we reversed the numbering on the silkscreen, so port GPA0 is numbered GPA7, and vice versa. This applies to both 8-bit ports.

Both defects are fixed in the production versions.

Here are the documents (on Google Drive for now):

interface board assembly guide

port extender board assembly guide

Testing guide for interface board

The best introduction to starting with the port extender is the article Physical Python – Part 1 which describes I2C setup,

Then follow the getting started guides (copied from github):

Quick2Wire Python API

A Python library for controlling the hardware attached to the Raspberry Pi’s header pins, without running as the root user.

Dependencies

The library depends on Python 3. To install Python 3 run this command from an administrator account, such as pi:

sudo apt-get install python3 

You’ll also find the python tools virtualenv and pip useful:

sudo apt-get install python-pip
sudo apt-get install python-virtualenv 

The GPIO API depends on Quick2Wire GPIO Admin. To install Quick2Wire GPIO Admin, follow the instructions athttp://github.com/quick2wire/quick2wire-gpio-admin

The I2C and SPI API depend on support in the kernel. Recent raspbian kernels should be fine.

Installation

The library is currently under active development, so we do not recommend installing it into the system-wide Python libraries. Instead, you can either use it without installation or install it into an isolated Python development environment created with virtualenv.

To use the library without installation, add the full path of the src subdirectory of the source tree to the PYTHONPATH environment variable. For example:

export QUICK2WIRE_API_HOME=[the directory cloned from Git or unpacked from the source archive]
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME/src 

If you’re using virtualenv, make your virtualenv active, and then run:

python3 setup.py install 

Getting Started With I2C

Warning:

Revision 2.0 of the Raspberry Pi swaps the connections to I2C buses 0 and 1.

With a revision 2.0 board, if you connect an I2C device to the appropriate header, you will see it when you run i2cdetect 1 instead of i2cdetect 0.

The library now auto-detects whether you are running version 1.0 or 2.0 of the board, so the same code will work on either.

The example:

In this example, we’re going to write a program that reads the state of the GPIO pins of an MCP23008 port expander connected to the Raspberry Pi’s I2C bus.

Before You Start Coding…

Ensure you are in the i2c group. Run the groups command to list your group membership. For example:

$ groups nat fuse i2c gpio 

You can add yourself to the i2c group with the command:

sudo adduser $USER i2c 

You must then log out and in again for Linux to apply the change in group membership.

Check the MCP23008 is connected to your I2C bus and its address is configured as expected. We can see the device on the bus by running the i2cdetect command. Remember to replace 0 with 1 if you hav a revision 2 board.

$ i2cdetect 0
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-0.
I will probe address range 0x03-0x77.
Continue? [Y/n] Y
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --             

The default address of the MCP23008 is 0×20, but it can be changed (read the chip’s data sheet for information about that). If the chip appears at a different address, change the value of the address variable in the code below.

Now Let’s Write Some Code!

To use the Quick2Wire I2C API we must import the quick2wire.i2c module. We’ll import it with a shorter name for convenience:

import quick2wire.i2c as i2c 

Let’s define variables to represent attributes of the MCP23008:

address = 0x20
iodir_register = 0x00
gpio_register = 0x09/code>

To communicate with the chip we need to create an I2CMaster object. The I2CMaster class supports the context manager protocol, meaning we can use the with statement to automatically close the bus when the user quits our program by pressing Control-C.

with i2c.I2CMaster() as bus:
    ...

Now we can communicate with the chip. First we’ll set all the GPIO pins be inputs by writing to the chip’s IODIR register. Setting a bit in the register to 1 switches the corresponding pin to be an input, so setting the byte to 255 (or 0xFF in hex) switches all pins to input. To write to the register we perform an I2C transaction containing a single write operation that writes two bytes: the register to write to and the value of the register.

 bus.transaction(
        i2c.writing_bytes(address, iodir_register, 0xFF))

Then we’ll read the value of the chip’s GPIO register by performing a transaction containing two operations: a write operation that tells the chip which register we want to read, and a read operation that reads a single byte from that register.

 read_results = bus.transaction(
        i2c.writing_bytes(address, gpio_register),
        i2c.reading(address, 1))

The I2CMaster’ transaction method returns a list of byte sequences, one for each read operation performed. Each result is an array of bytes read from the device. So the state of the GPIO pins is the first and only byte of the first and only byte sequence returned.

 gpio_state = read_results[0][0] 

We finally print that in hexadecimal:

 print("%02x" % gpio_state) 

Putting it all together:

#!/usr/bin/env python3

import quick2wire.i2c as i2c

address = 0x20
iodir_register = 0x00
gpio_register = 0x09

with i2c.I2CMaster() as bus:    
    bus.transaction(
        i2c.writing_bytes(address, iodir_register, 0xFF))

    read_results = bus.transaction(
        i2c.writing_bytes(address, gpio_register),
        i2c.reading(address, 1))

    gpio_state = read_results[0][0]

    print("%02x" % gpio_state)

5 Comments

  1. Having validated that my main expansion board is working I’ve now moved on to the port extender and I’m hitting a problem in that i2cdetect is NOT showing any address. Now this could well be down to an issue in my construction of one of the boards but I’m also wondering if in need to set the DIL switch (currently set to default shipped position). The assembly guide refers to the testing guide for more details but I can’t seem to find anything re setting the MCP23017 address via the DIL switches.

    Can you help point me in the right direction?

    Reply

    • Actually scratch that reseating the MCP23017 has fixed the issue – I think I should just avoid build work when I’m also fighting a flu bug. On with some more testing now.

      Reply

  2. feeback on the beta kit so far :

    feedback on beta kit

    from the testing the interface board section :

    https://docs.google.com/document/d/1_iAOXCrL0DSKhXRpXKX1bdGXJ7E0GMOsT9KgK-bKGs4/edit?pli=1

    pi@raspberrypi:~$ gpio-admin export 18
    -bash: /usr/bin/gpio-admin: Permission denied

    fixed with :
    sudo chmod +x /usr/bin/gpio-admin

    (probably could just set group execute if your paranoid)

    * I use gkterm on Linux, and (painfully) have used minicom. I’ve used terraterm on Windows. Mac users will know what they should use :)

    linux and mac users can use gnu screen (installed normally by default)

    screen /dev/ttyX 115200

    Will do the trick

    Reply

  3. installation section here need more work , does not really cover where to get the library from
    just what to do with it once you have it.

    also if you have just followed the instructions above you already have gpio-admin installed.

    ic2detect etc also need to be installed , but from where? …. (yes I know it’s i2c-tools but will others)

    needs more work here for sure

    Reply

    • Thanks for the feedback.

      You’re right – the getting started guide needs a lot of attention. I just spotted that I left out a link to Physical Python Part 1. The text refers to it, but there is no link to click. That article covers things like installing i2c-tools and enabling the i2c drivers in the kernel.

      I hope to have a first, partial draft of an improved ‘getting started’ guide available for review early next week.

      Reply

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>