adafruit / Adafruit-MCP23017-Arduino-Library

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

Problem with MCP23017 library #56

Closed RolfSste closed 3 years ago

RolfSste commented 3 years ago

Adafruit MCP23017 library includes

void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { updateRegisterBit(p, (d == INPUT), MCP23017_IODIRA, MCP23017_IODIRB); }

Unfortunately INPUT is in Arduino.h 0, which works correct for lot's of uC's, but not for ESP32, which comes with its own header file. There, INPUT is defined as 1, so MCP23017 is not correctly initialized. The Adafruit header file for MCP23017 should include

#define MCP23017_INPUT 0

so that pinMode can be written as

void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { updateRegisterBit(p, (d == MCP23017_INPUT), MCP23017_IODIRA, MCP23017_IODIRB); }

ladyada commented 3 years ago

better to change the function to just test if d == INPUT and write 0 please submit a PR :)

RolfSste commented 3 years ago

I have tested the above solution and I think it would be a clean consitent solution for the library.

ladyada commented 3 years ago

great, please submit a PR!

RolfSste commented 3 years ago

Sorry, I'm new to github and have to check out, how to submit an PR. It will take a little bit.

caternuson commented 3 years ago

Please try the newly released 2.0.0 version of the library which should resolve this issue: https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/releases/tag/2.0.0

caternuson commented 3 years ago

Closing for now. Should be fixed with 2.0.0 release. Tested on a Feather ESP32 with following sketch.

#include <Adafruit_MCP23X17.h>

#define IN_PIN 0

Adafruit_MCP23X17 mcp;

void setup() {
  Serial.begin(9600);
  Serial.println("MCP23xxx Input Test!");

  if (!mcp.begin_I2C()) {
    Serial.println("Error.");
    while (1);
  }

  mcp.pinMode(IN_PIN, INPUT);
}

void loop() {
  Serial.print("Input = "); Serial.println(mcp.digitalRead(IN_PIN ));
  delay(500);
}

and works as expected.

Screenshot from 2021-06-28 08-16-58

businescat commented 2 years ago

The 2.0.0/2.1.0 library still does not work with esp32 devkit v1 for some reason........ caused me quite a bit of headache. But works with my esp32-s2, so I'm happy it at least works on something. Hopefully this comment willl save someone a headache seems to be very picky about what esp32 it works on so try different ones.