Reference
MCP23017 16-Bit I/O Expander: Complete Guide
Add 16 fully configurable GPIO pins to a pin-limited board over a two-wire I2C bus, with per-pin pull-ups and interrupt-on-change. Pinout of the MCU-2317 breakout, I2C address selection, registers, an ESP-IDF example and the limits from the datasheet.
- 16 GPIO pins
- I2C up to 1.7 MHz
- 8 addresses
- Interrupts
Overview
The MCP23017 is a 16-bit I/O expander from Microchip. It has two 8-bit ports, PORT A (GPA0 to GPA7) and PORT B (GPB0 to GPB7). Every pin can be an input or an output. You talk to it over I2C, so it takes two pins on your microcontroller, whatever the number of pins you add.
- 16 bidirectional I/O pins in two 8-bit ports
- Weak internal pull-up on every pin, switched on per pin
- Two interrupt outputs (INTA and INTB) that can work separately or together, as active-high, active-low or open-drain
- Interrupt-on-change, with a programmable compare value
- Input polarity inversion
- I2C at 100 kHz, 400 kHz and 1.7 MHz
- Up to 8 devices on one bus, 128 extra pins in total
- The SPI version is the MCP23S17, which is why some breakout pins have double labels
Getting started
Specifications
| Item | Value |
|---|---|
| I/O pins | 16 (2 x 8-bit ports) |
| Interface | I2C (MCP23017) or SPI (MCP23S17, up to 10 MHz) |
| I2C speed | 100 kHz, 400 kHz and 1.7 MHz |
| I2C addresses | 0x20 to 0x27 (three address pins) |
| Supply voltage | 1.8 to 5.5 V, see the note below |
| Maximum current per pin | 25 mA sunk or sourced |
| Maximum current into the VDD pin | 125 mA |
| Maximum current out of the VSS pin | 150 mA |
| Total power dissipation | 700 mW |
| Internal pull-ups | Weak, 40 to 115 µA at 5 V, per pin selectable |
| Standby current | 1 µA maximum |
| Package of the chip | 28 pins (SPDIP, SOIC, SSOP or QFN) |
The voltage range depends on speed and temperature. According to the datasheet:
- 1.8 to 5.5 V at -40 to +85 °C is fine for 100 kHz.
- 400 kHz needs 2.7 V or more.
- 1.7 MHz needs 4.5 V or more.
- Operation up to +125 °C is only specified for 4.5 to 5.5 V.
On 3.3 V you can therefore use 100 kHz and 400 kHz, but not 1.7 MHz.
Pinout
The MCU-2317 breakout has a single row of 10 pins and a double row of 2 x 10 pins. The pictures show the pins on the drawing of the board with their functions. The labels printed on the back are the ones from the board itself.
Every label next to the double row names two pins, for example B0/A0. The drawing does not show which hole belongs to which name. In the pictures the first name of a label is the inner column of the double row and the second name the outer column. That is read from the order of the labels and not confirmed, so check it on your own board with a multimeter before you rely on it.
The datasheet says the RESET pin “must be externally biased”. Leaving it floating is the most common reason for a chip that never shows up on the bus. Tie it to 3.3 V, directly or through 10 kΩ.
Pinout diagram: component side
With the chip facing you, the double row is on the left and the single row on the right. The pins of the single row are labeled on the right. Of the double row the inner and the outer column are both labeled on the left.
Pinout diagram: back
The back shows the printed labels. The rows are mirrored: the single row is on the left and the double row on the right.
Pins at a glance
The pins grouped by function.
I2C address selection
The address is 0100 followed by A2, A1 and A0. Tie each address pin to GND or VCC, never leave them floating.
| A2 | A1 | A0 | Address |
|---|---|---|---|
| GND | GND | GND | 0x20 |
| GND | GND | VCC | 0x21 |
| GND | VCC | GND | 0x22 |
| GND | VCC | VCC | 0x23 |
| VCC | GND | GND | 0x24 |
| VCC | GND | VCC | 0x25 |
| VCC | VCC | GND | 0x26 |
| VCC | VCC | VCC | 0x27 |
One module with all three pins on GND uses 0x20.
GPIO overview
Each of the 16 pins supports digital input, digital output, a weak pull-up, interrupt-on-change and polarity inversion.
| Function | Available? | Alternative |
|---|---|---|
| Digital input | Yes | |
| Digital output | Yes | |
| PWM | No | ESP32 LEDC pins or a PCA9685 |
| ADC (analog input) | No | ESP32 ADC pins or an ADS1115 |
| DAC | No | MCP4725 |
| High-speed switching | Limited by the I2C bus | Native ESP32 GPIO |
It suits buttons, switches, relays (through a driver), LEDs, rotary encoders and keypad matrices.
Connecting to the ESP32-C6 Super Mini
| MCP23017 module | ESP32-C6 Super Mini |
|---|---|
| VCC | 3V3 |
| GND | GND |
| SDA/SI | GPIO 6 |
| SCL/SCK | GPIO 7 |
| RESET | 3V3 |
| A0, A1, A2 | GND |
- On the ESP32 the I2C peripheral can use almost any GPIO, so GPIO 6 and 7 work as SDA and SCL.
- Leave NC/SO and NC/CS unconnected.
- INTA and INTB are optional. You only need them for interrupt-driven inputs.
- Power the module from 3.3 V. The chip itself accepts up to 5.5 V, but the ESP32 pins are not 5 V tolerant, so the I2C lines must not be pulled up to 5 V.
Register map
The chip has 22 registers. With IOCON.BANK = 0 (the default) the A and B registers are interleaved:
| Register | Address (A / B) | Function |
|---|---|---|
| IODIRA / IODIRB | 0x00 / 0x01 | Direction: 1 = input, 0 = output |
| IPOLA / IPOLB | 0x02 / 0x03 | Input polarity inversion |
| GPINTENA / GPINTENB | 0x04 / 0x05 | Interrupt-on-change enable |
| DEFVALA / DEFVALB | 0x06 / 0x07 | Default compare value |
| INTCONA / INTCONB | 0x08 / 0x09 | Interrupt control: compare to DEFVAL or to the previous value |
| IOCON | 0x0A and 0x0B | Configuration. One register that answers on both addresses |
| GPPUA / GPPUB | 0x0C / 0x0D | Pull-up enable |
| INTFA / INTFB | 0x0E / 0x0F | Interrupt flag: which pin fired |
| INTCAPA / INTCAPB | 0x10 / 0x11 | Pin state captured at the interrupt |
| GPIOA / GPIOB | 0x12 / 0x13 | Read the pins or write the port |
| OLATA / OLATB | 0x14 / 0x15 | Output latch |
IOCON holds the bits BANK, MIRROR, SEQOP, DISSLW, HAEN, ODR and INTPOL. After power-up all registers are zero except IODIRA and IODIRB, which are 0xFF (all inputs).
ESP-IDF example
This example blinks an LED on GPA0 and reads a button on GPB0 (with the pull-up on, the button connects the pin to GND). It uses the i2c_master driver and needs no extra library.
ESP-IDF
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c_master.h"
#define SDA_GPIO 6
#define SCL_GPIO 7
#define MCP_ADDR 0x20
#define REG_IODIRA 0x00
#define REG_IODIRB 0x01
#define REG_GPPUB 0x0D
#define REG_GPIOA 0x12
#define REG_GPIOB 0x13
static i2c_master_dev_handle_t mcp;
static esp_err_t mcp_write(uint8_t reg, uint8_t val) {
uint8_t buf[2] = { reg, val };
return i2c_master_transmit(mcp, buf, sizeof(buf), -1);
}
static esp_err_t mcp_read(uint8_t reg, uint8_t *val) {
return i2c_master_transmit_receive(mcp, ®, 1, val, 1, -1);
}
void app_main(void) {
i2c_master_bus_config_t bus_cfg = {
.i2c_port = I2C_NUM_0,
.sda_io_num = SDA_GPIO,
.scl_io_num = SCL_GPIO,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus;
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus));
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = MCP_ADDR,
.scl_speed_hz = 400000,
};
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus, &dev_cfg, &mcp));
ESP_ERROR_CHECK(mcp_write(REG_IODIRA, 0xFE)); /* GPA0 output, rest input */
ESP_ERROR_CHECK(mcp_write(REG_IODIRB, 0xFF)); /* PORT B all inputs */
ESP_ERROR_CHECK(mcp_write(REG_GPPUB, 0x01)); /* pull-up on GPB0 */
while (1) {
uint8_t portb;
if (mcp_read(REG_GPIOB, &portb) == ESP_OK) {
bool pressed = !(portb & 0x01); /* button pulls GPB0 low */
mcp_write(REG_GPIOA, pressed ? 0x01 : 0x00);
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
With the Arduino IDE you can use the Adafruit MCP23017 library: set GPA0 (pin 0) as OUTPUT, GPB0 (pin 8) as INPUT_PULLUP and write the inverted button state to the LED.
Interrupts
Polling 16 inputs wastes bus time. Interrupt-on-change avoids this:
- Enable the pins you care about in GPINTENA and GPINTENB.
- Choose the trigger in INTCON: a change from the previous value, or a difference from DEFVAL.
- Connect INTA and INTB to free GPIO pins on the ESP32, or set the MIRROR bit in IOCON to combine both on one pin.
- Choose in IOCON whether the interrupt pin is active-high, active-low (INTPOL) or open-drain (ODR).
- When the interrupt fires, read INTF to see which pin caused it and INTCAP for the state at that moment. Reading INTCAP or GPIO clears the interrupt.
This suits battery-powered and event-driven projects.
MCP23017 vs PCF8574
| Feature | MCP23017 | PCF8574 |
|---|---|---|
| I/O pins | 16 | 8 |
| I2C speed | up to 1.7 MHz | 100 kHz or 400 kHz, depending on the manufacturer |
| Direction register | Yes | No (quasi-bidirectional) |
| Pull-ups | Switchable per pin | Fixed weak current source |
| Interrupt outputs | 2 (INTA, INTB) | 1 |
| Interrupt capture registers | Yes | No |
| Devices per bus | 8 | 8 |
Choose the MCP23017 when you need more than 8 pins, real push-pull outputs or a flexible interrupt system. Choose the PCF8574 when you need a few pins at minimum cost, for example on an LCD backpack.
Real gotchas
- Never leave RESET floating. It is the usual reason for a “device not found” error.
- Run an I2C scanner first. If 0x20 shows up, the wiring is correct.
- Watch the current limits. One pin can handle 25 mA, but the whole chip can take only 125 mA into VDD. Sixteen LEDs at 20 mA is 320 mA and is far over the limit. Use transistors or a driver chip for heavier loads.
- Keep the I2C wires short. Over 30 cm, or with several devices, add external 4.7 kOhm pull-ups to 3.3 V.
- Use OLAT for read-modify-write. When you change one output bit, read OLATA or OLATB and not GPIOA or GPIOB. GPIO shows the real pin state, which can differ from what you set when the pin is loaded.
Safety notes
Before you wire anything up
- Do not apply more than 5.5 V to the supply, or more than VDD plus 0.6 V to any other pin.
- Do not connect a 5 V signal to an ESP32 pin. Power the whole bus from 3.3 V.
- Do not drive relays, motors or solenoids straight from a pin. Use a driver or a transistor, with a flyback diode on inductive loads.
- Do not leave the address pins or RESET floating.
Conclusion
The MCP23017 adds 16 configurable pins to a board over two I2C wires, with per-pin pull-ups and interrupt-on-change. Tie RESET to 3.3 V, run an I2C scanner first, power the whole bus from 3.3 V and keep the total current under 125 mA into VDD.
More guides
Keep building.
From ESP32 HomeKit accessories to Arduino and ATtiny reference guides, there’s more where this came from.
All guides

