One frustrating aspect of designing a project around a microcontroller is the realization that you don’t have enough GPIO pins to drive all the all components you want to include. If you are lucky there are larger chips with the same architecture you have chosen (think going from and Arduino Uno to an Arduino Mega). But sometimes that is still not enough, projects involving robots and driving lots of LEDs comes to mind. One easy way around this limitation is to utilize specialized external microchips called “port expanders” that you can connect to your microcontroller to give you additional GPIO pins.

In this blog post, I will show you one particular implementation using the MCP23017 I/O expander and a Raspberry Pi (just to prove I am not simply an Arduino fanboy!) to add an additional 16 GPIO pins to my Pi. To communicate between the Pi and the MCP23017 we will be utilizing the I2C serial communication protocol. This is a two-wire protocol and typically the wires are labeled SDA for data and SCL and for clock.

First you will need to hook up the chip to your Pi. After connecting Vcc (for the MCP230117 this can be 1.8V to 5.5V, but remember the Pi can only handle 3.3V) and GND, we also have to establish the address for the MCP23017. The nice thing with I2C is it’s configured as a bus you can control with up to 8 port expanders using the same 2 two pins for SDA and SCL. All we have to do is to change the hardwired address for each MCP23017 by connecting each chip’s address pins to a different arrangement Vcc and GND connections as shown in the chart below:

Screen Shot 2014-12-09 at 8.30.56 PM:

We’re also going to connect a simple pushbutton to one of the expander I/O pins. Don’t forget the pull-down resistor to prevent the input pins from floating. In this example you can follow this layout:

Screen Shot 2014-12-09 at 8.34.03 PM

Next, it is time to write some code. We are going to create a simple application that detects when the pushbutton is pressed and then print a simple message to the screen. For the interest of readability, I only included the code to read one of the MC23017 chips. Just copy and repeat the code for each expander chip you have hooked up in your circuit.

Screen Shot 2014-12-09 at 8.33.04 PM

There you have it, if you use 8 port expander chips you will now have 128 additional GPIO pins at your disposal. What projects could you build with all those additional GPIO pins? Let me know in the comments below.