Example Code for Arduino-Interrupt Polling
Last revision 2025/12/13
Set the IO pins of the board to interrupt mode, such as eChangeLevel, eFalling, eRising, eHighLevel, eLowLevel. Set PA0, PA1, PB6, PB7 to eChangeLevel, eFalling, eRising, eHighLevel respectively, poll and serial print the pins where interrupt happened. Set a pin of IO expansion board to interrupt mode, poll if there is an interrupt occurring on the pins of the port. Experiment phenomenon: poll if there is an interrupt occurring on the port group(A, B, A+B), if there is, serial print the pin which is interrupted.
Hardware Preparation
- DFR0216-2 DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B x 1
- DFR0626 MCP23017 IIC to 16 Digital IO Expansion Module x1
- DFR0029 Gravity: Digital Push Button x1
- Gravity: Bright LED Module x1
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download the DFRobot_MCP23017 Library: DFRobot_MCP23017 Library
- About how to install the library?
Wiring Diagram

Other Preparation Work
Set PA0, PA1, PB6, PB7 to eChangeLevel, eFalling, eRising, eHighLevel interrupt modes respectively.
Sample Code
/*!
* @file pollInterrupt.ino
* @brief Set a pin of IO expansion board to interrupt mode, poll if there is an interrupt occurring on the pins of the port.
* @n Experiment phenomenon: poll if there is an interrupt occurring on the port group(A, B, A+B), if there is, serial print the pin which is interrupted.
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [Arya]([email protected])
* @version V1.0
* @date 2019-07-18
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_MCP23017
*/
#include <DFRobot_MCP23017.h>
DFRobot_MCP23017 mcp(Wire, 0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise I2C address within 0x20~0x27.
//DFRobot_MCP23017 mcp;//use default parameter, Wire 0x27(default I2C address)
/*Interrupt service function, prototype void func(int index), index: the number of the pin that is interrupted*/
void func(int index){
String description = mcp.pinDescription(/*pin = */index);
Serial.print(description);
Serial.println(" Interruption occurs!");
}
void setup() {
Serial.begin(115200);
/*wait for the chip to be initialized completely, and then exit*/
while(mcp.begin() != 0){
Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!");
delay(1000);
}
/*Parameter mode, the available is shown below:
eLowLevel eHighLevel eRising eFalling eChangeLevel
Low-level interrupt High-level interrupt Rising-edge interrupt Falling-edge interrupt Double-edge interrupts
Parameter cb Interrupt service function(with parameter)
Prototype void func(int)
*/
mcp.pinModeInterrupt(/*p = */mcp.eGPA0, /*mode = */mcp.eChangeLevel, /*cb = */func);//digital pin0(eGPA0), double edge interrupt, generate an interrupt when the status of pin0 changes, INTA output High level.
mcp.pinModeInterrupt(/*p = */mcp.eGPA1, /*mode = */mcp.eFalling, /*cb = */func);//digital pin1(eGPA1), falling edge interrupt, generate an interrupt when the status of pin 1 changes from High to Low, INTA output High level.
mcp.pinModeInterrupt(/*p = */mcp.eGPB7, /*mode = */mcp.eRising, /*cb = */func);//digital pin15(eGPB7), rising edge interrupt, generate an interrupt when the status of pin15 changes from Low to High, INTB output High level.
mcp.pinModeInterrupt(/*p = */mcp.eGPB6, /*mode = */mcp.eHighLevel, /*cb = */func);//digital pin14(eGPB6), high level interrupt, generate an interrupt when the pin 14 is in high level, INTB output High level.
}
void loop() {
/*pollInterrupts function is used to poll if an interrupt occurs on a port group
Parameter group, the available parameter is shown below: (default: eGPIOALL)
eGPIOA eGPIOB eGPIOALL
Port Group A Port Group B Port Group A+B
*/
mcp.pollInterrupts();
// delay(1000);
}
Result
Press the button connected with PA0, PA1, PB6, PB7 to trigger interrupt, and serial print the pins where interrupt happened.

Was this article helpful?
