ı have problem about hc-sr04

hi everyone,
ım an electrician in chemical plant,
and yesterday ı made a project.this project mesure a water tank and write 2x16 lcd.
hc-sr04 is working but sometimes show wrong value.
for example water tank is 220 cm(true) but ı look my lcd display and ı see true and folse values

-220(true)500ms later
-220(true)500ms later
-221(true)500ms later
-222(true)500ms later
-90(wrong)500ms later
-220(true)500ms later
-221(true)500ms later
-222(true)500ms later
-110(wrong)500ms later
220(true)500ms later
-221(true)500ms later
-222(true)500ms later
90 and 110 is problem because ı want to control the tank .if tank level arrive high ı will work motor.ı have to work this project becouse ı presented to my boss…it s important for me .how ı can get rid of wrong value?thanks

this is my code;

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int trigger_pin =6 ;
const int echo_pin =7 ;
int sure ;
int mesafe;
int alarm=8;

void setup() {
pinMode( trigger_pin,OUTPUT);
lcd.begin(16, 2);

lcd.print(“dere ust seviye”);

void loop() {
sure = pulseIn(echo_pin,HIGH);

lcd.print(" cm");

if (mesafe<300)

if (mesafe>120)


You can keep a moving average to limit the effect of false readings. Or you can do a simple average of 10 readings at 100 mS intervals rather than taking a reading once every second.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

You could check the variance of the reading. If the difference is greater than could be possibly expected from the water level in the tank changing then discard the reading. I have experienced bad (too small) readings from these sensors when my ground wire connection in the breadboard was not very good.

thank you.ı decided about take 50 different reading than.ı will calculate 50 values avarage and ı will use 100mS delay every calculate.thank you so much.