HC-SR04 Ultrasonic Sensor not returning values

Hello Home Assistant Community,
@glyndon @LeFab @Troon @aidbish

I’ve been encountering a persistent problem with an HC-SR04 Ultrasonic sensor connected to a D1 Mini ESP8266. Despite various attempts to troubleshoot, the sensor fails to return valid distance measurements and often times out, leading to nan values in ESPHome logs. I would appreciate any insights or suggestions you might have. Here’s a detailed account of the issue and the steps I’ve taken so far:

  1. ESPHome Configuration: I’ve double-checked my ESPHome YAML configuration. The trigger pin is set to D1, and the echo pin to D2,
    The configuration is really simple and there is much that can go wrong.
    Additionally, I’ve used other working sensors’ configurations on the new setup.

Here it is…

esphome:
  name: watersoftner-v2
  friendly_name: Watersoftner_v2

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ryvrdiPPlLlVYBdWu2K/p1/NsaEzz1WSpG/+Ph2lv40="

ota:
  password: "23d31109151006eed9e58fa03c511191"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Watersoftner-V2 Fallback Hotspot"
    password: "gHvieRf2c0e6"

captive_portal:

# Distance sensor
sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Distance_v2"
    update_interval: 20s

# End ------------------------------------------------------------
  1. Power Supply to D1 Mini: I checked the power supply and change the power source a few times.
  2. Breadboard Setup: I changed the breadboard and the wires a few times to make sure I did not have a faulty breadboard or wire.
  3. Hardware Verification: I’ve tested with multiple HC-SR04 sensors to rule out a defective sensor.
    Using the D1 and D2 equivalent to GPIO5 and GPIO4 respectively.

image

  1. Sensor Testing: Environmental factors and physical obstructions have been considered and ruled out. I’ve also conducted independent tests of the HC-SR04 sensors with a simple Arduino sketch, confirming their functionality outside the ESPHome environment.
  2. Community and Documentation: I’ve looked through the HC-SR04 documentation and searched the ESPHome forums for similar issues but haven’t found a resolution that fits my case.

I have two other sensors working correctly with similar configurations. The codes from the working sensors were directly copied to the new hardware setup, but the problem persists with the new HC-SR04 sensors (I have tried 5 different ones).

I’m reaching out to see if anyone has experienced something similar or can offer guidance on what else I might try to resolve this. Any help would be greatly appreciated.

1 Like

update_interval: 20s
Is to long for a echo period

Try to set
update_interval: 500ms
Or remove line

My config as attached pic

Thank you for the input. I change to check if anything … but nothing.
image

This is crazy !!! This is suppose to be so simple.
However I can NOT make these sensors to give me a reading. Like I said I have 2 sensors working for almost 8 months now. Now I an trying to create a few more and I can not !!! CRAZY !!!

This setup works form me:

  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    id: salt_level_percent
    name: "Salt Level Percent"
    update_interval: 60min
    timeout: 2m

I would double check connection (sometimes such cables and prototype boards don’t provide good connection) and try different pins. update_interval has nothing to do with sensor response time, it is just how frequently you’ll be taking the measurement.
Additionally - what is the distance you are measuring? Check the timeout setting - this is related to the max distance the sensor can measure. Check the documentation for parameters: HC-SR04

Hello @adorobis thank you for trying to help.
I did copied your setup, and still having the same issue.
Instead of using 60 min, I used 1 min interval so I can see the results faster.
I had 2 sensors that are working and used the same similar code.

I tried my functional codes in 5 different brand new sensors with new wires and different boards. This is insane and I am so frustrated. Not sure what is doing this.

Thanks anyway.


Although it worked for me before for one sensor, I had to change the value I used for the trigger_pin and the echo_pin for another one to the GPIO# - might be worth a try:

sensor:
 - platform: ultrasonic
   trigger_pin: GPIO13
   echo_pin: GPIO12
   name: 'Distance Meter'
   update_interval: 30s
   pulse_time: 25us

I assume you get the respective log entries for your setup, correct?

[19:12:09][C][ultrasonic.sensor:046]:   Echo Pin: GPIO12
[19:12:09][C][ultrasonic.sensor:047]:   Trigger Pin: GPIO13

Hello @chairstacker , I did try again to change some of the General I/O and no change. It is so frustrating because this is an easy configuration and I have done it before and currently I have 2 ultrasonic sensors that are working fine.
Now I need to make a few more for other projects and I can not get a single set up to work.

image

As far as the PINS, it does correspond to what I had in the code
I changed D6 (GPIO12) to trigger and D7 (GPIO13) for Echo.

image

I feel your pain. I assume you have tried other pins to rule out a hardware defect there as well.

I have tried, not just other pins, but I have tried 4 different D1 Mini ESP8266 and also 4 different HC-SR04 to make sure that I am not missing something basic.

Like I said before I have 2 functional systems working perfect. I just can not get the next batch to work.

Were they bought all together? If there was a problem in the manufacturing line, an entire lot could be bad.

Have you considered swapping one of the working HC-SR04’s to one of
the new D1’s?

I will try further different PINS…Thank you for trying to help