In my previous blog, we blinked an LED.
Now, we shall interface a button (inbuilt Flash Button), which is connected to GPOI0 to read button presses.
Getting Started
Let us create our new project
esp-generate --chip=esp32 button_press
Open src/bin/main.rs
file and type the below code
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use esp_backtrace as _;
use esp_hal::{
delay::Delay,
gpio::Level,
gpio::{Input, InputConfig},
gpio::{Output, OutputConfig},
main
};
use esp_println::println;
esp_bootloader_esp_idf::esp_app_desc!();
#[main]
fn main() -> ! {
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut led = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default());
let button = Input::new(peripherals.GPIO0, InputConfig::default());
let delay = Delay::new();
loop {
if button.is_low() {
led.set_high();
println!("Button Pressed");
delay.delay_millis(50);
}
else {
led.set_low();
}
}
}
Into the Code : Line-by-line explanation
#![no_std]
- Removes the standard library to suit bare-metal embedded systems.
#![no_main]
- Disables Rust's default entry point, enabling use of a custom embedded
main
function.
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
- Prevents using
mem::forget
, which can cause resource leaks with peripherals like GPIOs.
use esp_backtrace as _;
- Provides support for backtraces during panics, aiding debugging.
use esp_hal::{
delay::Delay,
gpio::Level,
gpio::{Input, InputConfig},
gpio::{Output, OutputConfig},
main,
};
- Brings in delay functionality, GPIO level enums, input/output pin types, and the custom entry macro.
use esp_println::println;
- Enables logging via
println!
, useful for debugging over serial.
esp_bootloader_esp_idf::esp_app_desc!();
- Embeds metadata (app version, timestamp, etc.) into the firmware.
#[main]
fn main() -> ! {
- Entry point of the application. Uses
-> !
since it loops forever.
let peripherals = esp_hal::init(esp_hal::Config::default());
- Initializes peripherals using default configuration.
let mut led = Output::new(peripherals.GPIO2, Level::Low, OutputConfig::default());
- Configures GPIO2 as an output pin, initially set to Low (LED off).
let button = Input::new(peripherals.GPIO0, InputConfig::default());
- Configures GPIO0 as an input pin (for reading a button's state).
let delay = Delay::new();
- Initializes a delay instance for blocking waits.
loop {
- Begins an infinite loop to continuously check the button and control the LED.
if button.is_low() {
- Checks if the button is pressed (logic Low, assuming pull-up setup).
led.set_high();
- Turns the LED on.
println!("Button Pressed");
- Logs button press to serial console.
delay.delay_millis(50);
- Adds a small delay for button debounce.
}
else {
led.set_low();
}
- If the button is not pressed, turns the LED off.
}
}
Outputs
Terminal Output
Hardware Output
Top comments (0)