Water level sensor "measurement timed out"

Hello everybody

I am busy with my first ESPHome project: a water level sensor. I am working by this tutorial. Instead of the NodeMCU I am using this D1 Mini ESP8266 and a JSN SR04 M-2 Ultrasonic Sensor.
So far the sensor is online in my ESPHome addon, but all I am getting in the logs is “Distance measurement timed out!”. I followed the little trouble shooting section in the tutorial (correct pins… well, I think so at least, distance between 20-400cm away from the water level), but no success yet.

This is the log:

INFO Reading configuration /config/esphome/tanklevel.yaml...
INFO Starting log output from tanklevel.local using esphome API
INFO Successfully connected to tanklevel.local
[21:12:42][I][app:102]: ESPHome version 2023.4.4 compiled on May  8 2023, 20:54:23
[21:12:42][C][wifi:505]: WiFi:
[21:12:42][C][wifi:363]:   Local MAC: BC:FF:4D:F6:1D:7D
[21:12:42][C][wifi:364]:   SSID: [redacted]
[21:12:42][C][wifi:365]:   IP Address: 192.168.0.43
[21:12:42][C][wifi:366]:   BSSID: [redacted]
[21:12:42][C][wifi:368]:   Hostname: 'tanklevel'
[21:12:42][C][wifi:370]:   Signal strength: -75 dB ▂▄▆█
[21:12:42][C][wifi:374]:   Channel: 10
[21:12:42][C][wifi:375]:   Subnet: 255.255.255.0
[21:12:42][C][wifi:376]:   Gateway: 192.168.0.1
[21:12:42][C][wifi:377]:   DNS1: 192.168.0.1
[21:12:42][C][wifi:378]:   DNS2: 0.0.0.0
[21:12:42][C][logger:294]: Logger:
[21:12:42][C][logger:295]:   Level: DEBUG
[21:12:42][C][logger:296]:   Log Baud Rate: 115200
[21:12:42][C][logger:297]:   Hardware UART: UART0
[21:12:42][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Acid Tank C Level'
[21:12:42][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[21:12:42][C][ultrasonic.sensor:045]:   Unit of Measurement: '%'
[21:12:42][C][ultrasonic.sensor:045]:   Accuracy Decimals: 1
[21:12:42][C][ultrasonic.sensor:045]:   Icon: 'mdi:water-percent'
[21:12:42][C][ultrasonic.sensor:046]:   Echo Pin: GPIO12
[21:12:42][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO14
[21:12:42][C][ultrasonic.sensor:048]:   Pulse time: 10 µs
[21:12:42][C][ultrasonic.sensor:049]:   Timeout: 11661 µs
[21:12:42][C][ultrasonic.sensor:050]:   Update Interval: 30.0s
[21:12:42][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Remaining Acid in Tank C'
[21:12:42][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[21:12:42][C][ultrasonic.sensor:045]:   Unit of Measurement: 'l'
[21:12:42][C][ultrasonic.sensor:045]:   Accuracy Decimals: 1
[21:12:42][C][ultrasonic.sensor:045]:   Icon: 'mdi:arrow-expand-vertical'
[21:12:42][C][ultrasonic.sensor:046]:   Echo Pin: GPIO12
[21:12:42][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO14
[21:12:42][C][ultrasonic.sensor:048]:   Pulse time: 10 µs
[21:12:42][C][ultrasonic.sensor:049]:   Timeout: 11661 µs
[21:12:42][C][ultrasonic.sensor:050]:   Update Interval: 30.0s
[21:12:42][C][mdns:108]: mDNS:
[21:12:42][C][mdns:109]:   Hostname: tanklevel
[21:12:42][C][ota:093]: Over-The-Air Updates:
[21:12:42][C][ota:094]:   Address: tanklevel.local:8266
[21:12:42][C][ota:097]:   Using Password.
[21:12:42][C][api:138]: API Server:
[21:12:42][C][api:139]:   Address: tanklevel.local:6053
[21:12:42][C][api:143]:   Using noise encryption: NO
[21:12:42][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal Sensor'
[21:12:42][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[21:12:42][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[21:12:42][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[21:12:42][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[21:12:48][D][sensor:109]: 'WiFi Signal Sensor': Sending state -75.00000 dBm with 0 decimals of accuracy
[21:13:06][D][ultrasonic.sensor:036]: 'Remaining Acid in Tank C' - Distance measurement timed out!
[21:13:07][D][ultrasonic.sensor:036]: 'Acid Tank C Level' - Distance measurement timed out!

Since the labelling on the board of the sensor is a little bit different as shown in the tutorial and I am using the D1 mini, some pictures of the wiring:


The sensor


The D1 Mini with 5V - 5V and GND - G


The D1 Mini with RX - D5 and TX - D6

I swapped the RX and TX with the D5 and D6 pins, but still no change.

Here is my yaml:

esphome:
  name: tanklevel
  platform: esp8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "123456789"

wifi:
  networks:
  - ssid: "Tank"
    password: "whatever"
  - ssid: "Office"
    password: "whatever"
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-tanklevel"
    password: "tanklevel"
    
# Acid Level Sensor
    
sensor:
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 60s          #to display WiFi signal strength
    
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 1
    update_interval: 30s
    name: "Acid Tank C Level"              #You may change the tank name
       
    filters:
      - lambda: return (1-((x-.10)/1.10))*100;
      - filter_out: nan
      
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    update_interval: 30s     #5 minutes. You can change this value
    name: "Remaining Acid in Tank C"
    unit_of_measurement: "l"
    accuracy_decimals: 1
    
    filters:
      - lambda: return (1-((x-.10)/1.10))*1000;
      - filter_out: nan

Does anybody have an idea what could be the problem? Am I messing it up with the wiring, or some stupid newby mistake… I would be thankful for any advice.

Some things you could try:

  1. Comment out one of the two sensor configs. It’s a little odd to set it up like that. Usually you’d use a template sensor or a copy sensor if you needed to duplicate/derive sensors.

  2. Have a tinker with pulse_time and timeout.

  1. Triple check all your wiring, especially grounds. Try your best quality cables and power supply (from phone etc). Try an esp32. Try soldering. Try other GPIOs.

Thank you for your advice. I had a quick session with commenting out the second sensor, the pulse_time and timout. No change though.
I will solder the wires directly to the board. Why were you suggesting an ESP32?

Have you tried using GPIO names instead of D names for the pins? Pin D5 would be GPIO14 while pin D6 would be GPIO12.

If that doesn’t work, try using pins D1 and D2. They would be GPIO5 and GPIO4, respectively.

1 Like

Sometimes just moving to a higher quality board with more resources can help. Especially if you’re using cheap clones. No guarantees though. Trying another board is just a trouble shooting step I usually do as part of “hardware isolation testing”.

Take a look here.

I had the exact same problem, “Distance measurement timed out!”. No amount of Google-ing produced any answers.

Long story short, I finally noticed the line in the log:

Timeout: 11661 µs

and thought that looked like a strange choice for a default value. Checked the ESPHome documentation for the Ultrasonic Sensor and was even more confused. Quote:

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

But, the timeout value in the log is denoted as microseconds, not meters. It’s supposed to be the amount of time that the ultrasonic sensor waits for a measurement of the ping time, the echo.

So, I tried adding a timeout value in the yaml - wouldn’t allow me to specify a value in microseconds, had to be meters. I added:

timeout: 20m

then compiled and installed. Guess what? Problem solved, the sensor worked perfectly and started retrieving real values for distance.

The log now shows:

Timeout: 116618 µs

Something’s borked in the ESPHome code for the ultrasonic sensor component, might log a bug report about it.

Anyway, it worked for me - give it a try.

2 Likes

I just tried it, and had the some behaviour. But no success :pensive: Just to make sure, here is my yaml:

esphome:
  name: acidtanklevel
  platform: esp8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "334210fbabf6aa3e4702ee0cdcd21495"

wifi:
  networks:
  - ssid: "Pumproom"
    password: "prpw"
  - ssid: "Office"
    password: "offpw"
    
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-tanklevel"
    password: "tanklevel"
    
sensor:
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 60s          #to display WiFi signal strength
# Acid Level Sensor    
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 1
    timeout: 20m
    update_interval: 30s
    name: "Acid Tank Level"              #You may change the tank name
       
    filters:
      - lambda: return (1-((x-.10)/1.10))*100;
      - filter_out: nan

If that is all correct, the only thing for me to try is taking a whole new set and sold it together again.

The only relevant difference between your config and mine is that I’ve added:

pulse_time: 10 µs

That’s the default, but I added it while attempting to diagnose the problem because I’d read of some boards needing a longer pulse time so I tried 20. Didn’t help so I changed it back and left it in.

You’re not using the same board that I am, same “family” though. Mine has an on board LED that lights when it receives the trigger pulse. That allowed me to confirm my connections were correct.

Most of these boards support multiple operating modes, often set by the absence or presence of a resistor, and the value of that resistor. My board has a point labelled R19 with no component on it. That makes it operate in trigger/echo mode. Adding a resistor of a specific value switches it to continuous mode where it makes and sends readings via serial comms. That’s why your board has the TX/RX labels on the pins (mine has Trig/Echo). Might be worth checking the data sheet for your board to confirm it’s in the correct mode.

Hello friend

First of all, thank you for taking your time to help me. This is definitly not plug & play. I added

pulse_time: 10 µs

Strangely though the editor did not want to accept “µs” as a unit, so I had to convert to s:

- platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 1
    timeout: 20m
    pulse_time: 0.00001 s
    update_interval: 30s
    name: "Acid Tank C Level"

But still, all I am getting is:

[10:09:41][C][logger:301]: Logger:
[10:09:41][C][logger:302]:   Level: DEBUG
[10:09:41][C][logger:303]:   Log Baud Rate: 115200
[10:09:41][C][logger:305]:   Hardware UART: UART0
[10:09:41][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Acid Tank C Level'
[10:09:41][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[10:09:41][C][ultrasonic.sensor:045]:   Unit of Measurement: '%'
[10:09:41][C][ultrasonic.sensor:045]:   Accuracy Decimals: 1
[10:09:41][C][ultrasonic.sensor:045]:   Icon: 'mdi:water-percent'
[10:09:41][C][ultrasonic.sensor:046]:   Echo Pin: GPIO12
[10:09:41][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO14
[10:09:41][C][ultrasonic.sensor:048]:   Pulse time: 0 µs
[10:09:41][C][ultrasonic.sensor:049]:   Timeout: 116618 µs
[10:09:41][C][ultrasonic.sensor:050]:   Update Interval: 30.0s
[10:09:41][D][api:102]: Accepted 192.168.0.178
[10:09:41][C][mdns:108]: mDNS:
[10:09:41][C][mdns:109]:   Hostname: tanklevel
[10:09:41][C][ota:093]: Over-The-Air Updates:
[10:09:41][C][ota:094]:   Address: tanklevel.local:8266
[10:09:41][C][ota:097]:   Using Password.
[10:09:41][D][api.connection:959]: Home Assistant 2023.5.4 (192.168.0.178): Connected successfully
[10:09:41][C][api:138]: API Server:
[10:09:41][C][api:139]:   Address: tanklevel.local:6053
[10:09:41][C][api:143]:   Using noise encryption: NO
[10:09:41][C][wifi_signal.sensor:009]: WiFi Signal 'WiFi Signal Sensor'
[10:09:41][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[10:09:41][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[10:09:41][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[10:09:41][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[10:09:54][D][ultrasonic.sensor:036]: 'Acid Tank C Level' - Distance measurement timed out!

I find the “Pulse time: 0 µs” very strange, what do you think?

Otherwise, the board LED is flickering diligently:

I will test your advice with the operating modes soon.

Yeah, the “pulse time: 0” is probably an issue. You’re definitely entering “μs” not “us”?

Dear friend

After some months with the parts on my desk, I gave it another try:

  • soldered another D1 together
  • got another sensor
  • added pulse_time: 0.00001s
  • tried pulse_time: 10μs but compiler is complaining

All with the same result: Pulse time: 0 µs & Distance measurement timed out!

The only thing that is still left is to get better hardware. Do you maybe still have an idea?

PS: I just stumbled about the units… do I have to add a space after the number? E.g. 10μs or 10 μs?

…just a friendly comment for future noobs reading this thread, like me, to clarify the statement above “the compiler is complaining” – one must use “10us” with a lower case “U”, and not “10μs” with the Greek letter “mu”, when editing the yaml file… and no space between the number and the units… :slight_smile:

3 Likes

Did you ever fix this problem ?

I have a similar problem with my set up

  • HC-SR04
  • D1Mini ESP8266
  • in Home Assistant environment

Let me know where you are at please !!!

Sorry my friend, not yet. Lots of other things & projects. I am (almost) it is not a problem in the code but with the hardware. I should solder a new device together and give it another try…

THANK YOU! I was beating my head against the wall and I can’t believe this fixed it.

Thanks for this post.
Also had a lot of trouble.
Increasing the timeout as indicated fixed it.

There’s no problem with that timeout value. The integration is making it convenient to put the distance in and it’s converting it to a time value… 2m takes sound 0.005831 seconds, so for the round trip it’s 0.011661 seconds which is what you see in the log.

datasheet mode operator :

default R27 void : MoDE 1

i think mode echo & trig… ???

my code :slight_smile:

  • platform: ultrasonic
    trigger_pin: GPIO15
    echo_pin: GPIO13
    name: “Livello Tanica”
    id: livello_tanica
    pulse_time: 15 us
    update_interval: 1s
    unit_of_measurement: “cm”
    accuracy_decimals: 1
    timeout: 20m # Aumenta il timeout a 20 metri

hey, I have tried adding the pulse time and timeout in meters as stated above, but no luck, still having the same problem as OP. My tank level is unknown.

Log:

[14:36:32][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'Water Tank Level'
[14:36:32][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[14:36:32][C][ultrasonic.sensor:045]:   Unit of Measurement: '%'
[14:36:32][C][ultrasonic.sensor:045]:   Accuracy Decimals: 0
[14:36:32][C][ultrasonic.sensor:045]:   Icon: 'mdi:arrow-expand-vertical'
[14:36:32][C][ultrasonic.sensor:046]:   Echo Pin: GPIO4
[14:36:32][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO5
[14:36:32][C][ultrasonic.sensor:048]:   Pulse time: 15 µs
[14:36:32][C][ultrasonic.sensor:049]:   Timeout: 174927 µs
[14:36:32][C][ultrasonic.sensor:050]:   Update Interval: 1800.0s

code:

  - platform: ultrasonic
    trigger_pin:
      number: GPIO5
      allow_other_uses: true
    echo_pin: 
      number: GPIO4
      allow_other_uses: true
    name: "Water Tank Level"
    unit_of_measurement: "%"
    accuracy_decimals: 0
    timeout: 30m
    pulse_time: 15us
    update_interval: 1800000ms
    filters:
      - lambda: |-
          float value = ((((x * 100) - 73) - (206 - 73)) / (206 - 73)) * -100;
          return value > 100 ? 100 : value;
      - filter_out: nan```