Tuesday, January 1, 2019

Use a MCP23017 GPIO port expander with an ATmega

The MCP23017 is a general purpose I/O port expander, it is driven using the I2C interface.
Each of the 16 channels can be set independently as an input or as an output pin.

This library provides functions to drive up to 8 MCP23017, it means you can expand your micro to 128 between input and output.

One of the interesting feature of this IC is the possibility to set an interrupt.
Interrupts can trigger a change on both INTA and INTB chip output, if mirror mode is selected a change on the bus A, or B trigger interrupts on both INTA and INTB.
One can also set the polarity of the interrupt, it means if the input gives you a high or low level on output.
Output interrupt channel can also be set as open drain.
You can set interrupt on each individual input channel, setting the compare mode, i.e. if the pin status has changed since the last reading, or if the pin has changed compared to a value set by the user. This last option is usefull if you want as example to set the comparation for rising or falling mode.
Then the interrupt can of course be enabled just for a single pin.

This interrupt features gives you the ability to quickly detect the change on a pin.

This library also provides function to set each individual pin mode as input or output, output pin status and read input pin status. Functions to read group of values for the full A or B channels as byte are also provided.

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.

If you want further information on that IC, i suggest you to take a look to that two interesting articles on that IC:

  • 01c: fixed main sample project
  • 01b: fixed an error on addressing when multiple IC connected
  • 01: first version


  • read risk disclaimer
  • excuse my bad english