Errors in hc-sr04 sensor readings

Hi!
I use an ultrasonic distance sensor hc-sr04 to display distance on an i2c lcd monitor, in general the business works fine but at certain distances it displays not so correct numbers. For example in the picture above he brings the number 1198 at a distance of 30 cm from the wall. This lie happens at different distances and in different numbers on the monitor. It is important for me to note that on the serial screen the numbers shown are correct and the forgery only appears on the LCD screen.
This is very detrimental to the whole project ...
The problem with the program (attached) or the problem with the component?

my code:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Wire.h>
#define trigPin 13
#define echoPin 12

void setup()
{
Serial.begin (9600);
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
double duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance >= 30 || distance <= 0)
{
Serial.println("out of range");
lcd.setCursor(0,0);
lcd.print ("out of range");
lcd.setCursor(8,1);
lcd.print(distance);

}
else
{
Serial.print(distance);
Serial.println("cm");
lcd.setCursor(0,0);
lcd.print ("be careful ");
lcd.setCursor(8,1);
lcd.print( distance);

}
delay(500);
}

Several problems: Please read how to post code in a scrolling window.
What are you using to get the reflections and is it directly in front of the sensor or off to one side?
Paul

Sorry I write from Israel and here we write from right to left so it always changes when you turn it into English. I'll try to fix it ... if you know I'll be happy to help.
The error also occurs when I put a large object in front of the two eyepieces together ...

The shape and material make a huge difference.
Paul

? So what do you say there is nothing to do when the eyepieces pass by various objects

'duration' should be 'unsigned long duration;', not 'double duration;'

1196.64 cm is over 11 meters! That is WAY outside the distance capability of the HC-SR04

You should probably set a timeout on the pulseIn(). Try:
duration = pulseIn(echoPin, HIGH, 30000ul);
That will limit the distance measurement to around 5 meters.

The OP may, eventually, find the SR04 is not the brightest tack in the box. For better ranging I used 2 SR04's with one SR04 set back from the other by 4cm. I then adjusted for mounting setback compared the 2 readings and if within 7% then the reading is correct otherwise try again.

Interesting, regarding the first comment at the beginning I did write the variable as "long" but it brings me really weird numbers and precisely as the eyes get closer to the object the displayed number increases .. so I changed to "double" and now it gives very good results but as mentioned sometimes it "lies" For a few seconds in an incomprehensible way ...

: Regarding the second note set

duration = pulseIn(echoPin, HIGH, 30000ul);

.I did not see any change when I defined it that way

Anyway, the idea someone wrote here to do by two components of hc-sr04 is a very likable idea. Is it possible to do the same with one component and compare between 2
? different readings

The HC-SR04 does occasionally return erroneous values. One way to accommodate that is to take a number of readings, say 5 or more, and use the median value. The NewPing library has a median "method" that makes this very simple.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.