hcsr04-gpio-cdev

Crates.iohcsr04-gpio-cdev
lib.rshcsr04-gpio-cdev
version0.1.4
created_at2025-09-30 12:02:44.510092+00
updated_at2025-12-22 11:48:38.407179+00
descriptiongpio-cdev-based driver for HC-SR04 on the Raspberry Pi 5.
homepage
repositoryhttps://github.com/andergisomon/hc-sr04-gpio-cdev
max_upload_size
id1861029
size46,071
andergisomon (andergisomon)

documentation

README

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(())
}
Commit count: 0

cargo fmt