-
Notifications
You must be signed in to change notification settings - Fork 173
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use esp_idf_hal::delay::FreeRtos; | ||
use esp_idf_hal::gpio::*; | ||
use esp_idf_hal::peripherals::Peripherals; | ||
use log::*; | ||
use esp_idf_svc::systime::EspSystemTime; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not possible to use the |
||
|
||
fn main() { | ||
// Initialize logging and necessary peripherals | ||
esp_idf_sys::link_patches(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
esp_idf_svc::log::EspLogger::initialize_default(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not possible to use |
||
info!("Hello, world!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What can be the configuration error here? Simply |
||
let echo_pin = PinDriver::input(peripherals.pins.gpio5).expect("Error configuring echo pin"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't importesp_idf_hal
. Useesp_idf_svc::hal
instead.