Ultrasonic Distance Sensor

The ultrasonic distance sensor allows you to use simple ultrasonic sensors like the HC-SR04 (datasheet, SparkFun) with ESPHome to measure distances. These sensors usually can’t measure anything more than about two meters and may sometimes make some annoying clicking sounds.

This sensor platform expects a sensor that can be sent a trigger pulse on a specific pin and will send out an echo pulse while the measurement is being taken. Because sometimes (for example if no object is detected) the echo is never returned, this sensor has a timeout option which specifies the maximum distance to wait for. If you set this too long, the sensor itself will timeout and it will appear as if there was a valid measurement.

Image
HC-SR04 Ultrasonic Distance Sensor.
Image
# Example configuration entry
sensor:
  - platform: ultrasonic
    trigger_pin: GPIOXX
    echo_pin: GPIOXX
    name: "Ultrasonic Sensor"

Configuration variables

  • trigger_pin (Required, Pin Schema): The output pin to periodically send the trigger pulse to.

  • echo_pin (Required, Pin Schema): The input pin on which to wait for the echo.

  • update_interval (Optional, Time): The interval to check the sensor. Defaults to 60s.

  • All other options from Sensor.

Advanced options:

  • timeout (Optional, float): The number of meters for the timeout. Most sensors can only sense up to 2 meters. Defaults to 2 meters.

  • pulse_time (Optional, Time): The duration for which the trigger pin will be active. Defaults to 10us.

  • id (Optional, ID): Manually specify the ID used for code generation.

See Also