Mcp23008 - Anyone attempted this i2c expander?

I picked up a mcp23008 that’ll be here Monday. It’s an i2c gpio expander. I was curious if anyone had attempted this with the Electric Imp yet and how did it go?

I’m not thinking there should be an issue and I’m hoping I can simply mod the sx1509 implementation example on the wiki.

Here is a link to some info if anyone is interested. Any help would be greatly appreciated!!

http://ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf

Yeah, that one should be pretty easy. You may want to start from scratch though, the SX1509 is more complex.

Rockin!! Thanks Hugo!

I also have some MCP23008’s in the mail… will post code as soon as I have a working example.

I wrote a library for the MCP23017:

Should be useable for the MCP23008 with very minor changes.

Excellent thanks!

I forked quirky’s library, and made the changes to create a separate class for the MCP23008. You would normally not mix these devices in one project, so a separate class is more code efficient than a subclass or polymorphic class (if that’s even possible in Squirrel).

I have not tested it yet, because I’m still waiting for my parts to arrive…

I tested the code above just for the basic input/output functionality, and it works ok. However, I took another approach in a new set of classes which emulate the functionality of the standard Pin class. This makes it easy to move pins from the electric imp’s native pins to i/o expander pins with minimal code changes.
For example:
`
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ)
port <- MCP23008(hardware.i2c89, 0)
led <- port.pin1 // or hardware.pin1
led.configure(DIGITAL_OUT)

function blink() {
imp.wakeup(0.5, blink)
led.write(1 - led.read())
}

blink()`

You can find the full code here: https://gist.github.com/marcboon/5606961

That is actually really sweet and how it should be done. Bravo :slight_smile:

Nice I will use it! How about the bigger chip above?

I have created a unified set of classes which handle both the MCP23008 and MCP23017. After creating one or more instances of either class, user code should only access member functions of the MCP230xxPin class, ie configure(), read(), and write(), which are compatible with the standard Pin class.

For example:
`port1 <- MCP23008(hardware.i2c12, 0) // 8-bit port pinstrapped to device address 0
port2 <- MCP23017(hardware.i2c12, 1) // 16-bit port pinstrapped to device address 1

button <- port1.pin1 // MCP23008 has pin1 to pin8
led <- port2.A.pin6 // MCP23017 has A and B ports, each having pin1 to pin8

button.configure(DIGITAL_IN_PULLUP)
led.configure(DIGITAL_OUT)
`

See the demo at the bottom of the code for another example:

If anyone wonders how to phisically connect the mcp23008 to the electric imp…

Very cool!

Thanks a bunch! Saved me a huge headache!

Hi, I am pretty sure this is a very simple question. I hooked my MCP23017 and was trying to use the codes given by you guys; however, I do not think I know how to use the class for MCP23017. I just simple want to get blink LEDs via my MCP23017 chip. Could anyone please help me out in this?

I am playing around with a MCP23008, and I am going to try and drive some 7 segment displays with a few of those.

Right now I use some TIP120 in a TO-220 package to drive the leds while just playing around, but what would be more sensible to use for the final thing? ULN2803ADW maybe?

Also, my plan is to make a wall clock, and the pcb’s holding the components should just be a set of the same boards. So my plan is to make it so it can hold an imp and the components needed to run it, MCP23008, resistors needed for the leds and some kind of transistor array/bunch of transistors to drive the leds.

The thing I am wondering about too, is how I set the address of a MCP23008? My idea is to just have one board with the imp on, then on the other boards just leave out the components for the imp, and just have the shift register and the components needed to drive the display, then just address each of those.

@MikeyDK, I’m (indirectly) using the MCP23008 for a wall clock too. I say ‘indirectly’ because it’s the part that Adafruit uses in its I²C backpack for character LCDs, one of which I’m using for an ancillary display. You can change the backpack’s I²C by connecting pads on the board and these connect directly to MCP23008 pins. So I guess if you’re using the raw chip, you’ll do that with jumper wires.

Do you have a link to the one you use? Should be possible to see from the schematic how it is done, since I failed in understanding that from the datasheet.

Here’s the product: http://www.adafruit.com/products/292

And here’s their MCP23008 library: https://github.com/adafruit/Adafruit-MCP23008-library