Ultrasonic Sensor Time Out

I am using a an HC-SR04 sensor with a nodemcu.
My code is

  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Salt Sensor"
    pulse_time: 20us
    timeout: 2.0m
    update_interval: 5s
    filters:
      - filter_out: nan

I keep getting a distance timeout error. This was discussed in github back in March and May with people having similar problems. Tried the fix but the code would not validate.

Here is my log.

[12:38:40][C][logger:175]: Logger:
[12:38:40][C][logger:176]:   Level: DEBUG
[12:38:40][C][logger:177]:   Log Baud Rate: 115200
[12:38:40][C][logger:178]:   Hardware UART: UART0
[12:38:40][C][ultrasonic.sensor:034]: Ultrasonic Sensor 'Salt Sensor'
[12:38:40][C][ultrasonic.sensor:034]:   Unit of Measurement: 'm'
[12:38:40][C][ultrasonic.sensor:034]:   Accuracy Decimals: 2
[12:38:40][C][ultrasonic.sensor:034]:   Icon: 'mdi:arrow-expand-vertical'
[12:38:40][C][ultrasonic.sensor:035]:   Echo Pin: GPIO15 (Mode: INPUT)
[12:38:40][C][ultrasonic.sensor:036]:   Trigger Pin: GPIO12 (Mode: OUTPUT)
[12:38:40][C][ultrasonic.sensor:037]:   Pulse time: 20 µs
[12:38:40][C][ultrasonic.sensor:038]:   Timeout: 11661 µs
[12:38:40][C][ultrasonic.sensor:039]:   Update Interval: 5.0s
[12:38:40][C][captive_portal:169]: Captive Portal:
[12:38:40][C][ota:029]: Over-The-Air Updates:
[12:38:40][C][ota:030]:   Address: salt_level.local:8266
[12:38:40][C][api:095]: API Server:
[12:38:40][C][api:096]:   Address: salt_level.local:6053
[12:38:44][D][ultrasonic.sensor:025]: 'Salt Sensor' - Distance measurement timed out!
[12:38:49][D][ultrasonic.sensor:025]: 'Salt Sensor' - Distance measurement timed out!
[12:38:54][D][ultrasonic.sensor:025]: 'Salt Sensor' - Distance measurement timed out!

Can someone tell me what I might need to fix.
Thank you.
carltonb

Having same issue here, getting random distance time out, did you find fix for this?

It fixed itself after a week of running, and a few boots in between.

The default value for timeout is 2m but the sensors I’m using have a maximum of 4m. I’ve noticed that I have to include timeout: 4.0m or I’ll get time outs above any value greater than 2. Looks like you’re still using 2.0m in your config.

5 Likes

@SpikeyGG That is so incredible helpful for me. I was pulling my hair out wondering if I had a bad connection, poor power supply, and even went and ordered replacement ultransonic sensor before reading this. I added a distance timeout and now my sensor is working exactly as I wanted!

Hi,

I have similar issue with the US-100 ultrasonic sensor. the maximum distance I can get is 75cm, but the sensor should cover at least 2.5m

this is my configuration

  • platform: ultrasonic
    trigger_pin: GPIO16
    echo_pin: GPIO32
    unit_of_measurement: cm
    update_interval: 30s
    pulse_time: 10us
    timeout: 4.0m

any ideas?
Regards

1 Like

Hi, how to fix?

Put Timeout variable on more than 2 meters.
timeout (Optional, float): The number of meters for the timeout. Most sensors can only sense up to 2 meters. Defaults to 2 meters.

1 Like

Try also adding inverted: true to the definition of the trigger pin. Some sensors are leading-edge, some are trailing-edge.
Also, as mentioned previously, the timeout distance should be set near the published limit for the sensor device. Too short (or long) can also cause unexpected results.
I struggled for a year one week to get one of these working, and it turned out that the code is fine, but the cohesion with the device is low. I attribute that to poor documentation that came with the device(s) I saw. They don’t explain well enough the semantics of the signals, and worse, describe the timings very badly.

1 Like

Is there somebody who have a solution to this error?
I have also a sensor time out…

The first 2 weeks i have installed the sensor everything works fine, but after a while it gives always the sensor time out, or very strange values.

The sensor i used is a hc-sr04, i also have replaced the sensor for a new one, and it is exactly the same.

Whe i look on the net there are many people with the same probles, but i can never find a solution.

This is my log file

[19:35:40][C][logger:275]: Logger:
[19:35:40][C][logger:276]:   Level: DEBUG
[19:35:40][C][logger:277]:   Log Baud Rate: 115200
[19:35:40][C][logger:278]:   Hardware UART: UART0
[19:35:40][C][ultrasonic.sensor:045]: Ultrasonic Sensor 'IBC percentage gevuld'
[19:35:40][C][ultrasonic.sensor:045]:   State Class: 'measurement'
[19:35:40][C][ultrasonic.sensor:045]:   Unit of Measurement: '%'
[19:35:40][C][ultrasonic.sensor:045]:   Accuracy Decimals: 0
[19:35:40][C][ultrasonic.sensor:045]:   Icon: 'mdi:water-percent'
[19:35:40][C][ultrasonic.sensor:046]:   Echo Pin: GPIO3
[19:35:40][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO1
[19:35:40][C][ultrasonic.sensor:048]:   Pulse time: 20 µs
[19:35:40][C][ultrasonic.sensor:049]:   Timeout: 8746 µs
[19:35:40][C][ultrasonic.sensor:050]:   Update Interval: 2.0s
[19:35:40][C][captive_portal:088]: Captive Portal:
[19:35:40][C][mdns:094]: mDNS:
[19:35:40][C][mdns:095]:   Hostname: niveau-ibc
[19:35:40][C][ota:085]: Over-The-Air Updates:
[19:35:40][C][ota:086]:   Address: niveau-ibc.local:8266
[19:35:40][C][api:138]: API Server:
[19:35:40][C][api:139]:   Address: niveau-ibc.local:6053
[19:35:40][C][api:143]:   Using noise encryption: NO
[19:35:42][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:44][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:46][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:48][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:50][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:52][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:54][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:56][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:35:58][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:36:00][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:36:02][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!
[19:36:04][D][ultrasonic.sensor:036]: 'IBC percentage gevuld' - Distance measurement timed out!

I know it is bit late, but I found that chnaging pulse_time to something like 200ms from default 10us allows me to meassure a distans approx 3,5m :slight_smile:

better later than never :stuck_out_tongue:

1 Like

…just an addition to the discussion, prompted by the post above.

I was having the same timeout issue with a sensor in my garage. After changing the Timeout setting to “10m”, and increasing the "pulse time to 30us, I stopped getting timeouts, and I now have a continuous distance readout.

However I have another issue I have not been able to figure out. The actual measured distance the sensor should be returning is half of the “average” it is returning as measurements – seems the sensor is not “calibrated” properly ? Is there a way to calibrate, or alter the measurements like say dividing by 2 ?

here is a plot of what it looks like, notice, it all started looking good at about 9:40

EDIT: …duh, my bad. The reported distance from the sensor reflects the “over-and-back” distance the sound traveled, so it will be twice the actual distance to the object. However the variability still seems like quite a lot even so. Also found there is no way to “calibrate the sensor itself”. Code could be written within the “device” to do so, but at this point it is beyond my skills.