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:
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.
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.
Have a tinker with pulse_time and timeout.
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?
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”.
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.
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.
…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…
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…
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.
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.