Using the Microchip MCP23017 we can add another 16 IOs to the Raspberry Pi.
The Raspberry Pi has a limited number of IO pins but does have I2C and SPI. With the Microchip MCP23017 I2C expander this can be increase by 16.
The i2c driver must be installed first.
Documents
- Schematic
- MCP23017 Driver
- LED chaser Python file
Note : For Rev 2 board change on line
bus = smbus.SMBus(0)
to
bus = smbus.SMBus(1)


I have purchased the starter kit (B) for Raspberry Pi and am trying to build the LED light chaser with MCP23017, but I (as a complete novice) am confused with the schematic pin-out when I look at the photos supplied. The schematic would imply that the leds are connected along one side of the MCP23017, but the photos show they are connected with half on each side of the chip.
Could you please clarify the pin-out of the chip? I have tried to find the details on the web, but without success.
The schematic view is not the same as a physical view. The schematic is drawn like that so it make easier to visualise the connections. The important point is the pin number on the IC showing how it should be connect up in the physical world.
Think of a schematic is like a London underground Tube map. It is drawn like that so it is easy to read but in the real world it is layout different.
Thanks, I’ll try that.
Thank you very much for this tutorial
Got everything up and running in a few minutes, nice ! More I2C madness to come !!
Greetings from Switzerland
Great project! Can you just confirm, if I buy the Starter Kit-C (as I have other plans!) and the extra LEDs you mention, that’s all I need to build this project?
Ah, just noticed the Kit-C doesn’t come with a MCP23017 16-bit I2C IO Expander – but as they’re only £1.50 on your web site, can you confirm that if I buy Kit-C, plus the extra LEDs plus the MCP23017 16-bit I2C IO Expander, then I’m ready to go?
You would also need the MCP23017 IC.
http://www.skpang.co.uk/catalog/mcp23017-16bit-i2c-io-expander-for-raspberry-pi-p-1104.html
Great, thanks for the confirmation, I’ll order now
This doesn’t work on a Revision 2 Pi – any ideas why that might be?
I have this working perfectly on my Revision 1 Pi, but if I unplug it and hook it up to my Rev2 (using the same SD card), it doesn’t work at all.
Running “i2cdetect -y 0″ shows no device, where usually (on the Rev1 Pi) we’d see the grid showing “20″, but the results are all empty on the Rev2 Pi.
If I try and run ledchaser.py, I get the following error:
File “./ledchaser.py”, line 16, in
bus.write_byte_data(0×20,0×00,0×00) # Set all of bank A to outputs
IOError: [Errno 5] Input/output error
Any ideas why the “i2cdetect -y 0″ command can’t find the MCP23017 on a Rev2 board but it can on a Revision 1?
Or more importantly, anyone know how I can make this work on a Revision 2 Pi?
Thanks
Ok, I found out why this doesn’t work on a Rev2 board and more importantly, how to fix it
Edit your ledchaser.py file by typing:
sudo nano ./ledchaser.py
Find this line:
bus = smbus.SMBus(0) to bus = smbus.SMBus(0)
Replace it with this line:
bus = smbus.SMBus(0) to bus = smbus.SMBus(1)
CTRL+x to exit and save your changes.
What you’re essentially doing is altering the bus location of the MCP23017.
Thanks to Nathan Chantrell for the blog post at http://nathan.chantrell.net/20120602/raspberry-pi-io-expander-board/
Can anyone advise how best to build this on stripboard / veroboard?
I’ve got it working great on breadboard but not sure how best to transfer it to something more permanent.
Thanks
Use this Perma-Proto board:
http://www.skpang.co.uk/catalog/adafruit-permaproto-fullsized-breadboard-pcb-3-pack-p-1132.html
Wow! What a brilliant invention, that’s exactly what I need, thanks
Hi,
I have a problem connecting MCP23017 with 5V voltage… With this voltage Raspberry Pi doesn’t see it. With 3.3V MCP23017 works fine. Any suggestion what problem could be ?
Are you on a Revision 2 Pi?
I’ve been playing with the code for this project, just making different patterns, speeds, etc.
Does anyone know how many LEDs I can safely light at the same time?
I loved this project so much I made it permanent, here it is transferred to stripboard:
https://www.dropbox.com/sh/c45qvbw6h28gzoa/n1s-0MeT1Y
Hope you like it
Hi Great tutorial. Awesome, easy given all the steps you explained. Now I am trying to used the chip with http://www.adafruit.com/products/459 a bi-color bargraph. I am having trouble understanding how to wire it, and given that it is wired, how to I program it, so that I can “multiplex” if I am using the right words? Any suggestions? I know that I need 3 PNPs wired to the anodes, where does the 3rd leg of the transistor go? Sorry for the basic wording. And then once I got it wired, how do I program to select the correct anode and cathode pin to fire off a specific LED?
Hi,
I have already all the parts needed to do this, but now I want to put the python script working with push buttons, and not only leds.
How can I put the script reading the MCP23017 ports?
Hope anyone can help me.
I’ve build you project but if i try to read the MCP23017 IC i get different values but not 20 in the bank if i try using “i2cdetect -y 1″ , this option because i have Rev.2 Pi.
Everything is correct wired and i have 5V on it from a second usb adapter.
I have done this because on the power suply from the py itselfs i already have a connected a lcd and some buttons with leds so i’m consuming to mutch from the power suply to also suply the MCP23017 IC from that.
What can be the reason why the MCP23017 IC gives me differnt values instead of that 20 value?
I use the “C” source code for mcp 23008 with PCF8574 instead mcp23008 part and it run well. so
(( Let me now if you try that with succes??? or where I can find a good “C” source code for mcp 27017?
I want use now mcp23017 with “C” language, I try to reuse and modifie the “C” code for mcp23008, but whithout succes
I’ve built this project and downloaded the script but I can only get 8 leds to work, connected to pins 1,-8. Any ideas what I am doing wrong?
I have tried two different mcp23017 chips with the same result. Does the code from this site need to be changed?
I put the second LEDs on a different breadboard and now they work, but I now can’t get the LED connected to GPB0 to work. I have changed the wire, resistor and LED and tried placing it on different parts of the breadboard, i have resited the mcp23017 aswell. Now I am truly stumped.
I can “manually” get the led to light by using some python code from another site (nathan chantrel) so i now the chip works and my wiring is ok. Has anyone else had all 16 leds work?