I2C: MCP23016

WiringPi supports an extension module for the MCP23016 (16-bit) I2C GPIO expansion chips. The MCP23016 is the fore-runner of the MCP23017 GPIO expansion chip and may be present on some older interface boards.

The Raspberry Pi has one I2C bus and the MCP23016’s have a 3-bit address select port, so in-theory you can connect up 8 MCP23016’s giving you a GPIO capability of an additional 128 IO pins using these devices.

Include

#include <wiringPi.h>
#include <mcp23016.h>

Initialise

mcp23017Setup (int pinBase, int i2cAddress) ;

The pinBase can be any number you like above 64 and the i2cAddress is the address of the device in the I2C bus (usually 0x20)  is the default but they can change if you have multiple devices. Use the i2cdetect command to probe your I2C bus to work out the right address to use.

You can call mcp23016Setup() as many times as needed for each MCP23016 you have in the system – just give it a different pin base and I2C bus address.

You don’t need to specify the number of pins here – the MCP23016 has 16 pins.

Notes

  • You need to load the I2C kernel modules before you can use I2C devices. Use the gpio command: gpio load i2c
  • Use the i2cdetect program to scan your I2C bus to make sure the Pi can see your devices. MCP23016’s will normally show up as 0x20, but when using multiple ones, they’ll each have a unique address (or should have!)
  • If you have a Rev 1 Pi, then the i2cdetect command is: i2cdetect -y 0 if you have a Rev. 2 Pi, then use i2cdetect -y 1
  • The gpio command supports the i2cdetect command and automatically caters for board revision. Simply type: gpio i2cd
  • The wiringPi MCP23016 driver knows which revision Pi you have, so you know need to take any special precautions – your code will work on either a Revision 1 or 2 Pi.
  • The MCP23016 does not have any internal pull-up/down resistors.