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:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
GPIO Expander | MCP23017 | 1 |
Jumper Wires | – | 1 |
MCP23017 Pinout
Pin label | Pin Description |
---|---|
GPB0-GPB7 | Bidirectional Input Output pin. Can be enabled for interrupt-on-change and/or internal weak pull-up resistor |
GPA0-GPA7 | Bidirectional Input Output pin. Can be enabled for interrupt-on-change and/or internal weak pull-up resistor |
VDD | Power |
VSS | Ground |
NC | No connection. Leave the pin unconnected |
SCL | Serial clock line (I2C) |
SDA | The serial data line (I2C) |
A0 | Hardware address pin A0 – Must be externally biased |
A1 | Hardware address pin A1 – Must be externally biased |
A2 | Hardware address pin A2 – Must be externally biased |
RESET# | Active low reset pin |
INTB | Interrupt output for Port B. Can be configured as active-high, active-low or open-drain |
INTA | Interrupt 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
Arduino | MCP23017 IO Expander |
---|---|
5V | VCC |
GND | GND |
A4 | SDA |
A5 | SCL |
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.