How to use multiple mcp23017 chips with adafruit mcp23017.h library

I experimented with I2C and the mcp23017 IO chip expander for my arduino ATMega2560, since I would prefer to use IO on arduino myself for other things, I just figure out how to use the adafruit mcp23017.h library and cannot figure out how to access multiple chips mcp23017 and how to use the contacts there separately, this is the code from the button library that I edited.

I want to be able to access individual chips and pins, I was not quite sure if in the configuration, so that the output modes for IO consistently rose from 0 to 15, if several chips were connected and addressed to the code. For example, if the first chip is addressed as 0x20, and the number of I / O numbers is from 0 to 15, if I added and turned to another chip, since 0x21 will count from 0 to 15 to 0 - 31. ~ Edit if you could would recommend or send a library that will be easier or can help, please.

#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"); } } 
+7
arduino i2c
source share
3 answers

Each chip must have a unique address, and this is doable according to the Microchip Guide p. 8. So, first of all, you must configure different addresses in the equipment layout.

You must also create an Adafruit_MCP23017 object for each chip that you want to use, and configure the appropriate addresses in your code.

In this case, the contacts of all the chips will have addresses in the range 0-15. To change the state of a contact, you must reference a specific instance.


Update

This is the 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() { } 
+5
source share

I share my solution using the 7 MCP23017 chip, 7x14 contacts = 98 contacts using the 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 

The 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); } 
+2
source share

I finally got my code to work! in the end it was easier than me, although I probably did not give a sufficiently detailed explanation of what my code was supposed to achieve, this is my code here.

 #include "Adafruit_MCP23017.h" Adafruit_MCP23017 mcp1; Adafruit_MCP23017 mcp2; void setup() { mcp1.begin(0); mcp2.begin(1); mcp1.pinMode(0, INPUT); mcp1.pinMode(1, OUTPUT); mcp2.pinMode(1, OUTPUT); Serial.begin(9600); pinMode(13, OUTPUT); } void loop() { mcp2.digitalWrite(1, mcp1.digitalRead(0)); digitalWrite(13, mcp1.digitalRead(0)); mcp1.digitalWrite(1, mcp1.digitalRead(0)); if(mcp1.digitalRead(1) == HIGH){ Serial.println("Led whent HIGH"); } } 

The problem was that I only announced 1 mcp, and thanks to Vladimir Tsykunov, I now know that I need mcp1 and mcp2, which can now be seen in my functional code. I may encounter problems in the future with things that I have not been revised, but for this project it is working now.

+1
source share

All Articles