Exercise C: Adaptation Challenge

~15 min

Goal

Extend your I/O expander setup to read a button input over I2C — replicating the GPIO button exercise, but entirely through the I2C I/O expander.

Steps

1. Create a New Project

esp-generate --chip esp32c3 -o unstable-hal -o vscode -o esp-backtrace -o log --headless i2c_challenge
cd i2c_challenge

2. Configure an Input Pin

Using the TCA6424 registers:

  • Set a pin's configuration bit to 1 (input)
  • This pin should be connected to a button on the uFerris board

3. Read the Button State

  • Read the input register for the relevant bank
  • Check the bit corresponding to your input pin
  • Detect when the button is pressed

4. Control the LED

Combine input reading with output control:

  • Read the button state from the I/O expander
  • When pressed, turn on the LED (on the I/O expander)
  • When released, turn off the LED

5. Build and Flash

cargo build --release
espflash flash target/riscv32imc-unknown-none-elf/release/i2c-button --monitor

What to Notice

  • The logic is identical to the direct GPIO exercise — only the hardware interface changed
  • All communication goes through I2C read/write operations
  • Think about latency: how does I2C polling compare to direct GPIO polling?
  • Could you use interrupts with the I/O expander? (The TCA6424 has an INT pin)