| Crates.io | hcsr04-gpio-cdev |
| lib.rs | hcsr04-gpio-cdev |
| version | 0.1.4 |
| created_at | 2025-09-30 12:02:44.510092+00 |
| updated_at | 2025-12-22 11:48:38.407179+00 |
| description | gpio-cdev-based driver for HC-SR04 on the Raspberry Pi 5. |
| homepage | |
| repository | https://github.com/andergisomon/hc-sr04-gpio-cdev |
| max_upload_size | |
| id | 1861029 |
| size | 46,071 |
Based on the gpio-cdev crate. Tested on the Raspberry Pi 5B. This was written in a few hours, YMMV.
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(())
}