Using multiple IO expanders in Arrays

Hi, I'm very new to Arduino so there may seem like an obvious answer to this question but I'm having trouble addressing pins on an IO expander in an array.

I am using multiple MCP23017 -E/SP chips in a cable continuity testing project. I am trying to create an array that lists the pins of these IO expanders however I'm not sure how to call them up.

I have got the chips working and addressed correctly but it seems to be more of a syntax issue I'm having with this particular problem.

#include "Adafruit_MCP23X17.h"

Adafruit_MCP23X17 mcp1;
Adafruit_MCP23X17 mcp2;
Adafruit_MCP23X17 mcp3;
Adafruit_MCP23X17 mcp4;
Adafruit_MCP23X17 mcp5;
Adafruit_MCP23X17 mcp6;
Adafruit_MCP23X17 mcp7;
Adafruit_MCP23X17 mcp8;


const byte pinsCableBegin[]= { mcp1(0), mcp2(3), 4, 5, 6, 7, 8, 9,10 };

void setup() { ...

For example, if I want to reference GPIO pin 3 on mcp2 in my array, I am unsure how to write that into the software. I have put some code above of what I assumed it would be [mcp2(3)] but this obviously doesn't compile and I wondered if someone knew how I should write this so that the array looks at the correct pin.

Any help would be greatly appreciated.

just create an array of objects (like already shown) and then address the MCP and pin.

#include <Adafruit_MCP23X17.h>

constexpr uint8_t noOfMcp {8};
Adafruit_MCP23X17 mcp[noOfMcp];

void setup() {
  Serial.begin(115200);
  Serial.println("MCP23xxx array test");

  for (int i = 0; i < noOfMcp; i++) {
    if (!mcp[i].begin_I2C(0x20 + i)) {
      Serial.print(i); Serial.println(" error");
    }
    mcp[i].pinMode(0, OUTPUT);
  }

  mcp[1].pinMode(2, OUTPUT);    // second MCP, third pin
  mcp[1].digitalWrite(2, HIGH); 
}

void loop() {
  for (int i = 0; i < noOfMcp; i++) {
    mcp[i].pinMode(0, HIGH);
  }
  delay(500);
  for (int i = 0; i < noOfMcp; i++) {
    mcp[i].pinMode(0, LOW);
  }
  delay(500);
}
//

Thank you for all the help.

I have tried this code but cant quite get it working but I will keep messing around with it in the meantime.

I had an idea which might make this easier as well but just wanted to verify if this is possible.

I am trying to check continuity between all the pins across the 8 IO expanders which is 128 different pins in total

Is it possible to define an imaginary set of pins that correspond to all 128 IO expansion pins for example,

#define 100 mcp1(0)
#define 101 mcp1(1)
#define 102 mcp1(2)
#define 103 mcp1(3)
#define 104 mcp1(4)
#define 105 mcp1(5)
#define 106 mcp1(6)
#define 107 mcp1(7)
#define 108 mcp1(8)
#define 109 mcp1(9)
#define 110 mcp1(10)
#define 111 mcp1(11)
#define 112 mcp1(12)
#define 113 mcp1(13)
#define 114 mcp1(14)
#define 115 mcp1(15)
#define 116 mcp1(16)
#define 117 mcp1(17)
#define 118 mcp2(0)
#define 119 mcp2(1)
#define 120 mcp2(2)
#define 121 mcp2(3)

...

#define 225 mcp8(15)
#define 226 mcp8(16)
#define 227 mcp8(17)


const byte pinsCableBegin[]= { 100, 101, 102, 103, 104, 105,... 227 };

This would make writing the pins into an array much easier as they would all be successive but I just wanted to know if this was possible before wasting my time trying to make it happen?

Thank you again!

describe exactly what is not working.

  • What do you see/get?
  • What do you await instead?
  • How does your schematic look like?
  • Post real pictures of your setup so we can see each and every connection.

I have tried compiling and running this in the IDE but the serial monitor only returns:

��me���MCP23est
2 error
3 error
4 error
5 error
6 error
7 error
�����

I have attached how the chips are wired with the diagram and I'm obviously just using different addresses for the different chips but I don't really understand what the code is doing.
Pinout1

I do not have a schematic for this I just wish to be able to address the IO expansion pins on the chips in a similar manner to that of the Arduino board pins.

For examples sake you could imagine that I want to run a continuity test of a cable with 2 ends where each connector has 60 pins so I need 120 total IO and I will be using the IO on the expansion boards to do this test.

The cable would have simple end to end connections e.g. pin 1 on connector 1 goes to pin 2 on connector 2 and so on but I just need to be able to call up the IO expansion pins in my array to be able to do this.

what code are you using? Obvious it's not my code from #3 because even the first line doesn't fit to my example.

Post your code!
Also state, what other result on Serial you would have expected to get!

This is the code I'm using:

#include <Adafruit_MCP23X17.h>

constexpr uint8_t noOfMcp {8};
Adafruit_MCP23X17 mcp[noOfMcp];

void setup() {
  Serial.begin(115200);
  Serial.println("MCP23xxx array test");

  for (int i = 0; i < noOfMcp; i++) {
    if (!mcp[i].begin_I2C(0x20 + i)) {
      Serial.print(i); Serial.println(" error");
    }
    mcp[i].pinMode(0, OUTPUT);
  }

  mcp[1].pinMode(2, OUTPUT);    // second MCP, third pin
  mcp[1].digitalWrite(2, HIGH); 
}

void loop() {
  for (int i = 0; i < noOfMcp; i++) {
    mcp[i].pinMode(0, HIGH);
  }
  delay(500);
  for (int i = 0; i < noOfMcp; i++) {
    mcp[i].pinMode(0, LOW);
  }
  delay(500);
}
//

I believe this is your code and I have tried to run it twice but I just get this on the output:

MCP23xxx arra���gL�#MCP23xxx array test
2 rror
4 error
5 error
6 error
7 error
��me���MCP23est
2 error
3 error
4 error
5 error
6 error
7 error
�����

I was expecting no errors on the Output but I'm not sure what its supposed to be doing.

Regardless, I didn't know if it was possible to do something as I suggested in #4 ?

that's why I asked for a schematic and for real pictures.

The sketch tries to start 8 (eight!) ICs - it seems only the first two are working. Hence get for errors for 2 - 7 .

There is no further output in the sketch - don't expect more information on the Serial.

Step 1: let's clarify your hardware.
including why your serial output seems to be incomplete.

Thanks for clarifying, it makes alot more sense now.

I only had 2 ICs connected when I ran that test hence the errors detected on the other 6.

I will add the other 6 and retest then get back to you if I still cant integrate it with my circuit.

Thank you again!