Objective: connect MCP23017 using I²C

You will need: Raspberry Pi, MCP23017, 1 off 330Ω resistors, push-button switch, patch leads and small breadboard.


Picture of Microchip MCP23017

This project demonstrates how the Microchip MCP23017 16-Bit I/O Expander with I²C interface can be used to increase the number of GPIO pins on your Raspberry Pi. You can also use it to protect your Pi's GPIOs from damage - replacing the MCP23017 is much cheaper than replacing the Pi!

The MCP23017 has two banks of 8 GPIOs; each pin can sink or source up to 25mA subject to a chip limit of 150mA sink on VSS and 125mA source on VDD. It can operate on voltages between 1.8V and 5.5V. Its I²C base address is 0x20 but this can be changed by connecting combinations of A0, A1 and A2 to 3.3V enabling up to eight devices to be used on a single I²C bus.

Exclamation mark The RESET pin on the MCP23017 must be tied to VDD using a 10kΩ resistor. If you do not have this in place the chip may stop working or exhibit strange behaviour.

Exclamation mark You must ensure that each I²C address pin is provided with an external bias and is tied to either ground or 3.3V. This is unlike some other devices where you can use the default address by just leaving the pins unconnected. If you leave any address pin unbiased the address will float and your program will not work reliably.

When you define the base starting address for the 16 pins, the pins will be assigned sequential addresses that map to GPA0 to GPA7 then GPB0 to GBP7. So, for example, GPB1 will have an address that is base+9. Also note that the A and B banks are not organised as you might expect with regard to pin numbers

Microchip MCP23017 address structure

The MCP23017 also has two interrupt pins in addition to the 16 GPIO pins.

Link to source code. You will need to use the following commands to compile and run the program. You will be using the wiringPi library so it needs to be included on the command line:

gcc rp-mcp23017.c -o rp-mcp23017 -lwiringPi


Here is the wiring layout. The example has a push-button on GPA0, which is pulled high by the software. The push-button connects to ground. The LED is connected to GPB0 and is in series with a 330Ω resistor. The program flashes the LED on and off; pressing the button pauses the sequence until it's released.

Wiring layout

The MCP23017 is similar to the PCF8574 that is found on the I²C backpack on LCDs. The PCF8574 has only 8-bit capacity whereas the MCP23017 is 16-bit.

Do this with an ESP8266 or an Arduino.