Ultrasonic UART sensor

Tried something different. Installed the code from the manufacturer on Arduino IDE, and that works just fine. Same PINs. GPIO4 and GPIO5. Still the same ESP32.

Exactly the same setup as I had with ESPHome. Something crazy is going on.

Do you know if the ultrasonic sensor use the same (-ish) code to measure in the ESPHome sensor?

void loop()
  // The sensor is triggered by a falling edge of a HIGH pulse that 
  // is more than 60 microseconds in duration.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIGGER_PIN, LOW);
  digitalWrite(TRIGGER_PIN, HIGH);
  digitalWrite(TRIGGER_PIN, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  // If no object detected, fixed pulse width of 35ms is sent
  // by the sensor.
  duration = pulseIn(PWM_OUTPUT_PIN, HIGH);
  // Convert the pulse width duration into a distance
  distance = duration;
  distance = distance / 58;
  Serial.println(" cm");

Thanks. I will play with this a bit and see.

Just got a feedback from the ebay seller.

You cannot use HCSR04 code directly for this sensor.

There are subtle differences.

Please check the github link in the eBay description of this item.

I have provided full PWM code along with connection diagram.

So I guess that answers it. :slight_smile: I have to use my own code. Will see if I can make that somehow.

After heaps of debugging we found it. The default timeout is very wrong. I ended up with this YAML

  - platform: ultrasonic
    trigger_pin: GPIO4
    echo_pin: GPIO5
    name: "Ultrasonic Sensor"
    update_interval: 5s
    pulse_time: 10us
    timeout: 20m

With no timeout setting this is the default. (extra debugging added)

This is with timeout set to 20m.

The timeout setting and default has to be very wrong, or at lest for this sensor. Default of 5830us has to be low also for the SR04?


Hello corvy,
I’m trying to do the same thing.
Did you finally make it work with the resistors that tom_I suggested you to add or just by connecting the sensor to the ESP32 pins ?
Thanks :slight_smile:

Hello, I changed the timeout and added the resistors. Hope you make it work!

My build is finally completed. The sensor is fitted to a black water tank in my boat and it works just perfectly. :slight_smile: Thanks for the help and thanks to #third-party:esphome for a great product!

Screenshot 2020-07-28 10.10.35

1 Like

I ran into the same issue with the US-100 sensor (https://www.banggood.com/3pcs-US-100-Ultrasonic-Ranging-Module-with-Temperature-Compensated-Sensor-Dual-Mode-Serial-Port-p-1589413.html) - the fix was the same - setting timeout: 20m sorted it out for me. Putting this here so that others might find it quicker if they run into this.


Please can you tell me the size of your tank? I have been struggling to get a reliable reading with a narrow steel tank and I suspect that the sound is reflecting off of the sides.

I’ve noticed a interesting issue and fix when trying to set up a ultrasonic sensor, that I thought I would share.

While trying to setup a ultrasonic sensor using ESPHOME, it was only providing a distance value when the ESPHOME first booted. From then on it would report the “Distance measurement timed out!” error, soft restarting/rebooting the ESPHOME device and it would report a new value once and stop again. Here is my sensor setup. Nothing too fancy.

  - platform: ultrasonic
    trigger_pin: GPIO1
    echo_pin: GPIO3
    update_interval: 60s
    pulse_time: 10us
    timeout: 6m
    name: "Distance"
      - filter_out: nan

Oddly while trying to work out what was wrong, I was increasing the logging level to see what was happening. When I set logging to very verbose, the sensor started working as expected, updating every 60 seconds, yet changing the logging to a lower setting and it stops working!?

Anyone else seen this issue or know what is happening?

If your unit is stuck in the “Distance measurement timed out!” error loop, try adding this to see what happens. Worked for me!


My tank is 160 liters. Sorry for not replying sooner!

Try to increase the timeout setting.

Good work on getting it to work… can I just check whether you managed to get the UART running, or just the PWM one?

Just waited well over a month for the former to arrive, and will be slightly gutted if it ends up not being compatible with ESPHome.


Only the PWM one I got to work :slight_smile: Never bothered to investigate more on the UART one…

Ach! Never mind. Thanks for getting back. Have ordered a PWM version, which should be here by…the end of January :neutral_face:. May well be back around then. Happy boating until then!

1 Like

Just got my analogue sensor through. Plugged into my ESP32 with the pin arrangement (but no resistors, as didn’t have any), and it’s working perfectly. Seems really quite stable.

One question: did you manage to construct a good template for turning the distance into a percentage…?


Hello Ross, I made a template sensor in HomeAssistant for this purpose.

  - platform: template
        entity_id: sensor.tank_level
        # Tank empty at 23cm, full at 2cm, 21cm range of %
        value_template: "{{100 + ((0.21 - ((states('sensor.tank_level'))|float - 0.02) / 0.21 * 1>
        unit_of_measurement: '%'
        friendly_name: Tank
        icon_template: 'mdi:ruler'

Hope this helps :slight_smile:

1 Like

Hallo Corvy,
können Sie vielleicht ihr Build einmal komplett vorstellen (Code und Aufbau). Ich bin bereits 74 Jahre alt und nicht mehr ganz so fit, insbesondere mit Arduino da ich mich erst seit einiger Zeit damit beschäftige.
Anscheinend habe Sie eine Drahtlosversion erstellt, mit WiFi oder Blutooth. Das ist interessant wenn bei nachträglichen Einbau keine Möglichkeit der Kabelverlegung existiert.
Ich freune mich auf Ihre Veröffentlichung, bzw. auch per PN