MCP23008

Control the MCP23008/MCP23S08 8-Bit I/O Expander with Serial Interface with your Arduino

View on GitHub

PU2CLR - MCP23008 Arduino Library

It is an Arduino Library to control the MCP23008 8-Bit I/O Expander.

The MCP23008 device provides 8-bit, general purpose, parallel I/O expansion. It can be controlled via I²C bus applications. It is a great and inexpensive device that allow you to add more peripherals to be controlled by your Arduino board via I²C protocol.

Some Arduino projects may require more pins than the Arduino actually has. In these cases, you can use up to 8 MCP23008 devices using only the I²C bus (two Arduino pins) and add up to 64 input / output ports to your project.

This Arduino library implements the most important functions available on MCP23008 device and can be freely distributed using the MIT Free Software model.

Copyright (c) 2021 Ricardo Lima Caratti.

If you find this project useful, consider making a donation so that the author of this library can purchase components and modules for improvements and testing of this library. Click here to donate.

About Me

I hold a Master’s degree in Educational Technology from the Federal University of Ceará, where I delved into the ways technology can enhance learning experiences. My passion for computer science led me to specialize in it, focusing on Distributed Systems Development with an Object-Oriented approach, at the University of Brasília. My academic journey began with a Bachelor’s degree in Information Systems from the União Pioneira de Integração Social (UPIS-Brasília). Outside the classroom, my main hobbies are electronics and Amateur Radio.

Contents

  1. License Copyright
  2. Thanks
  3. Labrary Features
  4. Library Installation
  5. Other Arduino Libraries developed by the Author
  6. API documentation
  7. Basic Schematic
  8. Internal Interrupt setup
  9. MCP23008 reset control
  10. References

MIT License

Copyright (c) 2019 Ricardo Lima Caratti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE ARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Thanks

Labrary Features

This library uses the I²C communication protocol and implements most important functions offered by MCP23008 device from MicroChip. It also has primitive functions that make it easier to implement commands that may not have been implemented yet. The main features implemented can be seen below:

Demo video

MCP23008 Input/output setup with Arduino Nano Atmega 168

See on Youtube

Registers

This library has two basic function that you can use to control everything on MCP23008. You can use getRegister and setRegister methods to setup the registers below. Also this library has other functions that make the job easier to build applications based on Arduino and MCP23008 devices. See API documentation.

Register Value Description
IODIR 0x00 Controls the direction of the data I/O. When a bit is set, the corresponding pin becomes an input. When a bit is clear, the corresponding pin becomes an output.
IPOL 0x01 The IPOL register allows the user to configure thepolarity on the corresponding GPIO port bits.
GPINTEN 0x02 The GPINTEN register controls the interrupt-on-change feature for each pin.
DEFVAL 0x03 The default comparison value is configured in the DEFVAL register.
INTCON 0x04 The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.
IOCON 0x05 The IOCON register contains several bits for configuring the device.
GPPU 0x06 The GPPU register controls the pull-up resistors for the port pins.
INTF 0x07 The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register.
INTCAP 0x08 The INTCAP register captures the GPIO port value at the time the interrupt occurred.
GPIO 0x09 The GPIO register reflects the value on the port.
OLAT 0x0A The OLAT register provides access to the output latches.

Also this library has other functions that make the job easier to build applications. See API documentation.

Library Installation

Under construction..

You can install this library on your Arduino environment using different methods. The best ways to do that are described below.

Installing via Arduino IDE

This is the easiest method to install this library.

On Arduino IDE, select the “Manager Libraries…“ item and look for MCP23008. Finally select “PU2CLR MCP23008”. The images below show the installation process.

Going to Manager libraries…

Basic Schematic with LEDs

Looking for MCP23008

Basic Schematic with LEDs

Checking the PU2CLR_MCP23008 examples

Basic Schematic with LEDs

More information

See also Installing an Arduino Library

Installing via this repository

First, you have to download this library in zip format. After, unzip the MCP23008-master.zip file in your Arduino Library folder.

With that approach, you will have the most current version of the library. However, it may not be the most stable version. This is because the current version is always in development. Prefer releases. Do you need some old version (release) of this library? If yes, check here.

Other Arduino Libraries and projects developed by the Author

Basic schematic

The image below shows a basic MCP23008 application with LED. You can control up to 8 LEDs. The I²C bus address is set to 0x20. You can select another I²C address by dealing with the A0,A1 and A2 pins (from 0x20 to 0x27). This circuit uses the MCP23008 GPIO PINs as output.

GPIO as output setup

Basic Schematic with LEDs

Arduino setup suggestion

#include <pu2clr_mcp23008.h>

MCP mcp;

void setup() {
  mcp.setup(0x20, 0); // I²C Address 0x20 and all GPIO pins configured to output

You also can use the methos setRegister as shown below (See MCP23008 IODIR register)

    mcp.setRegister(REG_IODIR, 0B00000000);    // Sets  all GPIO pins to output

Output setup example

#include <pu2clr_mcp23008.h>

MCP mcp;

void setup() {
    mcp.setup(0x20,0);   // I²C address 0x20; All GPIO port/pin are configured as output
}

void loop() {
    for (uint8_t i = 0; i < 8; i++)
    {
        mcp.turnGpioOn(i); // Turns the GPIO / port i on
        delay(200);
        mcp.turnGpioOff(i); // Turns the GPIO / port i off
        delay(200);
    }
    delay(2000);
    for (uint8_t i = 0; i < 8; i++)
    {
        mcp.turnGpioOn(i); // Turns the GPIO / port i on
        delay(300);
    }
    delay(2000);
    for (uint8_t i = 0; i < 8; i++)
    {
        mcp.turnGpioOff(i); // Turns the GPIO / port i off
        delay(300);
    }
    delay(2000);
}

GPIO as input setup

Basic Schematic with LEDs

Arduino setup suggestion

void setup() {
  mcp.setup(0x20, 0B11111111); // I²C Address 0x20 and all GPIO pins configured to input

You also can use the methos setRegister as shown below (See MCP23008 IODIR register)

    mcp.setRegister(REG_IODIR, 0B11111111);    // Sets  all GPIO pins to input

GPIO as input and output setup

Basic Schematic with LEDs

#include <pu2clr_mcp23008.h>

MCP mcp;

void setup() {
  mcp.setup(0x20, 0B00001111); // I²C Address 0x20, GPIO 0-3 are inputs and  4-7 are outputs

You also can use the methos setRegister as shown below (See MCP23008 IODIR register)

    mcp.setRegister(REG_IODIR, 0B00001111);    // Sets  GPIO pins 0-3 to input and  GPIO pins 4-7 to output

See input/output sketch example

Prototype

The prototype below is based on the GPIO as input and output setup schematic

Basic prototype based on input / output schematic

Internal Interrupt setup

The MCP23008 device has internal interrupt support. That means you can know instantly when a GPIO configured as input changed its status (from high to low or from low to high). This library implements some functions to support MCP23008 internal interrupts. You also can configure the internal interrupt behaviour. See the MCP23008 library API for more details. See also the examples mcp_poc_interrupt01 ans mcp_poc_interrupt01.

The code below shows how to setup internal interrupt on given GPIO pin. Observe the MCP23008 internal pullup configured before configuring the interrupt. In this setup, the gpio.

It is important to know: only pins configured as inputs can cause interrupts. Pins configured as outputs have no affect on INT. After an interrupt is triggered, you can not deal with other interrupt until GPIO or INTCAP registers is read. Only reading GPIO or INTCAP registers will clean the interrupt status and will make a new interrupt possible.


  mcp.pullUpGpioOn(MCP_GPIO1);          // Enables internal pullup resistor on gpio pin 1
  mcp.pullUpGpioOn(MCP_GPIO3);          // Enables internal pullup resistor on gpio pin 3
  
  mcp.setInterrupt(INTERRUPT_INTPOL_ACTIVE_HIGH, INTERRUPT_ODR_OPEN_DRAIN); // Defines the behaviour of the interrupt

  // GPIO pin 1 setup
  mcp.interruptGpioOn(MCP_GPIO1, HIGH); // Sets the GPIO pin 1 to deal with interrupt. The pin 1 will be compared with the value 1 (HIGH). It will be launch an interrupt if the pin 1 goes to level 0 (LOW).

  // GPIO pin 5 setup
  mcp.interruptGpioOn(MCP_GPIO3, HIGH); // Sets the GPIO pin 3 to deal with interrupt. The pin 3 will be compared with the value 1 (HIGH). It will be launch an interrupt if the pin 1 goes to level 0 (LOW).

Again. see also the examples mcp_poc_interrupt01 ans mcp_poc_interrupt02 for more details. The schematic below can be used to check MCP23008 interrupt applications.

Basic schematic for interrupt setup

MCP23008 reset control

In most applications you can use the MC23008 reset pin directly connected to the VCC. You can also connect the MCP23008 RESET pin to the Arduino RESET pin (It is better than previous setup). However, you might need to control the reset via your Arduino sketch. If so, check the setup and reset functions to do that on MCP23008 library API Documentation. The code below shows the RESET setup on Arduino and MCP23008.

#include <pu2clr_mcp23008.h>

#define RESET_PIN 12  // Arduino pin 12 will be used to control the MCP23008 RESET

MCP mcp;


void setup() {

  mcp.setup(0x20, 0B00001111, RESET_PIN);  // GPIO 0 to 3 are input (buttons) and 4 to 7 are output (LEDs)
  mcp.setRegister(REG_GPPU, 0B00001111); // sets GPIO 0 to 3 with internal pull up resistors

  .
  .  
  . 
  mcp.reset();  // the setup function above already called the MCP23008 reset. But you can also call reset function if you need it in other situation

Extending the MCP class

A good way to customize the PU2CLR MCP Arduino Library for your needs is extending the current version of the library by using C++ OOP approaching. For example:

#include <pu2clr_mcp23008.h>
class MyCustomMCP : public MCP { // extending the original MCP class
  public:
    // New functions / methods
    int methodA() {    // some MCP action that PU2CLR MCP Arduino Library does not implement
      return 0;
    }

    int methodB() {    // another MCP action that PU2CLR MCP Arduino Library does not implement
      return 1;
    }

    // Overwriting existent methods
    void setRegister(uint8_t reg, uint8_t value) { 
      // Here, your setRegister code that will replace the original code 

      return;
    }
}

// Now you can use the extended class

MyCustomMCP myMCP;

void setup()
{
  Serial.begin(9600);
  while (!Serial);

  myMCP.methodA();
  myMCP.methodB();
  myMCP.setRegister(REG_GPIO, (myMCPgetRegister(REG_GPIO) & ~(1 << 2)) | (HIGH << 2) );

}




References