Interfacing GPIO Expander MCP23017 with Arduino

1,161 views

Introduction

The MCP23017 is a popular GPIO expander IC that allows the user to increase the number of available input/output pins on a microcontroller. By interfacing this IC with an Arduino board, we can effectively expand the number of digital pins on our board, giving us greater flexibility in designing and controlling our projects.

In this article, we will explore how to interface the “MCP23017 GPIO expander with an Arduino” board and demonstrate some basic programming examples.

What is GPIO Expander MCP23017?

GPIO expander MCP23017 is an Integrated Circuit (IC) that enables the expansion of General Purpose Input/Output (GPIO) pins on a microcontroller. The MCP23017 offers 16 additional GPIO pins, which can be individually configured as input or output. It communicates with the microcontroller through a serial interface using either I2C or SPI protocol. This IC is commonly used in various applications, including robotics, home automation systems, and industrial automation, where the need for additional GPIO pins is critical.

Hardware Components

To interface MCP23017 GPIO Expander with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
GPIO ExpanderMCP230171
Jumper Wires1

MCP23017 Pinout

GPIO Expander MCP23017-Pinout
GPIO Expander MCP23017-Pinout
Pin labelPin Description
GPB0-GPB7Bidirectional Input Output pin. Can be enabled for interrupt-on-change and/or internal weak pull-up resistor
GPA0-GPA7Bidirectional Input Output pin. Can be enabled for interrupt-on-change and/or internal weak pull-up resistor
VDDPower
VSSGround
NCNo connection. Leave the pin unconnected
SCLSerial clock line (I2C)
SDAThe serial data line (I2C)
A0Hardware address pin A0 – Must be externally biased
A1Hardware address pin A1 – Must be externally biased
A2Hardware address pin A2 – Must be externally biased
RESET#Active low reset pin
INTBInterrupt output for Port B. Can be configured as active-high, active-low or open-drain
INTAInterrupt output for Port A. Can be configured as active-high, active-low or open-drain

GPIO Expander Circuit

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoMCP23017 IO Expander
5VVCC
GNDGND
A4SDA
A5SCL

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

// Blinks an LED attached to a MCP23017 pin.

#include <Adafruit_MCP23X08.h>
#include <Adafruit_MCP23X17.h>
 
#define LED_PIN 0     // MCP23XXX pin LED is attached to
 
// only used for SPI
#define CS_PIN 6
 
// uncomment appropriate line
Adafruit_MCP23X08 mcp;
//Adafruit_MCP23X17 mcp;
 
void setup() {
  Serial.begin(9600);
  //while (!Serial);
  Serial.println("MCP23xxx Blink Test!");
 
  // uncomment appropriate mcp.begin
  if (!mcp.begin_I2C()) {
  //if (!mcp.begin_SPI(CS_PIN)) {
    Serial.println("Error.");
    while (1);
  }
 
  // configure pin for output
  mcp.pinMode(LED_PIN, OUTPUT);
 
  Serial.println("Looping...");
}
 
void loop() {
  mcp.digitalWrite(LED_PIN, HIGH);
  delay(500);
  mcp.digitalWrite(LED_PIN, LOW);
  delay(500);
}

Code Explanation

This Arduino code blinks an LED attached to a pin on the MCP23017 GPIO expander IC. The code uses the Adafruit_MCP23X08.h or Adafruit_MCP23X17.h library to communicate with the MCP23017 IC.

The LED is attached to pin 0 on the MCP23017, which is defined by the #define LED_PIN 0 statement.

The mcp.begin_I2C() function initializes the communication with the MCP23017 through the I2C protocol, and the pinMode() function sets the LED_PIN as an output pin.

In the loop function, the digitalWrite() function turns the LED on and off with a delay of 500 milliseconds between each state change.

Overall, this code demonstrates how to use the Adafruit_MCP23X08 or Adafruit_MCP23X17 library to control the MCP23017 GPIO expander and how to interface it with an LED to create a simple blinking effect.

Applications

Here are some common applications of the GPIO expander MCP23017:

  • Robotics
  • Home automation systems
  • Industrial automation
  • Audio systems
  • Gaming and entertainment systems
  • Internet of Things (IoT

Conclusion

The MCP23017 GPIO expander with an Arduino board can greatly increase the number of available digital pins, giving us greater flexibility in designing and controlling our projects. By using the library and sample code provided in this article, we can easily incorporate this IC into our Arduino projects and take advantage of the additional functionality it provides. Whether we are building a robotics project or a home automation system, the MCP23017 GPIO expander can help us achieve our goals with greater ease and efficiency.