Objective: connect MCP23017 using I2C

You will need: Arduino UNO, MCP23017, 330Ω resistor, 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 I2C interface can be used to increase the number of GPIO pins on your Arduino Uno. You can also use it to protect your Arduino's GPIOs from damage - replacing the MCP23017 is much cheaper than replacing the Uno!

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 I2C 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 I2C bus.

Exclamation mark You must ensure that each I2C 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.

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.

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.

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 I2C backpack on LCDs. The PCF8574 has only 8-bit capacity whereas the MCP23017 is 16-bit.

Do this with an ESP8266 or a Raspberry Pi.