### Initialize MCP23X17 over I2C Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Use `begin_I2C()` to initialize the MCP23X17 on the I2C bus. The default address is 0x20, but custom addresses and Wire instances can be specified. Returns true on success. ```cpp #include // 8-pin variant #include // 16-pin variant Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // Default address 0x20, default Wire instance if (!mcp.begin_I2C()) { Serial.println("MCP23X17 not found. Check wiring!"); while (1); } // Custom I2C address (A0 pin tied HIGH → 0x21) // if (!mcp.begin_I2C(0x21)) { ... } // Custom Wire instance (e.g., Wire1 on boards with two I2C buses) // if (!mcp.begin_I2C(0x20, &Wire1)) { ... } Serial.println("MCP23X17 initialized over I2C."); } ``` -------------------------------- ### Initialization over Software SPI Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Initializes the device using bit-banged software SPI, useful on platforms without hardware SPI or when hardware SPI pins are already in use. All four signal pins must be specified explicitly. ```APIDOC ## Initialization over Software SPI — `begin_SPI(cs, sck, miso, mosi)` Initializes the device using bit-banged software SPI, useful on platforms without hardware SPI or when hardware SPI pins are already in use. All four signal pins must be specified explicitly. ```cpp #include #define CS_PIN 10 #define SCK_PIN 13 #define MISO_PIN 12 #define MOSI_PIN 11 Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); if (!mcp.begin_SPI(CS_PIN, SCK_PIN, MISO_PIN, MOSI_PIN)) { Serial.println("MCP23S08 not found via software SPI!"); while (1); } Serial.println("MCP23S08 initialized over software SPI."); } ``` ``` -------------------------------- ### Initialize MCP23S08 over Software SPI Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Use `begin_SPI(cs, sck, miso, mosi)` for bit-banged software SPI, useful when hardware SPI is unavailable or pins are in use. All four signal pins must be specified. ```cpp #include #define CS_PIN 10 #define SCK_PIN 13 #define MISO_PIN 12 #define MOSI_PIN 11 Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); if (!mcp.begin_SPI(CS_PIN, SCK_PIN, MISO_PIN, MOSI_PIN)) { Serial.println("MCP23S08 not found via software SPI!"); while (1); } Serial.println("MCP23S08 initialized over software SPI."); } ``` -------------------------------- ### Configure Interrupt System with setupInterrupts() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Sets the global interrupt output behavior for the MCP23x17/x08. Options include mirroring INTA/INTB, open-drain output, and polarity (HIGH/LOW). Call this before configuring individual pin interrupts. ```cpp #include Adafruit_MCP23X08 mcp; void setup() { mcp.begin_I2C(); // Mirror INTA/INTB (single wire), active-drive output, asserts LOW mcp.setupInterrupts(true, false, LOW); // For open-drain with external pull-up, asserts LOW (wired-AND): // mcp.setupInterrupts(true, true, LOW); } ``` -------------------------------- ### Configuring the Interrupt System Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Sets up the global interrupt output behavior for the MCP23x08/x17 chip, controlling mirroring, open-drain output, and polarity. ```APIDOC ## Configuring the Interrupt System — `setupInterrupts()` Configures the global interrupt output behavior for the MCP23x17/x08. `mirroring` ORs INTA and INTB so a single wire signals any interrupt. `openDrain` sets the INT pin to open-drain (for wired-AND with other INT sources). `polarity` sets whether the INT pin asserts `HIGH` or `LOW`. ```cpp #include Adafruit_MCP23X08 mcp; void setup() { mcp.begin_I2C(); // Mirror INTA/INTB (single wire), active-drive output, asserts LOW mcp.setupInterrupts(true, false, LOW); // For open-drain with external pull-up, asserts LOW (wired-AND): // mcp.setupInterrupts(true, true, LOW); } ``` ``` -------------------------------- ### Enable Pin Interrupt with setupInterruptPin() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Enables interrupt-on-change for a specific pin, triggering on `CHANGE`, `LOW`, or `HIGH` levels. This function must be called after `setupInterrupts()`. Configure the pin as an input with a pull-up if needed. ```cpp #include #define BUTTON_PIN 1 #define INT_PIN 7 // MCU pin wired to MCP INTA Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); pinMode(INT_PIN, INPUT); // MCU reads INT line mcp.setupInterrupts(true, false, LOW); // INT asserts LOW mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); mcp.setupInterruptPin(BUTTON_PIN, LOW); // fire while pin is LOW } void loop() { if (!digitalRead(INT_PIN)) { // INT asserted uint8_t pin = mcp.getLastInterruptPin(); // which pin fired uint16_t states = mcp.getCapturedInterrupt(); // snapshot at interrupt time Serial.print("Interrupt on pin: "); Serial.println(pin); Serial.print("Captured state: 0b"); Serial.println(states, BIN); delay(250); mcp.clearInterrupts(); } } ``` -------------------------------- ### Initialize MCP23S17 over Hardware SPI Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Use `begin_SPI(cs_pin)` to initialize the MCP23S17 using the hardware SPI bus. The `cs_pin` selects the chip-select line. An optional hardware address byte can be provided for multi-device buses. Returns true on success. ```cpp #include #define CS_PIN 10 // Chip-select pin on the microcontroller Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // Basic hardware SPI, hw_addr defaults to 0b000 if (!mcp.begin_SPI(CS_PIN)) { Serial.println("MCP23S17 not found!"); while (1); } // With explicit SPI instance and hardware address 0b101 (A2=1, A0=1) // if (!mcp.begin_SPI(CS_PIN, &SPI, 0b101)) { ... } Serial.println("MCP23S17 initialized over hardware SPI."); } ``` -------------------------------- ### Initialize SPI with HW Address (Workaround) Source: https://github.com/adafruit/adafruit-mcp23017-arduino-library/blob/master/README.md Due to a hardware bug in the MCP23S17 chip (Rev. A Silicon Errata), if using a device with A2 = high and not using addressing, the hardware address must be set to 0b1XX. Initialize with 0b1XX even if not using addressing. ```cpp mcp.begin_SPI(10, &SPI, 0b100); ``` -------------------------------- ### Initialize SPI with HW Address Source: https://github.com/adafruit/adafruit-mcp23017-arduino-library/blob/master/README.md Use this function to initialize the SPI communication with a specific hardware address. Ensure HW Address recognition is enabled by calling enableAddrPins(). ```cpp mcp.begin_SPI(10, &SPI, 0b101); ``` -------------------------------- ### Bulk Port Write with writeGPIO() variants Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Writes to all output pins of a port (A, B, or AB) simultaneously using a bitmask. This is significantly faster than individual `digitalWrite()` calls. Ensure pins are configured as outputs. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); // Set all Port A pins as outputs for (uint8_t pin = 0; pin < 8; pin++) mcp.pinMode(pin, OUTPUT); // Set all Port B pins as outputs for (uint8_t pin = 8; pin < 16; pin++) mcp.pinMode(pin, OUTPUT); } void loop() { // Alternate patterns on both ports simultaneously mcp.writeGPIOAB(0b1010101001010101); // Port B = 0b10101010, Port A = 0b01010101 delay(500); mcp.writeGPIOAB(0b0101010110101010); delay(500); // Or write ports individually mcp.writeGPIOA(0xFF); // all GPA pins HIGH mcp.writeGPIOB(0x00); // all GPB pins LOW delay(500); } ``` -------------------------------- ### Bulk Port Read with readGPIO() variants Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Reads all pins of a port (A, B, or AB) in a single transaction, returning a bitmask. Use for efficient reading of multiple pins. Configure pins as inputs with pull-ups before reading. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); // Configure all Port A pins as inputs with pull-ups for (uint8_t pin = 0; pin < 8; pin++) { mcp.pinMode(pin, INPUT_PULLUP); } // Configure all Port B pins as inputs with pull-ups for (uint8_t pin = 8; pin < 16; pin++) { mcp.pinMode(pin, INPUT_PULLUP); } } void loop() { uint8_t portA = mcp.readGPIOA(); // bits 0–7 for GPA0–GPA7 uint8_t portB = mcp.readGPIOB(); // bits 0–7 for GPB0–GPB7 uint16_t portAB = mcp.readGPIOAB(); // bits 0–15, low byte=A, high byte=B Serial.print("Port A: 0b"); Serial.println(portA, BIN); Serial.print("Port B: 0b"); Serial.println(portB, BIN); Serial.print("A+B: 0x"); Serial.println(portAB, HEX); delay(500); } ``` -------------------------------- ### Initialization over Hardware SPI Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Initializes the MCP23S08 or MCP23S17 device using the hardware SPI bus. The `cs_pin` argument selects the chip-select line. An optional hardware address byte (`_hw_addr`) matches the A2/A1/A0 physical pins when multiple SPI devices share the bus. Returns `true` on success. ```APIDOC ## Initialization over Hardware SPI — `begin_SPI(cs_pin)` Initializes the MCP23S08 or MCP23S17 device using the hardware SPI bus. The `cs_pin` argument selects the chip-select line. An optional hardware address byte (`_hw_addr`) matches the A2/A1/A0 physical pins when multiple SPI devices share the bus. Returns `true` on success. ```cpp #include #define CS_PIN 10 // Chip-select pin on the microcontroller Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // Basic hardware SPI, hw_addr defaults to 0b000 if (!mcp.begin_SPI(CS_PIN)) { Serial.println("MCP23S17 not found!"); while (1); } // With explicit SPI instance and hardware address 0b101 (A2=1, A0=1) // if (!mcp.begin_SPI(CS_PIN, &SPI, 0b101)) { ... } Serial.println("MCP23S17 initialized over hardware SPI."); } ``` ``` -------------------------------- ### Writing a Digital Output Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Drives a configured output pin HIGH or LOW. Identical semantics to the Arduino built-in. ```APIDOC ## Writing a Digital Output — `digitalWrite()` Drives a configured output pin HIGH or LOW. Identical semantics to the Arduino built-in. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.pinMode(0, OUTPUT); // GPA0 as output } void loop() { mcp.digitalWrite(0, HIGH); // LED on delay(500); mcp.digitalWrite(0, LOW); // LED off delay(500); } ``` ``` -------------------------------- ### Initialization over I2C Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Initializes the MCP23x08 or MCP23x17 device on the I2C bus. The default I2C address is 0x20; it can be changed to 0x20–0x27 by setting the A0/A1/A2 hardware pins. Returns true on success and false if the device cannot be found. ```APIDOC ## Initialization over I2C — `begin_I2C()` Initializes the MCP23x08 or MCP23x17 device on the I2C bus. The default I2C address is `0x20`; it can be changed to `0x20`–`0x27` by setting the A0/A1/A2 hardware pins. Returns `true` on success and `false` if the device cannot be found. ```cpp #include // 8-pin variant #include // 16-pin variant Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // Default address 0x20, default Wire instance if (!mcp.begin_I2C()) { Serial.println("MCP23X17 not found. Check wiring!"); while (1); } // Custom I2C address (A0 pin tied HIGH → 0x21) // if (!mcp.begin_I2C(0x21)) { ... } // Custom Wire instance (e.g., Wire1 on boards with two I2C buses) // if (!mcp.begin_I2C(0x20, &Wire1)) { ... } Serial.println("MCP23X17 initialized over I2C."); } ``` ``` -------------------------------- ### Configure Pin Direction with pinMode() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Sets a pin as INPUT, OUTPUT, or INPUT_PULLUP, mirroring the Arduino `pinMode()` API. For `INPUT_PULLUP`, the internal 100 kΩ pull-up resistor is enabled. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.pinMode(0, OUTPUT); // GPA0 → output (LED) mcp.pinMode(1, INPUT_PULLUP); // GPA1 → input with pull-up (button) mcp.pinMode(8, INPUT); // GPB0 → floating input (sensor) // MCP23X17 pin IDs: GPA0–GPA7 = 0–7, GPB0–GPB7 = 8–15 // MCP23X08 pin IDs: GPA0–GPA7 = 0–7 (no Port B) } ``` -------------------------------- ### Enable Hardware Address Pins (SPI) with Adafruit_MCP23X17 Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Enables the HAEN bit in IOCON for SPI transactions, allowing multiple MCP23Sxx devices on a single bus. Must be called immediately after begin_SPI() and before other operations. ```cpp #include #define CS_PIN 10 Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // HW address 0b101 matches A2=1, A1=0, A0=1 physical wiring if (!mcp.begin_SPI(CS_PIN, &SPI, 0b101)) { Serial.println("Error initializing MCP23S17!"); while (1); } // Enable HW address decoding FIRST (sends to all devices, then 0b1xx) mcp.enableAddrPins(); // Now safe to use the device normally mcp.pinMode(0, OUTPUT); mcp.digitalWrite(0, HIGH); Serial.println("Ready."); } ``` -------------------------------- ### Bulk Port Write Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Writes to all output pins of a specified port (GPIOA, GPIOB, or GPIOA+B) simultaneously using a bitmask. This is more efficient than individual `digitalWrite()` calls. ```APIDOC ## Bulk Port Write — `writeGPIO()` / `writeGPIOA()` / `writeGPIOB()` / `writeGPIOAB()` Writes all 8 (or 16) output pins of a port in a single register transaction using a bitmask. Much faster than calling `digitalWrite()` in a loop. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); // Set all Port A pins as outputs for (uint8_t pin = 0; pin < 8; pin++) mcp.pinMode(pin, OUTPUT); // Set all Port B pins as outputs for (uint8_t pin = 8; pin < 16; pin++) mcp.pinMode(pin, OUTPUT); } void loop() { // Alternate patterns on both ports simultaneously mcp.writeGPIOAB(0b1010101001010101); // Port B = 0b10101010, Port A = 0b01010101 delay(500); mcp.writeGPIOAB(0b0101010110101010); delay(500); // Or write ports individually mcp.writeGPIOA(0xFF); // all GPA pins HIGH mcp.writeGPIOB(0x00); // all GPB pins LOW delay(500); } ``` ``` -------------------------------- ### Write Digital Output with digitalWrite() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Drives a configured output pin HIGH or LOW, with identical semantics to the Arduino built-in `digitalWrite()` function. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.pinMode(0, OUTPUT); // GPA0 as output } void loop() { mcp.digitalWrite(0, HIGH); // LED on delay(500); mcp.digitalWrite(0, LOW); // LED off delay(500); } ``` -------------------------------- ### Bulk Port Read Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Reads all pins of a specified port (GPIOA, GPIOB, or GPIOA+B) in a single transaction, returning the state as a bitmask. ```APIDOC ## Bulk Port Read — `readGPIO()` / `readGPIOA()` / `readGPIOB()` / `readGPIOAB()` Reads all 8 pins of a port in a single register transaction, returning a `uint8_t` bitmask (bit 0 = pin 0). The MCP23X17 variants also expose `readGPIOA()`, `readGPIOB()`, and `readGPIOAB()` for named port access, where `readGPIOAB()` returns a 16-bit value. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); // Configure all Port A pins as inputs with pull-ups for (uint8_t pin = 0; pin < 8; pin++) { mcp.pinMode(pin, INPUT_PULLUP); } // Configure all Port B pins as inputs with pull-ups for (uint8_t pin = 8; pin < 16; pin++) { mcp.pinMode(pin, INPUT_PULLUP); } } void loop() { uint8_t portA = mcp.readGPIOA(); // bits 0–7 for GPA0–GPA7 uint8_t portB = mcp.readGPIOB(); // bits 0–7 for GPB0–GPB7 uint16_t portAB = mcp.readGPIOAB(); // bits 0–15, low byte=A, high byte=B Serial.print("Port A: 0b"); Serial.println(portA, BIN); Serial.print("Port B: 0b"); Serial.println(portB, BIN); Serial.print("A+B: 0x"); Serial.println(portAB, HEX); delay(500); } ``` ``` -------------------------------- ### Enabling Hardware Address Pins (SPI) Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Enables the HAEN bit in IOCON for SPI transactions, allowing multiple MCP23Sxx devices to share a single SPI bus by decoding hardware address pins (A2/A1/A0). This function must be called immediately after `begin_SPI()` and before any other operations. ```APIDOC ## Enabling Hardware Address Pins (SPI) — `enableAddrPins()` Available on both `Adafruit_MCP23X17` and `Adafruit_MCP23X08`. Enables the HAEN bit in IOCON so the chip decodes its A2/A1/A0 hardware address pins on SPI transactions, allowing multiple MCP23Sxx devices to share a single SPI bus. Must be called immediately after `begin_SPI()` and before any other operations. ```cpp #include #define CS_PIN 10 Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); // HW address 0b101 matches A2=1, A1=0, A0=1 physical wiring if (!mcp.begin_SPI(CS_PIN, &SPI, 0b101)) { Serial.println("Error initializing MCP23S17!"); while (1); } // Enable HW address decoding FIRST (sends to all devices, then 0b1xx) mcp.enableAddrPins(); // Now safe to use the device normally mcp.pinMode(0, OUTPUT); mcp.digitalWrite(0, HIGH); Serial.println("Ready."); } ``` ``` -------------------------------- ### Configuring Pin Direction Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Sets a pin as INPUT, OUTPUT, or INPUT_PULLUP. Mirrors the standard Arduino `pinMode()` API. For `INPUT_PULLUP`, the chip's internal 100 kΩ pull-up resistor is enabled automatically. ```APIDOC ## Configuring Pin Direction — `pinMode()` Sets a pin as `INPUT`, `OUTPUT`, or `INPUT_PULLUP`. Mirrors the standard Arduino `pinMode()` API. For `INPUT_PULLUP`, the chip's internal 100 kΩ pull-up resistor is enabled automatically. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.pinMode(0, OUTPUT); // GPA0 → output (LED) mcp.pinMode(1, INPUT_PULLUP); // GPA1 → input with pull-up (button) mcp.pinMode(8, INPUT); // GPB0 → floating input (sensor) // MCP23X17 pin IDs: GPA0–GPA7 = 0–7, GPB0–GPB7 = 8–15 // MCP23X08 pin IDs: GPA0–GPA7 = 0–7 (no Port B) } ``` ``` -------------------------------- ### Enabling a Pin Interrupt Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Enables interrupt-on-change for a specific pin and defines its trigger mode (CHANGE, LOW, or HIGH). This must be called after `setupInterrupts()`. ```APIDOC ## Enabling a Pin Interrupt — `setupInterruptPin()` Enables interrupt-on-change for an individual pin and sets the trigger mode: `CHANGE` (any edge), `LOW` (level), or `HIGH` (level). Must be called after `setupInterrupts()`. ```cpp #include #define BUTTON_PIN 1 #define INT_PIN 7 // MCU pin wired to MCP INTA Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); pinMode(INT_PIN, INPUT); // MCU reads INT line mcp.setupInterrupts(true, false, LOW); // INT asserts LOW mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); mcp.setupInterruptPin(BUTTON_PIN, LOW); // fire while pin is LOW } void loop() { if (!digitalRead(INT_PIN)) { // INT asserted uint8_t pin = mcp.getLastInterruptPin(); // which pin fired uint16_t states = mcp.getCapturedInterrupt(); // snapshot at interrupt time Serial.print("Interrupt on pin: "); Serial.println(pin); Serial.print("Captured state: 0b"); Serial.println(states, BIN); delay(250); mcp.clearInterrupts(); } } ``` ``` -------------------------------- ### Read Digital Input with digitalRead() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Reads the state of a configured input pin, returning HIGH or LOW. Suitable for both INPUT and INPUT_PULLUP modes. Ensure the pin is configured correctly before reading. ```cpp #include #define BUTTON_PIN 1 // GPA1 Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); // active-low button } void loop() { if (mcp.digitalRead(BUTTON_PIN) == LOW) { // LOW = pressed Serial.println("Button pressed!"); delay(250); // debounce } } ``` -------------------------------- ### Query Interrupt Source with Adafruit_MCP23X17 Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Reads the interrupt pin and captured state without clearing flags. Ensure interrupts are set up and an ISR is attached. ```cpp #include #define INT_PIN 2 // MCU interrupt-capable pin Adafruit_MCP23X17 mcp; volatile bool intFlag = false; void ISR_handler() { intFlag = true; } void setup() { Serial.begin(9600); mcp.begin_I2C(); mcp.setupInterrupts(true, false, LOW); for (uint8_t p = 8; p < 16; p++) { // watch all Port B pins mcp.pinMode(p, INPUT_PULLUP); mcp.setupInterruptPin(p, CHANGE); } pinMode(INT_PIN, INPUT); attachInterrupt(digitalPinToInterrupt(INT_PIN), ISR_handler, FALLING); } void loop() { if (intFlag) { intFlag = false; uint8_t pin = mcp.getLastInterruptPin(); // returns 255 on error uint16_t capture = mcp.getCapturedInterrupt(); // clears flags if (pin != MCP23XXX_INT_ERR) { Serial.print("Changed pin: "); Serial.println(pin); Serial.print("State at interrupt: 0x"); Serial.println(capture, HEX); } } } ``` -------------------------------- ### Reading a Digital Input Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Reads the state of a single digital pin. This function works for pins configured as either INPUT or INPUT_PULLUP. ```APIDOC ## Reading a Digital Input — `digitalRead()` Returns `HIGH` or `LOW` for any configured input pin. Works for both `INPUT` and `INPUT_PULLUP` modes. ```cpp #include #define BUTTON_PIN 1 // GPA1 Adafruit_MCP23X08 mcp; void setup() { Serial.begin(9600); mcp.begin_I2C(); mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); // active-low button } void loop() { if (mcp.digitalRead(BUTTON_PIN) == LOW) { // LOW = pressed Serial.println("Button pressed!"); delay(250); // debounce } } ``` ``` -------------------------------- ### Querying Interrupt Source Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Retrieve information about the pin that triggered an interrupt and its state at the time of the interrupt. `getLastInterruptPin()` returns the pin number, and `getCapturedInterrupt()` returns the pin state snapshot and clears interrupt flags. ```APIDOC ## Querying the Interrupt Source — `getLastInterruptPin()` / `getCapturedInterrupt()` `getLastInterruptPin()` reads the INTF register and returns the pin number (0–15) that triggered the most recent interrupt without clearing it. `getCapturedInterrupt()` reads INTCAP, which holds the pin state snapshot captured at the exact moment the interrupt fired; calling it also clears the interrupt flags. ```cpp #include #define INT_PIN 2 // MCU interrupt-capable pin Adafruit_MCP23X17 mcp; volatile bool intFlag = false; void ISR_handler() { intFlag = true; } void setup() { Serial.begin(9600); mcp.begin_I2C(); mcp.setupInterrupts(true, false, LOW); for (uint8_t p = 8; p < 16; p++) { // watch all Port B pins mcp.pinMode(p, INPUT_PULLUP); mcp.setupInterruptPin(p, CHANGE); } pinMode(INT_PIN, INPUT); attachInterrupt(digitalPinToInterrupt(INT_PIN), ISR_handler, FALLING); } void loop() { if (intFlag) { intFlag = false; uint8_t pin = mcp.getLastInterruptPin(); // returns 255 on error uint16_t capture = mcp.getCapturedInterrupt(); // clears flags if (pin != MCP23XXX_INT_ERR) { Serial.print("Changed pin: "); Serial.println(pin); Serial.print("State at interrupt: 0x"); Serial.println(capture, HEX); } } } ``` ``` -------------------------------- ### Disable Pin Interrupt with disableInterruptPin() Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Disables interrupt-on-change for a single pin without affecting other interrupt configurations. Use this to selectively silence interrupts on specific pins. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.setupInterrupts(false, false, HIGH); mcp.pinMode(3, INPUT_PULLUP); mcp.setupInterruptPin(3, CHANGE); // enable // Later, disable it: mcp.disableInterruptPin(3); // interrupt on pin 3 now silenced } ``` -------------------------------- ### Disabling a Pin Interrupt Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Disables the interrupt-on-change functionality for a single pin without affecting other interrupt configurations. ```APIDOC ## Disabling a Pin Interrupt — `disableInterruptPin()` Disables the interrupt-on-change for a single pin without affecting other pins or the global interrupt configuration. ```cpp #include Adafruit_MCP23X17 mcp; void setup() { mcp.begin_I2C(); mcp.setupInterrupts(false, false, HIGH); mcp.pinMode(3, INPUT_PULLUP); mcp.setupInterruptPin(3, CHANGE); // enable // Later, disable it: mcp.disableInterruptPin(3); // interrupt on pin 3 now silenced } ``` ``` -------------------------------- ### Clearing Interrupts Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Resets the interrupt flags on the MCP23x chip by reading and discarding the INTCAP register. This allows subsequent interrupts to fire after processing the current one. Note that if the interrupt condition persists, the interrupt may not clear. ```APIDOC ## Clearing Interrupts — `clearInterrupts()` Reads and discards the INTCAP register(s), which resets the interrupt flags on the MCP23x chip. Call this after processing an interrupt to allow subsequent interrupts to fire. Note: if the interrupt condition still exists (for `DEFVAL`-based interrupts), the interrupt will not clear. ```cpp #include Adafruit_MCP23X08 mcp; void setup() { mcp.begin_I2C(); mcp.setupInterrupts(true, false, LOW); mcp.pinMode(0, INPUT_PULLUP); mcp.setupInterruptPin(0, LOW); } void loop() { // ... handle interrupt condition ... mcp.clearInterrupts(); // reset so next interrupt can fire delay(100); } ``` ``` -------------------------------- ### Clear Interrupts with Adafruit_MCP23X08 Source: https://context7.com/adafruit/adafruit-mcp23017-arduino-library/llms.txt Resets interrupt flags on the MCP23x chip by reading the INTCAP register. Call this after processing an interrupt. Note: If the interrupt condition persists, the interrupt may not clear. ```cpp #include Adafruit_MCP23X08 mcp; void setup() { mcp.begin_I2C(); mcp.setupInterrupts(true, false, LOW); mcp.pinMode(0, INPUT_PULLUP); mcp.setupInterruptPin(0, LOW); } void loop() { // ... handle interrupt condition ... mcp.clearInterrupts(); // reset so next interrupt can fire delay(100); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.