Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ultrasonic Sensor HC-SR04 Example #320

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/ultrasonic_sensor_hcsr04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use esp_idf_hal::delay::FreeRtos;
Copy link
Collaborator

@ivmarkov ivmarkov Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't import esp_idf_hal. Use esp_idf_svc::hal instead.

use esp_idf_hal::gpio::*;
use esp_idf_hal::peripherals::Peripherals;
use log::*;
use esp_idf_svc::systime::EspSystemTime;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible to use the esp-idf-svc crate inside the esp-idf-hal crate. Circular dependency.


fn main() {
// Initialize logging and necessary peripherals
esp_idf_sys::link_patches();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esp_idf_hal::sys

esp_idf_svc::log::EspLogger::initialize_default();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not possible to use esp-idf-svc here so it is not possible to use regular info!, warn! etc, logging. Use println! instead.

info!("Hello, world!");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need that line?


let peripherals = Peripherals::take().unwrap();

// Configure pins for trigger and echo
let mut trigger_pin = PinDriver::output(peripherals.pins.gpio4).expect("Error configuring trigger pin");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What can be the configuration error here? Simply .unwrap()

let echo_pin = PinDriver::input(peripherals.pins.gpio5).expect("Error configuring echo pin");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.



loop {
// Send a 10us pulse to the trigger pin to start the measurement
trigger_pin.set_high().expect("Error: Unable to set trigger pin high");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. These methods are actually infallible. See #140

FreeRtos::delay_us(10);
trigger_pin.set_low().expect("Error: Unable to set trigger pin low");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.


// Wait for the echo pin to go high (start of pulse)
while !echo_pin.is_high() {}

// Measure the duration of the echo pulse (in microseconds)
let start_time = EspSystemTime {}.now().as_micros();
while echo_pin.is_high() {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are busy looping. Isn't this triggering the TWDT? An even more interesting example might be an async one, as it would avoid the busy looping, and the waits there are expressed trivially.

let end_time = EspSystemTime {}.now().as_micros();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.


// Calculate the duration of the echo pulse in microseconds
let pulse_duration = end_time - start_time;

// Calculate the distance based on the speed of sound (approximately 343 m/s)
// Distance in centimeters: duration * speed_of_sound / 2 (since the signal goes to the object and back)
let distance_cm = (pulse_duration as f32 * 0.0343) / 2.0;

// Distance in inches: distance_cm / 2.54 (since 1 inch = 2.54 cm)
let distance_inches = distance_cm / 2.54;

// Print the measured distance in both centimeters and inches
info!("Distance: {:.2} cm, {:.2} inches", distance_cm, distance_inches);

// Wait for a brief moment before taking the next measurement
FreeRtos::delay_ms(1000);
}

}
Loading