4 releases
Uses new Rust 2024
| 0.1.4 | Dec 22, 2025 |
|---|---|
| 0.1.3 | Nov 22, 2025 |
| 0.1.2 | Sep 30, 2025 |
| 0.1.1 |
|
| 0.1.0 | Sep 30, 2025 |
#1096 in Hardware support
16KB
192 lines
HC-SR04 driver for the Raspberry Pi 5
Based on the gpio-cdev crate. Tested on the Raspberry Pi 5B. This was written in a few hours, YMMV.
How to use
use hcsr04::*;
use std::{thread::sleep, time::Duration};
const ECHO_PIN: u32 = 20; // GPIO20
const TRIG_PIN: u32 = 21; // GPIO21
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut hcsr04 = HcSr04::new(TRIG_PIN, ECHO_PIN)?;
// let timeout = range_to_timeout(DistanceUnit::Cm(4.0))?;
loop {
let distance = hcsr04.dist_cm(None)?;
println!("Distance: {:05.2}cm", distance.to_val());
sleep(Duration::from_secs_f32(0.2));
}
Ok(())
}
Dependencies
~2MB
~39K SLoC