Recently, I started working on using the MCP23018 IO Expander, to add more IO to my Arduino UNO. I tried several libraries out there, but I finally ran into one posted by OldBikerPete, here.
The MCP2301x library he posted worked like a charm.
I just made one little modification to it, to add functions to enable/disable the internal pull-up resistor for individual pins, or port. This enabled me to configure the input and output pins to behave like the pins in Arduino or AVR boards.
For instance, configuring output pins with NORMAL polarity and pull-up resistor ENABLED, the pins, when HIGH, will output +5v, or +3.3v if working with that voltage -- no need to add a pull up resistor externally. They will output 0v when LOW.
For input pins, configuring them with INVERTED polarity and pull-up resistor ENABLED, the pin will be set HIGH when grounded, such as with a push button. There is no need for external pull-up resistor.
Attached is Peter's library, with the addition of the functions to enable/disable the internal pull-up resistor. I also added example code for connecting two MCP23018 IO expanders.
Both MCP23018 are wired similarly, and both share the same I2C lines. The only difference is setting up the address for each. For the first one, you can simple ground the ADDR pin. For the second one, you can connect the ADDR pin with any resistor to +5v (or +3.3v, depending on voltage of the board). The address for MCP 1 will be 0x20, and for two will be 0x27.
If adding more than 2 MCP chips, then you need to use voltage dividers. In the attached example, I use a voltage divider to set the second MCP to address 0x21. Read the datasheet for the MCP23018 for voltage ranges for each address.
Big thanks, and full credit to OldBikerPete!
MCP2301x.zip (21.4 KB)