Cannot get accurate readings from HC-SR04

I’m really confused. I’m having problems getting a reliable reading from my HC-SR04 ultrasonic sensors.

I currently have 2 mounted in my garage pointing down toward the middle of each parking space.

They read a constant 1.08 ft and 1.16 ft respectively, whether a vehicle is parked in that space or not.

I’ve tried using Tasmota on a D1 mini; ESPHome on a D1 Mini, D1 Mini S2, and an ESP32.

I tried using an Arduino and a simple sketch and got accurate readings while pointing the sensor in different directions.

So at this point, I’m stumped. None of the boards I can add to ESPHome & Home Assistant produce accurate readings and I don’t know how to have my Arduino board report to HA.

Suggestions are welcome.

  - platform: ultrasonic
    trigger_pin: 1
    echo_pin: 2
    name: "Cindy's Garage Space"
    update_interval: 30s
    unit_of_measurement: ft
      - filter_out: nan

  - platform: ultrasonic
    trigger_pin: 4
    echo_pin: 6
    name: "Glen's Garage Space"
    update_interval: 30s
    unit_of_measurement: ft
      - filter_out: nan

This is the code I’m currently using but as I said the distances are not reflecting correct values.

The distance from the ceiling to the garage floor is probably about 10 feet, maybe a bit less.

I’m powering the Ultrasonic sensors with 5v from the S2 but the sensors report the same values even if I power them independently.

Thanks in advance.

The two sensors could be interfering with each other. They both work on the same frequency. Try just one and see if that makes a difference.

here’s the sensor config I use for mine using an ESP8266:

  - platform: ultrasonic
    trigger_pin: D4
    echo_pin: D3
    timeout: 4m
    update_interval: 2s
    accuracy_decimals: 3
    name: Distance

try using the “Dx” syntax instead of just the x number.

also if you just can’t get it to work using ESPHome then the way to get the data from Arduino into HA is to use MQTT.

I had only one sensor connected and there was no difference in the readings.

These are installed in a standard 2 car garage.

I saw one example where someone used 2 sensors per parking space for a total of 4 in the same garage. This project appeared to work successfully using a D1 mini.

This is another reason for my confusion. There are many stories of success as well as those like mine where readings are inaccurate.

I’m using a D1 mini S2 here. The pins are not referenced by “Dx” so I don’t think I can change this.

If I were to use MQTT I would need a WiFi shield for the Arduino correct?

I have zero experience with the D1 Mini. But yes you would need some way for it to communicate to an MQTT broker.

I would use the ESP32 that already should have the wifi portion available. At least I think it should. All of my ESP32’s I’ve used have wifi built-in. Then I think you can also use the Dx pin references.

and if not then you should still be able to use the arduino sketch on the ESP32 and use its wifi.

before I switched to ESPHome all of my sensor projects used an ESP based device (ESP32 or ESP8266 NodeMCU boards - mostly ESP8266 NodeMCU’s) that I then used MQTT for communication.

I’m pretty sure I still have an Arduino sketch that I used for my US sensor exactly the same way you are using it for a garage parking distance sensor if you are interested in it.

But that brings up another question…

if you haven’t been using wifi already then how are you getting the ESPHome data into HA to test it? Does the D1 mini use ethernet or am I missing something else?

D1 Mini, S2, and ESP32 all have WiFi. That’s how they are being used in ESPHome.

Out of all of the devices I’ve tried, the Audrino seems to be the only one producing acceptable readings.

I can’t understand why this is happening. All other factors are the same. I’m not moving the sensors.
I either power them with the 5v pin of the device or with an independent power supply. The readings produced are the same.

Using the Arudino seems to be the only solution but then I need to get the output to HA.

OK so after thinking about the conversation I now realize that the D1 variants and ESP32 aren’t giving correct readings using Tasmota or ESPHome but then you tried some form of Arduino board using an arduino sketch and that worked (and I assume you got readings via the serial comms).

I originally thought you were saying you tried an arduino sketch on the boards above and that worked but ESPHome didn’t.

OK so now I think I’m on the same page.

Have you tried the working arduino sketch on the ESP32 to see if it works there?

But if you have a working Ardiuno board and can’t get the others to work then yes you would need a wifi shield for the Arduino board. Then you can use MQTT to get the data to HA.

I just looked at the pinout for the D1 mini and google shows the pins as referenced by “Dx”. Have you actually tried to change it? Do you have the boards specified correctly in the ESPHome config?

I’m not sure why they wouldn’t work correctly either since I have a few US distance sensors all working with ESP8266 boards and ESPHome.

The board I’m currently using is an S2 Mini.

I suppose I could try substituting Dx instead of pin number to see if that would make a difference.

The beginning of my config file:

  name: "garage-monitor"
  friendly_name: Garage Monitor

  board: esp32-s2-saola-1
    type: arduino
    version: 2.0.6

Just to be clear, when I tested this ultrasonic sensor with an Audrino I only had the sensor and the Audrino connected to each other. I had a simple sketch to read the sensor and I was watching the values with the serial monitor.

One interesting thing I discovered today was how the distance is reported is odd.

My config file:

unit_of_measurement: cm

The dashboard begins reading:
Glen’s Garage Space 1.07 cm

If I change the config:

unit_of_measurement: ft

The dashboard begins reading:
Glen’s Garage Space 1.07 ft

None of this makes sense to me.