Skip to content
My Account

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
No ratings yet, be the first.

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

1. What you needAn MCP23017 breakout (here the MCU-2317), an ESP32 board, jumper wires, an LED with a resistor and a button.
2. Wire it upFollow the section “Connecting to the ESP32-C6 Super Mini”. Tie RESET to 3.3 V, never leave it floating.
3. Scan and configureAn I2C scanner should show address 0x20. If it shows nothing, check RESET first. After power-up all 16 pins are inputs, so set IODIRA and IODIRB to choose which ones become outputs.

Specifications

ItemValue
I/O pins16 (2 x 8-bit ports)
InterfaceI2C (MCP23017) or SPI (MCP23S17, up to 10 MHz)
I2C speed100 kHz, 400 kHz and 1.7 MHz
I2C addresses0x20 to 0x27 (three address pins)
Supply voltage1.8 to 5.5 V, see the note below
Maximum current per pin25 mA sunk or sourced
Maximum current into the VDD pin125 mA
Maximum current out of the VSS pin150 mA
Total power dissipation700 mW
Internal pull-upsWeak, 40 to 115 µA at 5 V, per pin selectable
Standby current1 µA maximum
Package of the chip28 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.

MCU-2317 MCP23017 breakout pinout, component side, with the functions of the single row and the double row

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.

MCU-2317 MCP23017 breakout pinout, back, with the printed labels and the functions of every pin

Pins at a glance

The pins grouped by function.

MCU-2317 pins grouped by function: power, I2C, address, control and interrupts, PORT A, PORT B and the SPI-only pins
Advertisement

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.

A2A1A0Address
GNDGNDGND0x20
GNDGNDVCC0x21
GNDVCCGND0x22
GNDVCCVCC0x23
VCCGNDGND0x24
VCCGNDVCC0x25
VCCVCCGND0x26
VCCVCCVCC0x27

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.

FunctionAvailable?Alternative
Digital inputYes
Digital outputYes
PWMNoESP32 LEDC pins or a PCA9685
ADC (analog input)NoESP32 ADC pins or an ADS1115
DACNoMCP4725
High-speed switchingLimited by the I2C busNative ESP32 GPIO

It suits buttons, switches, relays (through a driver), LEDs, rotary encoders and keypad matrices.

Connecting to the ESP32-C6 Super Mini

MCP23017 moduleESP32-C6 Super Mini
VCC3V3
GNDGND
SDA/SIGPIO 6
SCL/SCKGPIO 7
RESET3V3
A0, A1, A2GND
  • 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:

RegisterAddress (A / B)Function
IODIRA / IODIRB0x00 / 0x01Direction: 1 = input, 0 = output
IPOLA / IPOLB0x02 / 0x03Input polarity inversion
GPINTENA / GPINTENB0x04 / 0x05Interrupt-on-change enable
DEFVALA / DEFVALB0x06 / 0x07Default compare value
INTCONA / INTCONB0x08 / 0x09Interrupt control: compare to DEFVAL or to the previous value
IOCON0x0A and 0x0BConfiguration. One register that answers on both addresses
GPPUA / GPPUB0x0C / 0x0DPull-up enable
INTFA / INTFB0x0E / 0x0FInterrupt flag: which pin fired
INTCAPA / INTCAPB0x10 / 0x11Pin state captured at the interrupt
GPIOA / GPIOB0x12 / 0x13Read the pins or write the port
OLATA / OLATB0x14 / 0x15Output 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, &reg, 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.

Advertisement

Interrupts

Polling 16 inputs wastes bus time. Interrupt-on-change avoids this:

  1. Enable the pins you care about in GPINTENA and GPINTENB.
  2. Choose the trigger in INTCON: a change from the previous value, or a difference from DEFVAL.
  3. Connect INTA and INTB to free GPIO pins on the ESP32, or set the MIRROR bit in IOCON to combine both on one pin.
  4. Choose in IOCON whether the interrupt pin is active-high, active-low (INTPOL) or open-drain (ODR).
  5. 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

FeatureMCP23017PCF8574
I/O pins168
I2C speedup to 1.7 MHz100 kHz or 400 kHz, depending on the manufacturer
Direction registerYesNo (quasi-bidirectional)
Pull-upsSwitchable per pinFixed weak current source
Interrupt outputs2 (INTA, INTB)1
Interrupt capture registersYesNo
Devices per bus88

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

  1. Never leave RESET floating. It is the usual reason for a “device not found” error.
  2. Run an I2C scanner first. If 0x20 shows up, the wiring is correct.
  3. 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.
  4. Keep the I2C wires short. Over 30 cm, or with several devices, add external 4.7 kOhm pull-ups to 3.3 V.
  5. 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.

Advertisement
All guides

More guides

Keep building.

From ESP32 HomeKit accessories to Arduino and ATtiny reference guides, there’s more where this came from.

All guides