It's easy to use the Adafruit MCP23017 with Python or CircuitPython with the Adafruit CircuitPython MCP23017 module. This module allows you to easily write Python code that adds up to 16 inputs or outputs over I2C.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a MCP23017 to your board exactly as shown below. Here's an example of wiring a Feather M4, a button and an LED to the expander using a solderless breadboard:

MCP23017

  • Board 3V 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)

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi, a button and an LED wired to the expander using a solderless breadboard:

MCP23017

  • Pi 3V to expander VIN (red wire)
  • Pi GND to expander GND (black wire)
  • Pi SCL to expander SCL (yellow wire)
  • Pi SDA to expander SDA (blue wire)

LED

  • LED + to 470Ω resistor
  • LED - to Pi GND (black wire)
  • 470Ω resistor to expander A0 (pink wire)

Button

  • Button leg to expander A1 (green wire)
  • Opposite button leg to Pi GND (black wire)

Python Installation of MCP230xx Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • pip3 install adafruit-circuitpython-mcp230xx

If your default Python is version 3, you may need to run pip instead. Make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython Usage

To use with CircuitPython, you need to first install the MCP230xx library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

Your CIRCUITPY/lib folder should contain the following folders:

  • adafruit_bus_device/
  • adafruit_mcp230xx/
cp

Before running the code, comment out the adafruit_mcp230xx.mcp23008 library and uncomment the adafruit_mcp230xx.mcp23017 library to import the correct library for the MCP23017.

# from adafruit_mcp230xx.mcp23008 import MCP23008

from adafruit_mcp230xx.mcp23017 import MCP23017

Then, comment out the mcp instance using the MCP23008 class, and uncomment the mcp instance using the MCP23017 class.

# Create an instance of either the MCP23008 or MCP23017 class depending on
# which chip you're using:
# mcp = MCP23008(i2c)  # MCP23008
mcp = MCP23017(i2c)  # MCP23017

Python Usage

Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:

python3 code.py

Example Code

# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Simple demo of reading and writing the digital I/O of the MCP2300xx as if
# they were native CircuitPython digital inputs/outputs.
# Author: Tony DiCola
import time

import board
import busio
import digitalio

from adafruit_mcp230xx.mcp23008 import MCP23008

# from adafruit_mcp230xx.mcp23017 import MCP23017


# Initialize the I2C bus:
i2c = busio.I2C(board.SCL, board.SDA)

# Create an instance of either the MCP23008 or MCP23017 class depending on
# which chip you're using:
mcp = MCP23008(i2c)  # MCP23008
# mcp = MCP23017(i2c)  # MCP23017

# Optionally change the address of the device if you set any of the A0, A1, A2
# pins.  Specify the new address with a keyword parameter:
# mcp = MCP23017(i2c, address=0x21)  # MCP23017 w/ A0 set

# Now call the get_pin function to get an instance of a pin on the chip.
# This instance will act just like a digitalio.DigitalInOut class instance
# and has all the same properties and methods (except you can't set pull-down
# resistors, only pull-up!).  For the MCP23008 you specify a pin number from 0
# to 7 for the GP0...GP7 pins.  For the MCP23017 you specify a pin number from
# 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4).
pin0 = mcp.get_pin(0)
pin1 = mcp.get_pin(1)

# Setup pin0 as an output that's at a high logic level.
pin0.switch_to_output(value=True)

# Setup pin1 as an input with a pull-up resistor enabled.  Notice you can also
# use properties to change this state.
pin1.direction = digitalio.Direction.INPUT
pin1.pull = digitalio.Pull.UP

# Now loop blinking the pin 0 output and reading the state of pin 1 input.
while True:
    # Blink pin 0 on and then off.
    pin0.value = True
    time.sleep(0.5)
    pin0.value = False
    time.sleep(0.5)
    # Read pin 1 and print its state.
    print("Pin 1 is at a high level: {0}".format(pin1.value))

If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!

If running Python: The console output will appear wherever you are running Python.

You will see the connected LED begin to blink on and off.

When you press the button, its status will be printed to the REPL.

That's all there is to using the MCP23017 with CircuitPython!

This guide was first published on Mar 23, 2022. It was last updated on 2022-03-23 13:02:11 -0400.

This page (Python & CircuitPython) was last updated on May 28, 2022.

Text editor powered by tinymce.