adafruit / Adafruit-MCP23017-Arduino-Library

Arduino Library for Adafruit MCP23017
Other
359 stars 204 forks source link

Communicating with chips with I2C addresses does not work #77

Closed derianlebreton closed 2 years ago

derianlebreton commented 3 years ago

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 One mcp23017 IO pin pulled high to make sure I'm really reading it

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


// 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:

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:

#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);
}

Reading mcp0 port: 255

I have tried 0x00, (uint8_t)0, and 0, but they all show the same broken behavior.

After reverting the library to version 1.3 and using the following code, it works as expected:

#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);
}
bergernetch commented 2 years ago

MCP23017/MCP23008 have 7-bit I2C address decoding, with 4 bits fixed. When you set A0, A1 and A2 to 0, your address is 0x20. So use this: mcp0.begin(0x20);

caternuson commented 2 years ago

Yep, what @bergernetch said. See datasheet for details. image

If uncertain, can do an I2C scan to determine addresses: https://learn.adafruit.com/scanning-i2c-addresses/arduino

caternuson commented 2 years ago

Closing, can reopen if still an issue with correct addressing.