ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ultrasonic_sensor.cpp
Go to the documentation of this file.
1#include "ultrasonic_sensor.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome {
6namespace ultrasonic {
7
8static const char *const TAG = "ultrasonic.sensor";
9
11 this->trigger_pin_->setup();
12 this->trigger_pin_->digital_write(false);
13 this->echo_pin_->setup();
14 // isr is faster to access
16}
18 this->trigger_pin_->digital_write(true);
20 this->trigger_pin_->digital_write(false);
21
22 const uint32_t start = micros();
23 while (micros() - start < timeout_us_ && echo_isr_.digital_read())
24 ;
25 while (micros() - start < timeout_us_ && !echo_isr_.digital_read())
26 ;
27 const uint32_t pulse_start = micros();
28 while (micros() - start < timeout_us_ && echo_isr_.digital_read())
29 ;
30 const uint32_t pulse_end = micros();
31
32 ESP_LOGV(TAG, "Echo took %" PRIu32 "µs", pulse_end - pulse_start);
33
34 if (pulse_end - start >= timeout_us_) {
35 ESP_LOGD(TAG, "'%s' - Distance measurement timed out!", this->name_.c_str());
36 this->publish_state(NAN);
37 } else {
38 float result = UltrasonicSensorComponent::us_to_m(pulse_end - pulse_start);
39 ESP_LOGD(TAG, "'%s' - Got distance: %.3f m", this->name_.c_str(), result);
40 this->publish_state(result);
41 }
42}
44 LOG_SENSOR("", "Ultrasonic Sensor", this);
45 LOG_PIN(" Echo Pin: ", this->echo_pin_);
46 LOG_PIN(" Trigger Pin: ", this->trigger_pin_);
47 ESP_LOGCONFIG(TAG,
48 " Pulse time: %" PRIu32 " µs\n"
49 " Timeout: %" PRIu32 " µs",
50 this->pulse_time_us_, this->timeout_us_);
51 LOG_UPDATE_INTERVAL(this);
52}
54 const float speed_sound_m_per_s = 343.0f;
55 const float time_s = us / 1e6f;
56 const float total_dist = time_s * speed_sound_m_per_s;
57 return total_dist / 2.0f;
58}
60void UltrasonicSensorComponent::set_pulse_time_us(uint32_t pulse_time_us) { this->pulse_time_us_ = pulse_time_us; }
61void UltrasonicSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; }
62
63} // namespace ultrasonic
64} // namespace esphome
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual ISRInternalGPIOPin to_isr() const =0
constexpr const char * c_str() const
Definition string_ref.h:69
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void set_timeout_us(uint32_t timeout_us)
Set the timeout for waiting for the echo in µs.
GPIOPin * trigger_pin_
Helper function to convert the specified distance in meters to the echo duration in µs.
void set_pulse_time_us(uint32_t pulse_time_us)
Set the time in µs the trigger pin should be enabled for in µs, defaults to 10µs (for HC-SR04)
void setup() override
Set up pins and register interval.
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:30