Skip to main content

Interfacing MCP23017 with Raspberry Pi

Project description


Library for Raspberry interfacing with MCP23017 (e.g. on a shield)


Created with use of

MCP23017 can be used with bank=0 or bank=1 - what you prefer.

16-bit mode (2-byte write/read) is NOT SUPPORTED at the moment.
The focus is on a more generic interface which supports interrupts and (perhaps) the 8-bit MCP chips also.



quick2wire.i2c (



MCP23017 class
MCP23017 class supports all low level configuration like accessing IOCON register, reading and writing any registers

- Initialize MCP23017 with address
- Optionally configure your Bank bit in constructor (default: BANK = 0)
- Read from specific Register
- Write to specific register
- Setting a specific Bit in IOCON
- Unsetting a specific Bit in IOCON

- Generating of two 8-bit-Port-Objects for a chip

Port Manager Class

Port manager supports more high level operations.

PortManager supplies the following methods to configure and work with an 8-bit-port (like Arduino lib on with more features)

*Set I/O mode*

*Set Pullups*

*Set Input Invert*

*Interrupt enabling*

*Set Inerrupt compare*

*Define Interrupt compare*

- Reading and writing

- Additionally it can handle a interrupt callback by passing a method:


Executing the module
Reading the chip on address 0x20
sudo ./ 32

Simple polling example

from MCP23017 import MCP23017, PortManager, IOCON

#create chip with bank=1 mode on address 0x20
chip1 = MCP23017(0x20, 1)
#create chip with bank=0 mode on address 0x21
chip2 = MCP23017(0x21, 1)

#set some config bits on chip1

#generate ports for chip1 - takes always a dict with keys 'A' and 'B' and the desired RPi GPIO interrupt pins
ports = chip1.generate_ports({'A':4, 'B':17})

#set ports of chip1 as input pins
for name,port in ports.items():
print(" Setting up port "+name)
#Set port to input pin

while True:
for name,port in ports.items():
print(" Reading port "+name)


for setting up callbacks see also

- TODO: implement 16 bit mode, affects:
- read() method
- write() method
- interrupt mirrors setting(?)

Project details

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page