MCP23008 multiple libraries

So there are 3 published libraries for the MCP23008, two of them seem to be separate efforts to port the adafruit library with pretty much identical results and the 3rd (arguably structured in a nicer fashion) built from the ground up by @rikkas7. The adafruit libraries currently lack support for the interrupt pin and the -RK versions (when I last checked) have neither support for the interrupts or reading all 8 pins at once.

I noticed the Adafruit MCP23017 does have the interrupt features so I cut and pasted those into a local copy, ripped out the port selecting parts and behold that seems to work, its not exactly elegant as the 23017 and 23008 do not appear to be written in the same style despite headers claiming the same author(s).

So I guess the question is do any of those in charge of maintaining those libraries want my sloppy hack as you could literally fit what I know about submitting such things via Github on the head of a pin?

Secondary question, if the interrupt feature gets put into the Adafruit libraries, would it also make its way into the I2C display library that uses copy of those same files?

Seems wise just to post what I did, currently I think there’s now some code missing that handles invalid pin numbers…
… shortly…

The reason the MCP23008-RK library doesn’t support interrupts is that handling it is kind of a problem. While adding the interrupt handler itself is easy, almost certainly the next thing you’ll want to do is query the MCP23008. The problem is that the I2C library is not reentrant, and if the main loop code happens to be making an I2C call when the MCP23008 interrupt happens, bad things will happen.

It’s possible to work around this either by dispatching to the loop, or guarding all of the I2C accesses with some sort of semaphore, but this is a pain to do from a library.

The multiple value read would be easy to add, but the API for MCP23008-RK basically emulates the built-in GPIO calls, and there isn’t a call to read multiple built-in GPIO lines. It would be easy to add, however.


Makes sense, I’m using the interrupt for a fairly specific purpose - waking the Photon up from sleep, so conveniently sidestep all the issues you describe as I don’t need to query it. Using it as a true interrupt against running code however does indeed introduce a few questions. But is that truly a problem the library should be addressing or it something the HAL or Wire library should understand?

1 Like


Thank you for this explanation. I was wondering why there is no support for the Interrupt functionality. Bummer but I understand.

Quick, question. Is there an analog equivalent to the MCP23008? I know you included an i2c ADC in your tutorial but was looking for ADC expansion as well.

Thanks, Chip

No you need a digital interface like I2C to be able to select which channel you are reading, howver there are lots and lots of multi-channel I2C ADCs that can offer you more or better analogue inputs.

OK I’ve been AWOL a while, the project I was working on had something of a hiatus having reached point where in theory it needed just a little polish but was otherwise finished. It had one issue, it didn’t seem to work very well if at all on anything beyond 0.6.3. I couldn’t establish what was upsetting it but since then Particle has accelerated to 1.4.4 and it seems dangerous to be reliant on something that old which with the best will in the world could easily meet a similar fate to Mesh wrt support in future.
So anyway as to why I am appending this here…
The project now requires reviving, my code on a Photon on 1.4.4 seems perfectly happy. On an electron it instantly goes to a solid white LED and doesn’t run at all.
Our project uses two MCP23008s, one controlling an LCD display and the other a membrane keypad with some status LED’s. To handle this we use the Adafruit_MC23008 library that is included in the I2C display library. As detailed in my previous posts this library is slightly tweaked to add support for the interrupt pin which we attach to the photon wake pin (basically copied out of the 23017 library).
They keypad code simply replaces the analogue read code in a library called analogbuttons for arduino with a 7bit value read off the IO expander. I removed all functionality from the code and left only putting something on the display and reading the keypad IO ports, at which point I could pinpoint the issue being they keypad/2nd use of the MCP23008 library being the issue.

So I guess the question is did versions of ParticleOS handle I2C differently in some way, does the Electron toolchain compile/link slightly differently to the Photon one or diverge somehow from 0.7 onwards? It seems weird that it seemingly runs fine on the one and not at all on the other.

Chip - for an ADC with I2C I use a MAX11612 with 4 channels if you use the internal reference voltage. There are other versions with more channels.


1 Like

@chipmc, there is also the 16-bit ADS1115 which has a bunch of other nice features.

1 Like

@armor, Is there a Particle library for the MAX11612?

Thanks, Chip