Using the MCP23017 with Arduino involves wiring up the expander to your Arduino-compatible microcontroller, installing the Adafruit MCP23017 library and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the MCP23017 VIN.
Here is an Adafruit Metro, a button and an LED wired up to the MCP23017 using a solderless breadboard:
MCP23017
- Board 5V to expander VIN (red wire)
- Board GND to expander GND (black wire)
- Board SCL to expander SCL (yellow wire)
- Board SDA to expander SDA (blue wire)
LED
- LED + to 470Ω resistor
- LED - to Board GND (black wire)
- 470Ω resistor to expander A0 (pink wire)
Button
- Button leg to expander A1 (green wire)
- Opposite button leg to Board GND (black wire)
Library Installation
You can install the MCP23017 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for MCP23017, and select the Adafruit MCP23017 Arduino Library library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
Load Example
Open up File -> Examples -> Adafruit MCP23017 Arduino Library -> mcp23xxx_combo. Before uploading the code, comment out the mcp
instance using the Adafruit_MCP23X08
class, and uncomment the mcp
instance using the Adafruit_MCP23X17
class.
// uncomment appropriate line // Adafruit_MCP23X08 mcp; Adafruit_MCP23X17 mcp;
// Controls an LED via an attached button. // ok to include only the one needed // both included here to make things simple for example #include <Adafruit_MCP23X08.h> #include <Adafruit_MCP23X17.h> #define LED_PIN 0 // MCP23XXX pin LED is attached to #define BUTTON_PIN 1 // MCP23XXX pin button 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 Combo Test!"); // uncomment appropriate mcp.begin if (!mcp.begin_I2C()) { //if (!mcp.begin_SPI(CS_PIN)) { Serial.println("Error."); while (1); } // configure LED pin for output mcp.pinMode(LED_PIN, OUTPUT); // configure button pin for input with pull up mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); Serial.println("Looping..."); } void loop() { mcp.digitalWrite(LED_PIN, !mcp.digitalRead(BUTTON_PIN)); }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 9600 baud. You should see the text "Looping...
" in the Serial Monitor.
When you press the button, the LED will light up.
That's all there is to using the MCP23017 STEMMA breakout with Arduino!
Page last edited January 22, 2025
Text editor powered by tinymce.