How to use Multiple mcp23017 chips with the adafruit mcp23017.h library
Every chip must have unique address, and this is doable according to Microchip's manual page 8. So, first of all, you should setup different addresses in your hardware layout.
You should also create an Adafruit_MCP23017
object for every chip you want to use, and setup corresponding addresses in your code.
In this case pins of all chips will have addresses in range 0-15. To change pin's state you should reference to particular instance.
Update
This is starting point for you
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
Adafruit_MCP23017 mcp3;
#define addr1 0x00
#define addr2 0x01
#define addr3 0x02
void setup() {
mcp1.begin(addr1);
mcp2.begin(addr2);
mcp3.begin(addr3);
}
void loop() {
}
I share my solution with 7 MCP23017 chip , 7x14 pins=98 pins using adafruit library.
Connections:
addr 0 = A2 low , A1 low , A0 low 000
addr 1 = A2 low , A1 low , A0 high 001
addr 2 = A2 low , A1 high , A0 low 010
addr 3 = A2 low , A1 high , A0 high 011
addr 4 = A2 high , A1 low , A0 low 100
addr 5 = A2 high , A1 low , A0 high 101
addr 6 = A2 high , A1 high , A0 low 110
addr 7 = A2 high, A1 high, A0 high 111
Connect pin #12 of the expander to Analog 5 (i2c clock)
Connect pin #13 of the expander to Analog 4 (i2c data)
Connect pins #15, 16 and 17 of the expander to ground (address selection)
Connect pin #9 of the expander to 5V (power) // operation voltage is 1.8 to 5.5
Connect pin #10 of the expander to ground (common ground)
Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)
Input #0 is on pin 21 so connect a button or switch from there to ground
Code:
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp1; // chip 1
Adafruit_MCP23017 mcp2; // chip 2
#define addr1 7 // 7 = A2 high, A1 high, A0 high
#define addr2 0 // 0 = A2 low, A1 low, A0 low
void setup()
{
Serial.begin(9600);
mcp1.begin(addr1);
mcp2.begin(addr2);
mcp1.pinMode(0, INPUT); //pin 21 on chip
mcp2.pinMode(0, INPUT); //pin 21 on chip
}
void loop()
{
if(mcp1.digitalRead(0)== HIGH )
Serial.println("HIGH");
delay(1000);
if(mcp2.digitalRead(0)== HIGH )
Serial.println("HIGH 2");
delay(1000);
}