UltraSonic HC-SR04 distance sensor shows wrong values?

Hello,

I’m new to ESPHome and I try to measure the distance with the UltraSonic HC-SR4 sensor.
But I get only to wrong values in the ESPHome logs:

[12:04:05][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.01 m
[12:04:05][D][sensor:092]: 'Abstandssensor': Sending state 0.00549 m with 2 decimals of accuracy
[12:04:07][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.00 m
[12:04:07][D][sensor:092]: 'Abstandssensor': Sending state 0.00497 m with 2 decimals of accuracy
[12:04:09][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.00 m
[12:04:09][D][sensor:092]: 'Abstandssensor': Sending state 0.00497 m with 2 decimals of accuracy
[12:04:11][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.01 m
[12:04:11][D][sensor:092]: 'Abstandssensor': Sending state 0.00583 m with 2 decimals of accuracy
[12:04:13][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.00 m
[12:04:13][D][sensor:092]: 'Abstandssensor': Sending state 0.00497 m with 2 decimals of accuracy
[12:04:15][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.00 m
[12:04:15][D][sensor:092]: 'Abstandssensor': Sending state 0.00497 m with 2 decimals of accuracy
[12:04:17][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.01 m
[12:04:17][D][sensor:092]: 'Abstandssensor': Sending state 0.00514 m with 2 decimals of accuracy
[12:04:19][D][ultrasonic.sensor:030]: 'Abstandssensor' - Got distance: 0.01 m

And the values are mostly the same also if I change the distance to the target.

Here is my ESPHome configuration:

esphome:
  name: brunnen
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxx"
  password: "xxx"

  ap:
    ssid: "xxx"
    password: "xxx"

captive_portal:
logger:
api:
ota:

sensor:
  - platform: ultrasonic
    trigger_pin: D8
    echo_pin: D7
    name: "Abstandssensor"
    update_interval: 2000ms

And here you can see my wiring:

I connected:

  • G -> GND
  • 3V -> Vcc
  • Echo -> D7
  • Trigger -> D8

Am I missing something?

Thank you for your help.

I’m on HA 0.116.3

Typically the HC-SR04 requires 5V. Some sellers have modules under that name that support both, 5V and 3.3V. And there is version “HC-SR04-P” that is typically the one that does support 5V and 3.3V.

If your module turns out to support 5V only, just beware that the echo pin should not be connected directly to an ESP pin, but through a voltage divider to ensure that the ESP pin does not receive more than 3.6V.

2 Likes

Have tried flashing tasmota for the same sensor may be an easier route and is supported

One more quick trouble-shooting step you could take is change the pins. According to this handy reference D8 can be problematic. D5/D6/D7 should be safe bets.

Another tip for future readers:

To make a voltage divider to step down 5v to 3.3v, you need one resistor to be 66% of the total resistance.

You can conveniently make this out of 3 of any value (X) of resistor:

R1 and R2 have a resistance of X / 2 (1/ [(1/x) + (1/x)]). Call that R4.

The voltage divider’s Vout is :

  • = R1 / (R1 + R4) * 5 V
  • = x / (X + x/2) * 5 V
  • = x / (1.5 x) * 5 V
  • = 1 / 1.5 * 5 V
  • = 2/3 * 5 V
  • = 3.3 V
1 Like