Exercise B: Button Input
~20 min
Goal
Read a physical button press and use it to control the LED. You'll need to find the Input abstraction and configure it correctly for the uFerris hardware.
Steps
1. Create a New Project
esp-generate --chip esp32c3 -o unstable-hal -o vscode -o esp-backtrace -o log --headless gpio_button
cd gpio_button
2. Find the Input Abstraction
Navigate to the esp-hal GPIO documentation.
- Find the
Inputstruct - Read what arguments its constructor takes
- Look at what configuration options are available
3. Understand the Hardware
The uFerris button is wired to pull the pin to ground when pressed:
- When not pressed: pin floats (needs pull-up to read high)
- When pressed: pin is pulled low
This means you need:
- Pull-up resistor configuration (internal)
- Detect a low level to know the button is pressed
4. Configure the Input
Apply the mental model:
- Instantiate — Create an
Inputinstance for the button pin - Configure — Set the pull resistor to pull-up
Check the InputConfig struct and its methods. How do you set the pull direction?
5. Read the Button and Control the LED
Find the method on Input that lets you read the current pin state. Use it to:
- Detect when the button is pressed (low)
- Turn on the LED when pressed
- Turn off the LED when released
6. Build and Flash
cargo build --release
espflash flash target/riscv32imc-unknown-none-elf/release/button --monitor
What to Notice
- How does
Inputconfiguration compare toOutputconfiguration? - The
Inputstruct has multiple methods for reading state