ESPHome 2026.2.4
Loading...
Searching...
No Matches
ultrasonic_sensor.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/gpio.h"
6
7#include <cinttypes>
8
9namespace esphome::ultrasonic {
10
12 static void gpio_intr(UltrasonicSensorStore *arg);
13
15 volatile uint32_t wait_start_us{0};
16 volatile uint32_t echo_start_us{0};
17 volatile uint32_t echo_end_us{0};
18 volatile bool echo_start{false};
19 volatile bool echo_end{false};
20};
21
23 public:
24 void set_trigger_pin(InternalGPIOPin *trigger_pin) { this->trigger_pin_ = trigger_pin; }
25 void set_echo_pin(InternalGPIOPin *echo_pin) { this->echo_pin_ = echo_pin; }
26
27 void setup() override;
28 void loop() override;
29 void dump_config() override;
30 void update() override;
31
33 void set_timeout_us(uint32_t timeout_us) { this->timeout_us_ = timeout_us; }
35 void set_pulse_time_us(uint32_t pulse_time_us) { this->pulse_time_us_ = pulse_time_us; }
36
37 protected:
39 static float us_to_m(uint32_t us);
41
46 uint32_t timeout_us_{};
47 uint32_t pulse_time_us_{};
48
51};
52
53} // namespace esphome::ultrasonic
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:92
This class simplifies creating components that periodically check a state.
Definition component.h:512
Base-class for all sensors.
Definition sensor.h:43
void set_echo_pin(InternalGPIOPin *echo_pin)
static float us_to_m(uint32_t us)
Helper function to convert the specified echo duration in µs to meters.
void set_trigger_pin(InternalGPIOPin *trigger_pin)
void set_timeout_us(uint32_t timeout_us)
Set the maximum time in µs to wait for the echo to return.
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)
static void gpio_intr(UltrasonicSensorStore *arg)