Python Usage

Make sure you've installed the bonnet onto your single board Linux computer by plugging it in and then restarting.

You will need to have I2C set up and activated - here's how to do it for Raspberry Pi.

Once that's done and the bonnet installed, board rebooted, you should be able to I2C scan to find the device with something like sudo i2c-detect -y 1 (the number may be different on non-Raspi computers)

The default address for the bonnet is 0x20

Python Installation of MCP23017 Library

You can use this Bonnet with Python and Raspberry Pi thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility 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:

  • sudo pip3 install adafruit-circuitpython-mcp230xx

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

Python Usage

To demonstrate the usage, we'll use Python code to read a button and light up an LED from the Python REPL.

Wire up the bonnet as follows:

  • LED anode through a resistor (220 to 4.7Kohm) to GPA0
  • LED cathode (short leg) to one of the GND pads (any)
  • One side of switch to GPA1
  • Other side of switch to one of the GND pads (any)

And start up Python3

First you'll need to import the necessary modules, initialize the I2C bus for your board, and create an instance of the class.

Download: file
import time
import board
import busio
import digitalio
from adafruit_mcp230xx.mcp23017 import MCP23017

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

mcp = MCP23017(i2c)  # MCP23017

Now you have the device called mcp, you can use that to 'create' DigitalInOut pins, by calling get_pin. The pins are ordered 0-15. Pin #0 is GPA0, #1 is GPA1, #8 is GPB0, #15 is GPB7

Download: file
pin0 = mcp.get_pin(0)  # GPA0
pin1 = mcp.get_pin(1)  # GPA1

Now you can treat these like regular digital inputs and outputs. Set GPA0 to be an output and level high voltage.

The LED should turn on!

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

And set GPA1 to be an input with a pullup

Download: file
# 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

Finally we can have a loop where we read and write data to the pins

Download: file
    # 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))
  

The LED should start blinking. You can also press the button to see the output change for the pin 1 value:

Type Control-C to quit. Then try this code that will light up the LED when the button is pressed:

Download: file
# Now loop setting the LED when the button is pressed
while True:
    pin0.value = not pin1.value

That's pretty much all you need to get started with the bonnet!

The fully commented example is here:

This full sketch uses the MCP23008 by default, change the "mcp = adafruit_mcp230xx.MCP23008" line to the MCP23017 before running this on the bonnet!
# 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))

 We have more examples, including interrupt detection so you don't have to constantly query the pin values for buttons. Visit https://github.com/adafruit/Adafruit_CircuitPython_MCP230xx/tree/master/examples to see all the examples

Some of the examples may be set up for an MCP23008 instead of MCP23017 so be sure to update the objection creation at the top!

This guide was first published on Mar 06, 2019. It was last updated on Mar 06, 2019. This page (Python Usage) was last updated on Nov 20, 2019.