MCP23017 and Interrupts - half working

userHead Sean.Haynes 2025-05-06 00:58:03 768 Views3 Replies

Evening all,

Brief outline:
I have been working on a button box project - had it all working but when completely compiled the number of toggle switches caused polling of the rotary encoders to be too slow missing inputs. The main board is an Arduino Leonardo, attached to which is a DFRobot MCP23017 expander: Gravity: MCP23017 IIC 16 Digital IO Expansion Module Wiki - DFRobot
So on reaching out and doing a bit of research it seems I need to use interrutps. I looked at the Adafruit library and now the DFRobot library.

 

Though I understand in concept what interrupts do, understanding the coding is ,for me, far more complicated so downloaded an example on the DFRobot Wiki. The IO sample code towards the bottom of the page: Gravity: MCP23017 IIC 16 Digital IO Expansion Module Wiki - DFRobot

 

However I can't seem to find a way of making it work, or rather it half does only reading and reporting change switch states on bank B, absolutely nothing on bank A.

I was hoping someone might be able to explain why, outside of my lack of knowledge, how to fix? Many thanks

 

 

 

 

 

#include <DFRobot_MCP23017.h>



 

DFRobot_MCP23017 mcp(Wire, 0x27);


 

bool intFlagA = false;  //INTA interrupt sign

bool intFlagB = false;  //INTB interrupt sign


 

/*Interrupt service function, prototype void func(int index), index represents the pin which is interrupted*/

void gpa0CB(int index) {

  String description = mcp.pinDescription(index);

  Serial.print(description);

  Serial.println(" Interruption occurs!");

  delay(1000);

}


 

void gpb7CB(int index) {

  String description = mcp.pinDescription(index);

  Serial.print(description);

  Serial.println(" Interruption occurs!");

  delay(1000);

}




 

void test(int index) {


 

  String description = mcp.pinDescription(index);

  Serial.print(description);

  Serial.println(" Interruption occurs!");

}


 

void setup() {

  Serial.begin(115200);

  pinMode(2, INPUT);  //use UNO external interrupt 0

  pinMode(3, INPUT);  //use UNO external interrupt 1

  mcp.pinMode(mcp.eGPB, INPUT_PULLUP);

  mcp.pinMode(mcp.eGPA, INPUT_PULLUP);


 

  (mcp.begin() != 0);

  {

  }


 

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA0, /*mode = */ mcp.eHighLevel, /*cb = */ test);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA1, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA2, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA3, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA4, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA5, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA6, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPA7, /*mode = */ mcp.eHighLevel, /*cb = */ gpa0CB);




 

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB7, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB6, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB5, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB4, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB3, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB2, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB1, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);

  mcp.pinModeInterrupt(/*pin = */ mcp.eGPB0, /*mode = */ mcp.eChangeLevel, /*cb = */ gpb7CB);



 

  attachInterrupt(/*Interrupt NO*/ 0, notifyA, CHANGE);  //Enable external interrupt 0, connect INTA to the main-controller's digital pin: UNO(2),Mega2560(2),Leonardo(3),microbit(P0)

  attachInterrupt(/*Interrupt NO*/ 1, notifyB, CHANGE);  //Enable external interrupt 1, connect INTB to the main-controller's digital pin: UNO(3),Mega2560(3),Leonardo(2),microbit(P1)

}

/*Interrupt service function*/

void notifyA() {

  intFlagA = true;

}

void notifyB() {

  intFlagB = true;

}


 

void loop() {

  if (intFlagA) {

    intFlagA = false;

    mcp.pollInterrupts(/*group = */ mcp.eGPIOA);

  }

  if (intFlagB) {

    intFlagB = false;

    mcp.pollInterrupts(/*group = */ mcp.eGPIOB);

  }

}

 

 

 

2025-05-27 00:39:13

anyone?

userHeadPic Sean.Haynes
2025-05-11 02:17:36

anyone? I've emailed the coder, no response - If I can just get this working I'm on the home run……

userHeadPic Sean.Haynes
Tonny12138 wrote:

If you have already emailed our technical support team at [email protected], please rest assured that our technical specialists will respond to your inquiry as soon as possible. We appreciate your patience.​

2025-05-11 19:53:50
1 Replies