ESPHome  2024.12.2
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 
5 namespace esphome {
6 namespace ultrasonic {
7 
8 static const char *const TAG = "ultrasonic.sensor";
9 
11  ESP_LOGCONFIG(TAG, "Setting up Ultrasonic Sensor...");
12  this->trigger_pin_->setup();
13  this->trigger_pin_->digital_write(false);
14  this->echo_pin_->setup();
15  // isr is faster to access
17 }
19  this->trigger_pin_->digital_write(true);
21  this->trigger_pin_->digital_write(false);
22 
23  const uint32_t start = micros();
24  while (micros() - start < timeout_us_ && echo_isr_.digital_read())
25  ;
26  while (micros() - start < timeout_us_ && !echo_isr_.digital_read())
27  ;
28  const uint32_t pulse_start = micros();
29  while (micros() - start < timeout_us_ && echo_isr_.digital_read())
30  ;
31  const uint32_t pulse_end = micros();
32 
33  ESP_LOGV(TAG, "Echo took %" PRIu32 "µs", pulse_end - pulse_start);
34 
35  if (pulse_end - start >= timeout_us_) {
36  ESP_LOGD(TAG, "'%s' - Distance measurement timed out!", this->name_.c_str());
37  this->publish_state(NAN);
38  } else {
39  float result = UltrasonicSensorComponent::us_to_m(pulse_end - pulse_start);
40  ESP_LOGD(TAG, "'%s' - Got distance: %.3f m", this->name_.c_str(), result);
41  this->publish_state(result);
42  }
43 }
45  LOG_SENSOR("", "Ultrasonic Sensor", this);
46  LOG_PIN(" Echo Pin: ", this->echo_pin_);
47  LOG_PIN(" Trigger Pin: ", this->trigger_pin_);
48  ESP_LOGCONFIG(TAG, " Pulse time: %" PRIu32 " µs", this->pulse_time_us_);
49  ESP_LOGCONFIG(TAG, " Timeout: %" PRIu32 " µs", this->timeout_us_);
50  LOG_UPDATE_INTERVAL(this);
51 }
53  const float speed_sound_m_per_s = 343.0f;
54  const float time_s = us / 1e6f;
55  const float total_dist = time_s * speed_sound_m_per_s;
56  return total_dist / 2.0f;
57 }
59 void UltrasonicSensorComponent::set_pulse_time_us(uint32_t pulse_time_us) { this->pulse_time_us_ = pulse_time_us; }
60 void UltrasonicSensorComponent::set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; }
61 
62 } // namespace ultrasonic
63 } // namespace esphome
virtual void digital_write(bool value)=0
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
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) ...
GPIOPin * trigger_pin_
Helper function to convert the specified distance in meters to the echo duration in µs...
void set_timeout_us(uint32_t timeout_us)
Set the timeout for waiting for the echo in µs.
virtual void setup()=0
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:27
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void setup() override
Set up pins and register interval.
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
constexpr const char * c_str() const
Definition: string_ref.h:68
virtual ISRInternalGPIOPin to_isr() const =0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28