Teensy 3.2 - Wrong (?) readings from mcp23017/i2c

Status
Not open for further replies.

DanieleFromItaly

Active member
Hello guys, I'm trying to read pin status of a mcp23017 in one shot using the Adafruit_mcp23017 library (https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library);

The sketch:

Code:
#include <Adafruit_MCP23017.h>

/* Our I2C MCP23017 GPIO expanders */
Adafruit_MCP23017 mcp;


void setup() {
  Serial.begin(9600);
  Serial.println("MCP23007 testbuttons ...");
  //
  mcp.begin();
  Wire.setClock(100000);
  // setup pins, pullup resistor and INPUT
  for (int i = 0; i < 15; i++) {
    mcp.pinMode(i, INPUT);
    mcp.pullUp(i,HIGH);
  }
}

void loop() {
  int i = 0;
  delay(50);
  // all pins in one shot
  uint16_t pins = mcp.readGPIOAB();
  // GPA
  uint8_t pinA = mcp.readGPIO(0);
  // GPB
  uint8_t pinB = mcp.readGPIO(1);
  Serial.println("Read: " + String(pins)+ "/"+ String(pins, BIN)+ " : A: "+String(pinA)+"/"+String(pinA, BIN)+ " : B: "+String(pinB)+"/"+String(pinB, BIN));
  
}

Result: 32767/111111111111111 : A: 255/11111111 : B: 127/1111111

Am I wrong or it should be 65535 ? Also B should be 255..

I tried 2 IC, one normal size on a breadboard and one on small board with the smd version.

Thanks,
Daniele.
 
Actually, you are driving only 15 mcp input pins (0 to 14) high with your pull-up configuration which would explain the missing bit:
Code:
  // setup pins, pullup resistor and INPUT
  for (int i = 0; i < 15; i++) {
    mcp.pinMode(i, INPUT);
    mcp.pullUp(i,HIGH);
  }

Try instead :
Code:
  // setup pins, pullup resistor and INPUT
  for (int i = 0; i < [B]16[/B]; i++) {
    mcp.pinMode(i, INPUT);
    mcp.pullUp(i,HIGH);
  }
 
Status
Not open for further replies.
Back
Top