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:

    1. 35964.50877192983 cm away
    2. 144735.33333333340 cm away
    3. 153507.50877193000 cm away
    4. 162278.77192982469 cm away
    5. 171052.05263157916 cm away
    6. 179822.78947368444 cm away
    7. 188593.07017543850 cm away
    8. 197371.64912280708 cm away
    9. 206141.92982456140 cm away
    10. 214913.57894736839 cm away
    11. 223685.52631578940 cm away
    12. 232456.89473684219 cm away
    13. 241229.26315789471 cm away
    14. 249997.29824561413 cm away
    15. 258771.82456140377 cm away
    16. 267546.50877192983 cm away
    17. 276315.40350877214 cm away
    18. 285087.42105263157 cm away
    19. 293859.31578947376 cm away
    20. 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

    1. /* ESP8266 */
    2. var sensor = require("HC-SR04").connect(NodeMCU.D1,NodeMCU.D2,function(dist) {
    3. console.log(dist+" cm away");
    4. });
    5. /* PICO
    6. var sensor = require("HC-SR04").connect(B3,B4,function(dist) {
    7. console.log(dist+" cm away");
    8. });
    9. */
    10. setInterval(function() {
    11. sensor.trigger(); // send pulse
    12. }, 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
    • $ Donate
About

Ultrasonic HC-SR04 outputs incorrect dist value

Posted by Avatar for coyu1314 @coyu1314

Actions