Trouble with the MCP23xxx module

Posted on
  • Hi there!

    I'm trying to interface and Espruino Pico with an MCP23017 and I'm running into a little trouble with the I2C communication (I2C2, pins B10 & B3, MCP device address 0x20)

    The error I'm getting is as follows:

    Uncaught InternalError: Timeout on I2C Write Transmit Mode 2
     at line 1 col 43

    I've tried manually reading data from the MCP chip and that works as far as I can tell, my original assumption was incorrect wiring but I've been over half a dozen times and everything looks sound, 10k pullup resistors included on the i2c lines etc, and I can read data from the chip.

    The problem only seems to occur when something tries to write to the chip, I've tested two different chips both giving the same error, does anyone think they could lend me a hand with this? It'll likely be something forehead-slappingly simple I've overlooked but on the off chance it isn't, I figured I'd post here



  • I did quite extensive work on the MCP23017... the only challenge I faced was a spike when I added interrupt extension caused by Espruino pin mode change. This is now though fixed (1v95). I started with the available module to learn the MCP23017. Wading through the code made me though apply some changes... last but not least because I wanted add the interrupt functionality.

    Take a look at Exploring adding setWatch/clearWatch (inerrupt handling) to MCP23017 (MCP2308) Portexpander Ports. It includes a rewrite of the module. You can ignore the interrupt part...

  • Maybe you could post your full code? Could you try setting the address to 0? The module seems to automatically add 0x20 to the I2C address, so it's expecting just a number between 0 and 7 - if that's the issue I'll update the docs

  • Updating to 1v95 is a good shout, as is setting the address to 0 instead of 0x20, I've tried setting the address to 0 (along with 1 to 7 for make sure I'm not on the wrong address) but I'm still getting the same error unfortunately. I've just updated to 1v95 this moment and re-tested and I'm getting the same issue, full code is below, it's pulled almost verbatim from the example:


    var i2c=I2C2;
    i2c.setup({scl: B10, sda: B3});
    var address = 0;
    var RST=A8;
    var port=require("MCP23017").connect(i2c,RST­,address);


    Uncaught InternalError: Timeout on I2C Write Transmit Mode 2
     at line 1 col 42
    in function "s" called from line 1 col 128
    in function "m" called from line 1 col 18
    in function "mode" called from line 1 col 28
    Uncaught InternalError: Timeout on I2C Write Transmit Mode 2
     at line 1 col 29
    this.i2c.writeTo(this.i2ca,c);return this.i2c.readFrom(this....
    in function "r" called from line 1 col 53
    ...8:this.b;return this.p.r(18)[255<this.b?1:0]&a?1:0
    in function "read" called from line 1 col 26

    I've also attached a potato-quality photo of my wiring, note the reset pin is not connected - I've tried it both with and without, each result in the same error

    1 Attachment

    • image_uploaded_from_ios_720.png
  • The wiring all looks fine to me - and you're sure it's a MCP23017 and not a MCP23S17 (the SPI one?).

    You don't seem to have wired up the reset wire to A8 though - you could give that a try.

    Other thing you could try is swapping var i2c=I2C2; to var i2c=new I2C(); - which will use software I2C instead of hardware. It shouldn't make a difference, but it's possible it would help.

  • Definitely 23017 not 23S17, I checked that last night before posting to make sure I'd covered as many potentially silly issues as possible :).

    Tried the software I2C trick, changed from I2C2 to new I2C() and everything worked perfectly. Then I tried changing back to hardware I2C and it worked perfectly too, so then I tried power-cycling and trying hardware I2C and it still works - I can't really explain what fixed the issue, perhaps establishing a solid software I2C link kicked something in the MCP chip and got it in a good state, but it's working now.

    I'll keep an eye on it and monitor for any instability, thanks for your help :)

  • Okay, to keep you posted, issue is re-occurring now after waiting for 5 minutes and re-sending the hardware sketch to the pico on I2C2 pins B10 & B8 - Out of the blue I decided to try I2C1 pins B8 and B9 and they seem to work fine

  • It could actually just have been a bad connection on one of the pins? These things happen, especially with breadboard :)

  • Damn, thought we had this sorted but I've come across some other weirdness -_- First off, thanks again for all your help, I know everyone here is busy so I appreciate the time. Second, I've come across a couple of issues interfacing with two MCP23017s, one on address 0b0000101 and one on address 0b0000111, I'll list them below:

    1) Address 0b0000000 fails to connect for some reason.
    2) The 'writePort()' command doesn't seem to work, I've tested it with multiple values (0, 1, 0b0000000000000000, 0b1111111111111111) to no effect.
    3) Port A on address 0b0000101 fails to read and write, tested with two chips in multiple places on the breadboard.
    4) Writing to pins on the expanders intermittently fails for long periods, then suddenly starts working again after messing about with code and not moving the physical breadboard.
    5) When reading/writing to the chips at sub-500ms intervals, after a short time the I2C bus packs up and gives a 'Uncaught InternalError: Timeout on I2C Write Transmit Mode 2' error. The bus occasionally recovers and begins re-scanning after a couple of minutes but usually stays down.

    I'm almost certain there's something underpinning all of this weirdness, but it feels like I'm working in the dark in terms of debugging - things seem to be happening at random



  • Going back to a single chip, do you still have instability?

    I'm not sure what to suggest really. Perhaps you could try putting a capacitor across the MCP23017's power pins just in case there are some fluctuations on the power rail?

    The bitrate should default to 50000 so that shouldn't be an issue, but I guess you could try going faster with bitrate:100000 in I2C.setup (or more) and see if that helps somehow.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Trouble with the MCP23xxx module

Posted by Avatar for 27percent @27percent