MCP23017 library issue

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
derianlebreton
 
Posts: 5
Joined: Sun Oct 24, 2021 1:50 am

MCP23017 library issue

Post by derianlebreton »

Hi all,

I'm having a weird problem with the Adafruit_MCP23X17 library (I have the latest installed, and latest version of BusIO), specifically when trying to set up one of these devices with a defined I2C address. For testing purposes, I have reduced my system to the following:

1x QTPY microcontroller, providing 3.3V and GND
1x mcp23017 at I2C address 0x00 (pins A0, A1, A2 connected to GND)
mcp 23017 ~reset line pulled to 3.3V via 100kΩ
0.1 uF and 10uF caps on 3.3V
2.2kΩ pull-ups to 3.3V on SCK and SCL pins from QTPY
Plus a couple of wires to pull some of the mcp23017 IO pins high so I can make sure I'm really reading them

When I run the following sketch, this device works as anticipated:

Code: Select all

#include <Adafruit_MCP23X17.h>

// Create IO expander instance
Adafruit_MCP23X17 mcp0;

void setup() {
    mcp0.begin_I2C();
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO()));
    delay(1000);
}
I get output like this:

Code: Select all

Reading mcp0 port: 32
Which corresponds to the one wire I have pulling a pin high. However, this is not suitable for me as I need multiple mcp23017s on the I2C bus for my project. If I try to provide an I2C address in mcp0.begin_I2C(), I only get bogus data:

Code: Select all

#include <Adafruit_MCP23X17.h>

// Create IO expander instance
Adafruit_MCP23X17 mcp0;

void setup() {
    mcp0.begin_I2C(0);
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO()));
    delay(1000);
}

Code: Select all

Reading mcp0 port: 255
I have tried 0x00, (uint8_t)0, and 0, but they all show the same broken behavior.

User avatar
derianlebreton
 
Posts: 5
Joined: Sun Oct 24, 2021 1:50 am

Re: MCP23017 library issue

Post by derianlebreton »

I reverted the library to version 1.3, and now the following code works:

Code: Select all

#include <Adafruit_MCP23017.h>

// Create IO expander instance
Adafruit_MCP23017 mcp0;

void setup() {
    mcp0.begin((uint8_t)0);
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO(0)));
    delay(1000);
}

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP23017 library issue

Post by adafruit_support_carter »

1x mcp23017 at I2C address 0x00 (pins A0, A1, A2 connected to GND)
That won't actually set 0 for an address. Try configuring A0/A1/A2 like you want, and then see what address shows up in an I2C scan:
https://learn.adafruit.com/scanning-i2c ... es/arduino

For reference, A0/A1/A2 are setting 3 bits of the address. The other bits are fixed (and non-zero):
i2caddr.jpg
i2caddr.jpg (33.37 KiB) Viewed 585 times

User avatar
derianlebreton
 
Posts: 5
Joined: Sun Oct 24, 2021 1:50 am

Re: MCP23017 library issue

Post by derianlebreton »

Interesting. Using version 1.3 of the library I have two MCP23017s communicating properly, one at 0x00 and one at 0x01. Maybe the old library does some magic that the new version undid and didn't document?

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP23017 library issue

Post by adafruit_support_carter »

Oh, there was a change. OK, sorry about that. There were breaking changes introduced at the 2.0.0 release:
https://github.com/adafruit/Adafruit-MC ... /tag/2.0.0

Prior to that, the "address" was essentially only a 3bit value representing A0/A1/A2:
https://github.com/adafruit/Adafruit-MC ... #L118-L122
The fixed upper bits were or'd in at write time:
https://github.com/adafruit/Adafruit-MC ... 17.cpp#L73

Now the address is the full address - same as you'd see when running an I2C scan.

User avatar
derianlebreton
 
Posts: 5
Joined: Sun Oct 24, 2021 1:50 am

Re: MCP23017 library issue

Post by derianlebreton »

Thanks for the note. It would be useful if this change was reflected in the example sketches.

Thanks,
Brian.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”