Expand description
§hcsr04
A platform-agnostic, no_std driver for the HC-SR04 ultrasonic distance sensor using embedded-hal and embedded-hal-async traits.
This driver allows you to:
- Measure distance using the HC-SR04 sensor.
- Compensate for temperature effects on distance measurement.
§Installation
Add hcsr04 to your project:
cargo add hcsr04 --features defmtOR add the following to your Cargo.toml:
[dependencies]
hcsr04 = { version = "0.1", features = ["defmt"] }§Optional Cargo features
defmt: Usedefmtlogging to print messages.
§Examples
Usage examples can be found in the examples folder and run on the esp32-c3 board.
- ultrasonic_led: measure distance and control LED based on the distance.
- temperature_compensation: measure distance and compensate for temperature.
§Measure distance
Full code see ultrasonic_led.rs
#[embassy_executor::task]
/// Task to measure distance and control LED based on the distance.
///
/// # Arguments
///
/// * `trig` - The trigger pin of the HC-SR04 sensor.
/// * `echo` - The echo pin of the HC-SR04 sensor.
/// * `led` - The LED pin to control based on the distance.
async fn ultrasonic_led(trig: AnyPin<'static>, echo: AnyPin<'static>, led: AnyPin<'static>) {
info!("Starting ultrasonic led task");
// Init GPIO
let mut led = Output::new(led, Level::Low, OutputConfig::default());
let trig = Output::new(trig, Level::Low, OutputConfig::default());
let echo = Input::new(echo, InputConfig::default().with_pull(Pull::Down));
// Create a HC-SR04 sensor instance
let mut hcsr04 = Hcsr04::builder()
.trig(trig)
.echo(echo)
.delay(Delay)
.temperature(NoTemperatureCompensation)
.build();
loop {
// Measure distance
let distance = match hcsr04.measure_distance().await {
Ok(distance) => distance,
Err(e) => {
error!("Fail to measure {}", e);
continue;
}
};
info!("measure distance value is {} cm.", distance);
if distance < 30.0 {
led.set_high();
} else {
led.set_low();
}
Timer::after_millis(500).await;
}
}§Wokwi
Wokwi provides a simulation solution for embedded and IoT system engineers.
- Install the Wokwi for VS Code extension.
- Press
F1OR⌘ + ⇧ + Pto open the command palette. - Type in
Wokwi: Start Simulator and Wait for Debuggerand press enter. - In the Wokwi simulator, click on the
Debugbutton to start debugging.
§License
Licensed under either of:
- GPL-3.0 license (LICENSE-GPLv3 or https://www.gnu.org/licenses/gpl-3.0.html)
Re-exports§
pub use error::Error;pub use hcsr04::Hcsr04;pub use temperature::NoTemperatureCompensation;pub use temperature::TemperatureProvider;
Modules§
- error
- Error types for HC-SR04 driver
- hcsr04
- HC-SR04 ultrasonic distance sensor driver core implementation
- temperature
- Temperature compensation for HC-SR04 sensor