https://github.com/marcoradocchia/hc-sr04
Raspberry Pi Rust driver for the HC-SR04 ultrasonic distance sensor.
https://github.com/marcoradocchia/hc-sr04
hc-sr04 raspberry-pi raspberrypi rust ultrasonic-distance-sensor ultrasonic-sensor
Last synced: about 1 month ago
JSON representation
Raspberry Pi Rust driver for the HC-SR04 ultrasonic distance sensor.
- Host: GitHub
- URL: https://github.com/marcoradocchia/hc-sr04
- Owner: marcoradocchia
- License: gpl-3.0
- Created: 2022-09-21T21:38:22.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-07-06T04:55:21.000Z (10 months ago)
- Last Synced: 2025-03-17T20:01:46.955Z (about 1 month ago)
- Topics: hc-sr04, raspberry-pi, raspberrypi, rust, ultrasonic-distance-sensor, ultrasonic-sensor
- Language: Rust
- Homepage:
- Size: 32.2 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
HC-SR04






This crate provides a driver for the **HC-SR04**/**HC-SR04P** ultrasonic
distance sensor on *Raspberry Pi*, using
[rppal](https://docs.rs/rppal/0.13.1/rppal/) to access Raspberry Pi's GPIO.## Examples
Usage examples can be found in the
[examples](https://github.com/marcoradocchia/hc-sr04/tree/master/examples)
folder.## Measure distance
```rust
use hc_sr04::{HcSr04, Unit};// Initialize driver.
let mut ultrasonic = HcSr04::new(
24, // TRIGGER -> Gpio pin 24
23, // ECHO -> Gpio pin 23
Some(23_f32) // Ambient temperature (if `None` defaults to 20.0C)
).unwrap();// Perform distance measurement, specifying measuring unit of return value.
match ultrasonic.measure_distance(Unit::Meters).unwrap() {
Some(dist) => println!("Distance: {.2}m", dist),
None => println!("Object out of range"),
}
```## Calibrate measurement
Distance measurement can be calibrated at runtime using the `HcSr04::calibrate`
method that this library exposes, passing the current ambient temperature as
`f32`.```rust
use hc_sr04::{HcSr04, Unit};// Initialize driver.
let mut ultrasonic = HcSr04::new(24, 23, None).unwrap();// Calibrate measurement with ambient temperature.
ultrasonic.calibrate(23_f32);// Perform distance measurement.
match ultrasonic.measure_distance(Unit::Centimeters).unwrap() {
Some(dist) => println!("Distance: {.1}cm", dist),
None => println!("Object out of range"),
}
```