Ultrasonic sensor hc-sr05 fails measuring distances over 200cm

Hello,

I,m currently working with the hc-sr05 ultrasonic sensor and a Wemos D1 mini board to measure distaces between 10cm and 200cm. The problem I’m dealing with, is that when there is an object 200cm of further from the sensor and not moving the sensor starts reading random values between 80cm ad 180cm. Only when the object is at 180 cm or closer, the sensor works as expected.

Code:

#include <NewPingESP8266.h>
#include <Adafruit_NeoPixel.h>
#define NUMPIXELS 1

const int PIN =13;

float duracion =0;
int iterations = 1;
float distance;

#define TRIGGER_PIN 14
#define ECHO_PIN 16
#define MAX_DISTANCE 400

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

NewPingESP8266 sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
// pinMode(ledPin, OUTPUT);
Serial.begin (9600);
pixels.begin();

pixels.setPixelColor(0, pixels.Color(255,0,0));
pixels.show();
delay(500);

pixels.fill((0,0,0));
pixels.show();
delay(500);

pixels.setPixelColor(0, pixels.Color(255,0,0));
pixels.show();
delay(500);

pixels.fill((0,0,0));
pixels.show();
}

void loop() {

duracion = sonar.ping_median(iterations);

distance = (duracion/2)*0.0343;

if (distance >= 180 || distance <= 10) {
Serial.println(“Out of range”);
pixels.fill((0,0,0));
pixels.show();
delay(10);
}
else {
Serial.print(distance);
Serial.println(" cm");
pixels.setPixelColor(0, pixels.Color(0,255,0));
pixels.show();
delay(10);
}

}

Thank you,
Camilo

In the attachment you can see the random values I got when the wall was 300 cm from the sensor

Serial.jpg

Maybe you are receiving echoes from a previous ping. Try changing your 10 millisecond delays to 30 milliseconds.

Thank you for your reply, I changed the delays in my if statements but the result is the same. I'm wondering if there is a way to modify that from inside the New Ping library but since my programming skills are not that sharp I'm having trouble with it.

Upon making new measurements I can say that the problem only occurs when pointing the sensor to open spaces (example; the sky, a far away wall) maybe is there hardware instead of the programming unless someone with the same problem know about a way around with a programming solution.