Exercise B: I/O Expander Interrupt
~15 min
The Challenge
The TCA6424 I/O expander on the uFerris board has an INT output pin that connects to one of the Xiao GPIO pins. Instead of polling the I/O expander over I2C to check for button presses, use this interrupt line to get notified when an input changes.
Steps
- Create a new project
esp-generate --chip esp32c3 -o unstable-hal -o vscode -o esp-backtrace -o log --headless expander_interrupt
cd expander_interrupt
-
Find the INT pin — check your pinout card to see which Xiao GPIO pin the I/O expander's INT output is connected to.
-
Configure a GPIO interrupt on that pin — the INT line is typically active-low, so configure for a falling edge trigger.
-
In the interrupt handler, set a flag indicating that the I/O expander state has changed.
-
In the main loop, when the flag is set, read the I/O expander's input register over I2C to determine which button was pressed, then react accordingly (e.g., toggle an LED on the expander).
What to Notice
- This combines two peripherals: GPIO interrupts and I2C communication
- The interrupt tells you something changed, but you still need I2C to find out what changed
- Compare this to your earlier polling approach — what's the advantage?
- The Instantiate → Configure → Control pattern applies to both the GPIO interrupt setup and the I2C I/O expander