Ultrasonic UART sensor

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.

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO1
    echo_pin: GPIO3
    update_interval: 60s
    pulse_time: 10us
    timeout: 6m
    name: "Distance"
    filters:
      - 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!

logger:
  level: VERY_VERBOSE

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.

TIA.

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…?

TIA

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

sensor:
  - platform: template
    sensors:
      tank_level_percent:
        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
Gruß
Winfried

Can you show the esphome code full?

I am struggling here trying to get this done

Sorry mate, I have since moved away from this setup and I no longer have this config.

Hold the phone! Found it!

esphome:
  name: waste_tank_level
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "wifissid"
  password: "passord"
 
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Waste Tank Level"
    password: "passord"

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "enkeltpass"

ota:
  password: "enkeltpass"

captive_portal:

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO32
    echo_pin: GPIO33
    name: "Waste Tank Level"
    update_interval: 5s
    pulse_time: 10us
    timeout: 20m
    accuracy_decimals: 3

Not that this is a few years old so things might have changed.

Sure mate I will give a try, thnk you so much.

Yeah no success here with this code also.

I dont know what I am doing wrong here with A02YYUW PWM version.

Tried a lot of different codes :confused:

I will open a topic maybe if someone could help me

Yeah, try that. Make sure all cables are connected well :slight_smile:

Good luck!