Ultrasonic HC-SR04 outputs incorrect dist value

Posted on
  • Hi all, I am playing with the HC-SR04 on my esp8266 today and found that the outputted cm value doesn't seem correct at all. it looks like this(see below). a very large value returned. btw, I am completely new to this iot. but I did played with the ultrasonic sensor once with the arduino uno a while back and the distance output seems correct. so, my question is that is it possible that there's a bug in the distance calculation? I highly doubt that. but just want to make sure. If not, what am I doing wrong here? thanks in advance

    I used the code straight from https://www.espruino.com/HC-SR04

    output values:

    35964.50877192983 cm away
    144735.33333333340 cm away
    153507.50877193000 cm away
    162278.77192982469 cm away
    171052.05263157916 cm away
    179822.78947368444 cm away
    188593.07017543850 cm away
    197371.64912280708 cm away
    206141.92982456140 cm away
    214913.57894736839 cm away
    223685.52631578940 cm away
    232456.89473684219 cm away
    241229.26315789471 cm away
    249997.29824561413 cm away
    258771.82456140377 cm away
    267546.50877192983 cm away
    276315.40350877214 cm away
    285087.42105263157 cm away
    293859.31578947376 cm away
    302631.43859649135 cm away
  • What did yòu change A0 and A1 to and how did you wire it up?

    I think the values you are seeing are random values.

  • I have
    A0 map to NodeMCU.D1(Label D1 on board) => Trig on sensor
    A1 map to NodeMCU.D2(Label D2 on board) => Echo On sensor

  • Is the HC-SR04 code even known working on ESP8266? Because of how it has to keep yielding to do the radio stuff, timing-critical things can be problematic....

  • I don't know if the code is working on the esp8266. Can any body share their experience with the esp8266 here?

  • I have same issue on esp32, just increasing numbers ...

    I don't think it's a bug with timing, rather setWatch not triggering on rising signal only on falling. As values are based on timer of falling event numbers are increased all the time.

  • Same here guys. Data seems clean for second when I disconnected and connect echo pin again

  • connect pin Vcc to 5V and try again.

    Hint: Some use a voltage divider for the Echo pin and some don't, check this Google search

    used code

    /* ESP8266 */
    var sensor = require("HC-SR04").connect(NodeMCU.D1,No­deMCU.D2,function(dist) {
            console.log(dist+" cm away");
    /* PICO 
    var sensor = require("HC-SR04").connect(B3,B4,functio­n(dist) {
            console.log(dist+" cm away");
    setInterval(function() {
            sensor.trigger(); // send pulse
    }, 500);

    distance HC-SR04 to

    • room ceiling 168.57866655316 cm
    • hand on top 10.12233265659 cm

  • Search for the RCW-0001 ultrasonic sensor, it works with 3v3 - 5v.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Ultrasonic HC-SR04 outputs incorrect dist value

Posted by Avatar for coyu1314 @coyu1314