LED light chaser with MCP23017 on Raspberry Pi


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.





















Note : For Rev 2 board change on line

bus = smbus.SMBus(0)


bus = smbus.SMBus(1)

Parts List

Short URL:

Tags: , , , ,

28 Responses to LED light chaser with MCP23017 on Raspberry Pi

  1. Mr P Townsend on 21/08/2012 at 13:59

    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.

  2. admin on 22/08/2012 at 08:56

    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.

    • pt on 22/08/2012 at 09:06

      Thanks, I’ll try that.

  3. OutOfRange on 13/09/2012 at 21:03

    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

  4. Rich Kavanagh on 30/09/2012 at 20:00

    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?

    • Rich Kavanagh on 30/09/2012 at 20:04

      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?

  5. admin on 30/09/2012 at 20:02
  6. Rich Kavanagh on 30/09/2012 at 20:05

    Great, thanks for the confirmation, I’ll order now :)

  7. Rich Kavanagh on 13/10/2012 at 13:47

    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 :)

    • Rich Kavanagh on 13/10/2012 at 13:57

      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/

  8. Rich Kavanagh on 13/10/2012 at 15:52

    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 :)

  9. DJVS on 18/10/2012 at 18:40

    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 ?

    • Rich Kavanagh on 19/10/2012 at 21:11

      Are you on a Revision 2 Pi?

  10. Rich Kavanagh on 19/10/2012 at 21:12

    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?

  11. Rich Kavanagh on 06/11/2012 at 21:54

    I loved this project so much I made it permanent, here it is transferred to stripboard:


    Hope you like it :)

  12. ming on 12/11/2012 at 02:07

    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?

  13. canibalimao on 15/01/2013 at 15:22


    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.

  14. Pascal 0 on 18/01/2013 at 08:13

    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?

  15. jean Pierre on 07/02/2013 at 09:08

    I use the “C” source code for mcp 23008 with PCF8574 instead mcp23008 part and it run well. so
    I want use now mcp23017 with “C” language, I try to reuse and modifie the “C” code for mcp23008, but whithout succes :-( (( Let me now if you try that with succes??? or where I can find a good “C” source code for mcp 27017?

  16. David Hester on 06/03/2013 at 19:47

    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?

    • David Hester on 06/03/2013 at 19:52

      I have tried two different mcp23017 chips with the same result. Does the code from this site need to be changed?

  17. David Hester on 06/03/2013 at 21:25

    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.

    • David Hester on 06/03/2013 at 21:50

      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?

  18. Kitshwa on 01/02/2014 at 18:44

    Is there a way to use the 16-bit I/O Port Expander to control more than 16 LEDs?

  19. Herman Du Plessis on 12/03/2014 at 07:29

    I am completely new to raspberry pi and even microprosessors so please bear with me. I have been through most of steps successfully this far but got stuck at Errno 5. I replaced bus = smbus.SMBus(0) with bus = smbus.SMBus(1) and it still gives me an Errno 5 Input/output error. (line 16).

    Can anybody help me, please.

    • Herman Du Plessis on 12/03/2014 at 11:57

      running i2c detect still gives no values