Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Handle Input

Using same wiring from the blinky chapter, let's now use external input to make the LED blink.

  • Access exercises/handle_input in order to edit src/main.rs.

Warning

This exercise is slightly more involved, in the sense you'll need to add more code. But there is always the solution if it gets too hard.

Exercise

We will use the button labelled BOOT linked to GPIO9 to toggle the LED.

  1. As per usual, we will be editing src/main.rs.

  2. We first need to fill in the boilerplate. Once that's done, this line might be of help:

    // you will also have other `esp_hal` submodules besides `gpio`.
    +use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull},
    

    You may get inspired by the previous exercises and the boilerplate page.

  3. Create an Output::new passing GPIO7 peripheral, Level::High and default OutputConfig.

    • Assign it to the led variable.
    • Hint: with peripherals initialised, GPIO7 is a field in that struct.
  4. Create an Input::new passing GPIO9 peripheral.

    • Use default InputConfig, overwrite with as_pull(Pull::High).
    • Assign it to the btn variable.
  5. Add the logic inside loop:

    • When pressing it should turn the led on, and delay it 2 seconds.
    • Then it turns itself off.

The examples/handle_input.rs contains a solution. You can run it with the following command cargo run --example handle_input --release.