Skip to content
If this is your first visit, please click here to learn about shipping and delivery.
If this is your first visit, please touch here to learn about shipping and delivery.
Through-hole integrated circuit package used in an I/O expansion circuit

Handle MCP23008 Interrupts Reliably: Captured Inputs, Debouncing and Event Processing

An I/O expander's interrupt line can tell a controller that an input needs attention. It does not automatically turn a bouncing switch into one clean button press, and it does not provide an unlimited history of everything that happened while the controller was busy.

A reliable design separates three stages: notification from the expander, reading the relevant input state, and deciding which application event that state represents. This guide uses a small two-button panel to make those stages easy to test.

Confirm the current device restrictions

PMD Way's MCP23008 I2C expander IC provides a compact interface for additional digital signals. Check the exact marking and current documentation before assigning pins.

The Microchip MCP23008/MCP23S08 datasheet, revision I, identifies GP7 as output-only on the MCP23008. Use GP0 and GP1 for this input example. Do not assume an older “eight bidirectional pins” summary describes every permitted use.

Check the supply, bus-speed and logic-level requirements together. Verify the package pinout, hold reset in its required inactive state, configure address pins deliberately and provide appropriate decoupling and I2C pull-ups.

Prove ordinary input reads first

Before enabling interrupts, configure the two chosen pins as inputs with a defined idle level. For an active-low button arrangement, each switch connects its input to the circuit's ground when pressed, with a suitable pull-up providing the released level.

Read and print the raw input state slowly enough to inspect it. Verify each switch independently, then both together. Check that the physical labels match the bit assignments. This simple polling test separates wiring mistakes from interrupt configuration mistakes.

Keep unused inputs defined as appropriate for the design. Do not use a software interpretation to compensate for an electrically floating input. Likewise, do not attach a relay coil or other load directly merely because a catalogue lists a per-pin current figure.

Choose what should trigger attention

The device can compare enabled inputs with their previous state or with a configured default. The relevant controls are GPINTEN, INTCON and DEFVAL. Choose the mode to match the meaning of the input rather than copying a register value without understanding it.

For a button panel, both pressing and releasing may matter. A “pressed” event can activate a command, while a “released” event allows the next press to be recognised. For an alarm input, the current asserted level may matter more than the number of transitions.

Write those requirements in plain language before implementing them. For example: “One accepted press advances one menu item; holding the button does not repeat; a release must be accepted before the next press.” That sentence provides a testable contract.

Distinguish capture from current state

INTCAP retains a port snapshot from an interrupt; GPIO reports the port state. Reading either clears the captured interrupt condition, subject to the configured level condition. INTF identifies interrupt sources. A default-comparison condition can remain asserted while the input still differs.

These registers are not an event FIFO. If two transitions occur while software is occupied, do not assume two separately timestamped records will be waiting. Design this panel for human-operated switches, not as a substitute for a hardware pulse counter.

A useful diagnostic log records the notification time, captured state and later confirmed state separately. If those differ, the record shows that the input changed during servicing; it need not mean the bus returned corrupt data.

Keep the controller interrupt handler short

Arduino's attachInterrupt documentation recommends short interrupt service routines and explains restrictions on timing functions. For this design, let the handler set a notification flag and return. Perform I2C transactions, serial printing and button interpretation in the main task.

Use the platform's appropriate method for sharing state between the handler and main code. A volatile flag may be part of that design, but volatile alone does not make a multi-step read-and-clear operation atomic. Avoid a sequence that accidentally clears a new notification arriving during service.

The expander's ODR option configures the INT output as open-drain; it is not a general open-drain mode for every GPIO. Use a compatible pull-up and controller input level when selecting that arrangement. Check the intended active polarity before attaching the controller's interrupt.

Debounce the application state

Arduino's debounce example explains why mechanical contacts can produce several rapid transitions from one operation. An interrupt detects activity; debouncing decides when the state has become stable enough to accept.

Maintain a candidate state, a time when that candidate last changed and an accepted state. When a raw input differs, update the candidate and its timestamp. Accept it only after it remains stable for a chosen interval. Select that interval from the actual switch behaviour and responsiveness requirement rather than treating one value as universal.

Perform this timing in the normal task, without a blocking delay inside the interrupt handler. Treat a stable transition from released to pressed as one event. If repeat-on-hold is required, implement it as an explicit feature with its own timing.

Service pending work without trapping the application

After reading the expander, recheck pending work and the interrupt line as appropriate. If it remains asserted, investigate the selected comparison mode and current input state. Do not wait indefinitely for a line to release while ignoring communication errors or a legitimately held alarm input.

Use bounded service work and timeouts so display updates and other tasks continue to run. A periodic input check can provide useful recovery for a human-interface panel, but it does not turn the design into a guaranteed lossless recorder of fast pulses.

Test the awkward cases deliberately

Input-panel validation cases
Action Expected observation
One ordinary press and release One accepted press event.
Hold the button No unintended repeated commands.
Press both buttons together Defined behaviour for the combination.
Operate a button during a display update The service delay remains acceptable.
Restart with a button held A documented initial state, without a surprise command.
Simulate a communication error An error path rather than a frozen application.

Keep the raw diagnostic log available until these tests pass. When choosing from the I/O expander IC collection, the interrupt feature is most useful when its hardware behaviour is paired with a clear, tested software event model.

Previous article Battery Power for Arduino Projects: Voltage Range, Runtime and Regulator Heat