Adding Buttons and Indicators with an MCP23017 I/O Expander
A control panel can run out of microcontroller pins long before it runs out of useful features. An I/O expander moves additional digital connections onto a serial bus, leaving the main controller to manage the application. It is a good fit for many buttons and status indicators, provided the project can tolerate bus-based access rather than direct processor-pin timing.
This guide uses the MCP23017 breakout board from the I/O expander collection. It focuses on a practical control panel and a pin restriction that older examples may overlook.
Check the current device documentation
The MCP23017 uses I2C; the similarly named MCP23S17 uses SPI. Both are described as 16-bit expanders, but the current Microchip MCP23017/MCP23S17 datasheet identifies GPA7 and GPB7 as output-only on the MCP23017. Do not plan sixteen button inputs from an older generic “16 I/O” description.
Reserve those two pins for suitable outputs or leave them unused, and check the documentation and errata for the exact device you receive. A library presenting sixteen configurable pin numbers does not override a hardware restriction.
Create the pin budget before drawing the panel. Count inputs and outputs separately, including spare functions and any interface requirements that must remain on the main controller.
Choose tasks that suit an expander
| Function | Suitability question |
|---|---|
| Push-buttons and switches | Can the application sample and debounce them at a suitable rate? |
| Status LEDs | Are the load and current-limiting arrangements appropriate? |
| Fast pulse counting | Would a direct hardware counter or interrupt input be more suitable? |
| Precisely timed waveform output | Does the application need a dedicated timer or PWM driver? |
| External actuator control | Is an appropriate driver and safe default state provided? |
A pin expander is not a replacement for every function attached to a microcontroller pin. Reading a bus transaction takes time, and other bus traffic can delay the next update. Keep timing-critical tasks on hardware designed for them.
Plan the bus and supply together
Check the breakout's supply arrangement and the voltage used by its I2C pull-ups. The controller, expander and any other devices must share compatible bus levels or use suitable translation. A board accepting one supply voltage is not automatically compatible with every controller's signal pins.
Identify the address-select connections and set them deliberately. If multiple boards are used, record each address and its panel role. An address scan is useful for confirming acknowledgements, but it does not prove that the board is correctly configured or that every external connection works.
Start with one expander and short wiring. Add other bus devices only after the first panel works. Check the combined pull-up arrangement rather than assuming that every breakout can contribute its own resistors without affecting the bus.
Give every button a defined inactive state
A switch input needs an intentional bias when the switch is open. Use a suitable internal or external pull arrangement as supported by the device and circuit. Record whether a pressed button reads high or low, and keep that convention consistent in the application.
For a first test, connect one button to one permitted input and display the raw state. Confirm both open and closed readings before adding debounce. If the state flickers while the button is untouched, investigate the electrical connection rather than increasing the debounce time to hide it.
Then add software debounce appropriate to the interface. Treat a stable transition as an event so a held button does not repeatedly execute an action unless repeat behaviour is deliberately wanted.
Use interrupts as notifications
The device provides configurable interrupt outputs and capture facilities described on Microchip's MCP23017 product page. An interrupt can tell the controller that an input condition needs attention, reducing unnecessary polling.
Design the handling routine to read the relevant state and clear the condition according to the datasheet. Keep a short processor interrupt handler where appropriate, and perform bus work in a controlled part of the main program rather than assuming every library is safe to call from an interrupt.
Test simultaneous button changes and a held input. An interrupt is not a promise that every brief edge will be preserved as a separate event. If that distinction matters, use a more suitable capture mechanism.
Drive indicators within a complete current budget
Use current limiting for LEDs and suitable drivers for larger loads. The datasheet's absolute-maximum pin current is not a normal design target, and the package also has total supply and return limits. Do not multiply a per-pin headline figure by sixteen to size the load.
For a panel, measure the all-indicators-on condition as well as ordinary use. A lamp-test function may be the highest-demand state. Check the voltage and logic levels while that pattern is active.
Keep motors, relay coils and other substantial loads out of the direct expander outputs unless an explicitly suitable interface circuit is provided. The breakout makes connections convenient; it does not add a power stage.
Make startup and reset predictable
Specify the desired inactive state for every output. Initialise the expander deliberately and verify the actual sequence from power-up to normal operation. Where supported, establish intended output-latch values before enabling output directions, and confirm the behaviour on the bench.
Test a controller reset while the expander remains powered, then test the reverse. The two devices may not restart together. Reapply the required configuration after the relevant reset condition rather than relying on the last successful run.
If a display or indicator controls an important user decision, provide an observable fault state when communication fails. A stale “ready” LED can be more misleading than an obviously blank panel.
Keep the software mapping readable
Define names such as START_BUTTON, MENU_BUTTON and READY_LED instead of scattering numeric expander pins through the program. Keep the hardware mapping in one place, including the device address and port identity.
Microchip's GPIO expander feature application note provides further context on the family's register features. Use a library or register implementation that matches the current device documentation, and preserve a small diagnostic program for future repairs.
Finally, test each control against a labelled wiring schedule and photograph the assembled panel. A good I/O expansion design provides more than extra pins: it gives every input a defined state, every output a checked load and the application a clear response to resets and communication faults.