Problem with MCP23017 Input Registering on all Pins

I bought a few MCP23017 I2C expander modules to try to get more digital inputs on a NodeMCU but am having a weird issue where any one of the GPA pins being pulled low seems to trigger a low reading across all of them. I've checked the connections with a multimeter and they all seem to be isolated. I've also gone though the available documentation for the Adafruit library but can't figure out what I am doing wrong..

Any help would be much appreciated!

Here is the MCP23017 module I am using.

And here is the code:

#include "Adafruit_MCP23017.h"
#include <Wire.h>

Adafruit_MCP23017 mcp;

void setup() {
  Serial.begin(115200);   //start serial monitor
  Wire.begin(D2, D1);   //set to default I2C pins on NodeMCU
  
  mcp.begin();      // initialize mcp, use default address 0

  mcp.pinMode(0, INPUT);  //set pin GPA0 to input
  mcp.pullUp(0, HIGH);  // turn on a 100K pullup internally

  mcp.pinMode(1, INPUT);  //srt pin GPA1 to input
  mcp.pullUp(1, HIGH);  // turn on a 100K pullup internally
}

void loop() {

int sensor0 = mcp.digitalRead(0);   //read GPA0 pin
int sensor1 = mcp.digitalRead(1);   //read GPA1 pin

Serial.print("GPAO: ");   
Serial.println(sensor0);  //print sensor0 reading

Serial.print("GPA1: ");
Serial.println(sensor1);  //print sensor1 reading

delay(20);
}

Possibly nothing but, the few tutorial/demos I checked all had the Wire.begin parameters as D1 *then * D2. Yours are reversed.

How are you powering this?
The NodeMCU is a 3V3 device so you either need to power the MCP23017 board with 3V3 or use an I2C level shifter if you are powering it with 5V.

You also need 3K3 pull up resistors on each I2C line, if it is working at 3V3.

// turn on a 100K pullup internally

Is not sufficient.

Thanks for the quick responses. I don't know exactly what happened, but the MCP23017 all of a sudden started working as expected.. I did run a couple example sketches from different libraries, so not sure if that somehow reset something?

The other thing I did was connect the A0 A1 and A2 pins from the side of the board opposite the GPA and GPB pins to ground, as described in one of the example sketches.

Possibly nothing but, the few tutorial/demos I checked all had the Wire.begin parameters as D1 then D2. Yours are reversed.

Interesting. I do have some other sensors, multiplexers, ADC, etc.. running via I2C, though, and they all seem to be working.

How are you powering this?

I'm running the NodeMCU, sensors, MCP23017, etc.. with the 5V terminal of an L298. The L298 itself is powered by a 12V battery (to drive 12V motors).

You also need 3K3 pull up resistors on each I2C line, if it is working at 3V3.

Ah, I see. So I suppose I should add 5k resistors if it's running at 5V?

so not sure if that somehow reset something?

No you didn't reset anything, it was probably a wiring fault. If you are using solderless bread board it is easy to think something is connected because you plug it in a hole. They often don't make proper contact.

So I suppose I should add 5k resistors if it's running at 5V?

Well you would if 5K was a "normal" resistor value, as it is not you use 4K7. The idea is that you should have about 1mA being switched. This makes the switching wave forms have sharper edges as the stray capacitance on the lines can charge up faster. The specification suggests a current of 2mA but in industry we use 1mA for a bit of reliability. It this context reliability means on avrage a longer life for a part or system.

I'm running the NodeMCU, sensors, MCP23017, etc.. with the 5V terminal of an L298.

Ouch! the NodeMCU is basically a 3V3 device and so all things attached to it that generate signals read by the NodeMCU are at 3V3. You will eventually damage it by feeding it with 5V signals.

Thanks Grumpy_Mike. I started to suspect it had been a wiring issue after I built a couple more identical circuits and could not reproduce the problem.

As for the NodeMCU voltage issue, I was wondering about that as well. I did read that it was 5V tolerant, though, which is why I decided to go ahead with powering everything for the L298's 5V rail: Is ESP8266 I/O really 5V tolerant? - Digital Me

There is a “law” which says that any headline on any article that ends in a question mark can be answered by no.

It should generally work if you connect the pull-ups only to 3.3 V (the NodeMCU provides that).

The MCP23017 will not drive the I2C bus higher than that and at worst, it may not respond correctly to "fringe" conditions but it will not cause electrical damage to the NodeMCU.