Selecting second I2C device

I have two I2C port expanders, MCP23017. I can write out to both with arrays that display the LED pattern I want from each of the expanders. I’m using the Centipede library although I’m not using the Centipede device (with 4 port expanders on it). I like the portWrite command that lets me write 16 bits at one time. This part works. ( 16 LEDs are attached to the outputs of the port expanders).

In another snippet of code (not shown), I use a For loop to write out individual bits to one of the 23017s, basically a light chaser/sequencer. Works well. I wrote the For loop as a function and used it to make it work.

Now, the clincher. I can’t get the OTHER MCP23017 to work with the same function, as shown in attached code. Like I said above, I can address both when I’m using arrays, but using the same techniques, CANNOT get two devices to do sequencing.

Attached is program in it’s entirety…it’s a work in progress so forgive some of my errant comments or code. I do lots of cut and paste from other working code to save time.

In the loop at the bottom I’ve commented out the device that works at address 0x20 (Centipede uses just the digit 0). The NEXT one commented out for device at 0x21 (Centipede “1”) does not work: all lights go on and never go off.

Finally the code that is NOT commented out is my attempt to use the Wire library to select device 21. No lights go on at all in this attempt.

Ideas? Suggestions?

Spiral2PortsAllLIghts.ino (1.01 KB)

The centipede library does the whole I2C handling, you must not interfere with it. To select the second chip just add 16 to the pin number and send it to CS.digitalWrite() and the library automatically selects the second chip.

16 hex or 16 base 10? I’ll try both. I’ve been to Centipede site many times, and they didn’t really explain how to address the other chips on their board.

16 hex is written 0x16, 16 without a prefix is always base 10.

I've been to Centipede site many times, and they didn't really explain how to address the other chips on their board.

It's often just easier to read the code :-).