10000 GitHub - 06chaynes/hc-sr04: Rust embedded-hal sensor crate for HC-SR04
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

06chaynes/hc-sr04

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hc-sr04

A platform agnostic driver to interface with the HC-SR04 (ultrasonic distance)

Features

  • Platform agnostic using embedded-hal traits
  • Support for both blocking and non-blocking measurements
  • Distance measurements with accurate timing using embedded clocks

Usage

Add this to your Cargo.toml:

[dependencies]
hc-sr04 = "0.2.0"

Example

For a complete working example using ESP32, check out the examples/esp32-example directory. Here's a basic demonstration of how to use the HC-SR04 ultrasonic distance sensor with the esp-idf-hal crate:

use esp_idf_hal::{delay::Delay, peripherals::Peripherals};
use hc_sr04::HcSr04;
use log::{error, info};

// Create clock implementation for ESP32
struct SystemClock;
impl embedded_timers::clock::Clock for SystemClock {
  type Instant = embedded_timers::instant::Instant32<1_000_000>;
  fn now(&self) -> Self::Instant {
    embedded_timers::instant::Instant32::new(unsafe {
      esp_idf_sys::esp_timer_get_time() as u32
    })
  }
}

// Initialize sensor with ESP32 pins
let peripherals = Peripherals::take().unwrap();
let echo_pin = esp_idf_hal::gpio::PinDriver::input(pins.gpio9).unwrap();
let trigger_pin = esp_idf_hal::gpio::PinDriver::output(pins.gpio10).unwrap();

let clock = SystemClock;
let delay = Delay::new(1000);
let mut sensor = HcSr04::new(trigger_pin, echo_pin, clock, delay);

// Take measurements
match sensor.measure() {
  Ok(distance) => info!("Distance: {} cm ({} mm)", distance.cm(), distance.mm()),
  Err(e) => error!("Error: {:?}", e),
}

How it Works

The driver triggers the HC-SR04 sensor and measures the duration of the echo pulse to calculate distance. It supports:

  • Trigger pulse generation
  • Echo pulse timing measurement
  • Distance calculation
  • Error handling for timeout and pin failures

License

Licensed under either of

at your option.

About

Rust embedded-hal sensor crate for HC-SR04

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%
0