Giter Site home page Giter Site logo

marcoradocchia / hc-sr04 Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 3.0 33 KB

Raspberry Pi Rust driver for the HC-SR04 ultrasonic distance sensor.

License: GNU General Public License v3.0

Rust 100.00%
hc-sr04 raspberry-pi raspberrypi ultrasonic-distance-sensor ultrasonic-sensor rust

hc-sr04's Introduction

HC-SR04

GitHub source size GitHub open issues GitHub open pull requests GitHub sponsors Crates.io downloads Crates.io version GitHub license

This crate provides a driver for the HC-SR04/HC-SR04P ultrasonic distance sensor on Raspberry Pi, using rppal to access Raspberry Pi's GPIO.

Examples

Usage examples can be found in the examples folder.

Measure distance

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.

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"),
}

hc-sr04's People

Contributors

marcoradocchia avatar rahel-a avatar yonaka43 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.