I have been experimenting with I2C and the mcp23017 IO expander chip for my arduino ATMega2560 as I would rather use the IO on the arduino its self for other things I am just figuring out how to use the adafruit mcp23017.h library and cant figure out how to address multiple mcp23017 chips and how to use there pins individually this is the code from the button library that I editied.
I want to be able to address the individual chips and the pins I was not quite sure if in the setup that the pin modes for the IO go up sequentially from 0 past 15 if multiple chips are connected and addressed in code. For example if the first chip is addressed as 0x20 and the IO number count is from 0 - 15 if I added and addressed another chip as 0x21 will that count go from 0 - 15 to 0 - 31.
#include <Wire.h>
#include "Adafruit_MCP23017.h"
//pin 1 and 0 are mcp pins not arduino IO pins
Adafruit_MCP23017 mcp;
void setup() {
mcp.begin(); // use default address 0
mcp.pinMode(0, INPUT);
mcp.pinMode(1, OUTPUT);
Serial.begin(9600);
pinMode(13, OUTPUT); // use the p13 LED as debugging
}
void loop() {
// The LED will 'echo' the button
digitalWrite(13, mcp.digitalRead(0)); //Writes pin 13 to the reading of pin 0
mcp.digitalWrite(1, mcp.digitalRead(0)); //Writes pin 1 to the reading of 0
if(mcp.digitalRead(1) == HIGH){ //if pin 1 == high serialprint led whent high
Serial.println("Led whent HIGH");
}
}
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() {
}