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:
OR add the following to your Cargo.toml:
[]
= { = "0.1", = ["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
/// 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
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)