HC-SR04 sensor problem

I have a problem. When use this senson, sometimes it returns zero values even thought it shouldnt.
I attach screen with example.
Is that sensor fault or i did something wrong?

And here code:

int t, s;
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);

  t = pulseIn(echo, HIGH);
  s = (t / 2) / 29.1;


I recently also got a HC-SR04 sensor. I can’t guarantee your code is correct due to the small portion of code you gave. However, during some research and playing with it, I too saw that the readings would sometimes go wild. Can’t remember exactly what it is, but he does have a certain range he can “see” and it’s not straight forward.

Do you maybe have an item near him? Is the “thing” you’re drawing near to him made of fabric or some lumpy material? I played with him and fabrics and he wasn’t too happy about it… Using plastic block or similar made him chill out a bit… Try clearing all unnecessary items away from him, or even better, tell him in code to ignore certain values (“noises”) if clearing the environment is not an option …

if (distance >= maxDistanceAllowed || distance <= minDistanceAllowed ){
  } else {

Hope it helped you, let us know how it goes for you…

Read the article and comments, people seem to have the same problem… There also seem to be solutions for it.

digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);

Your trigger sequence is unusual, More standard is

digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

You have done a good job as first time poster in using the code tags. You will get better assistance if you post your complete code, or better yet, a simplified version of the code which still demonstrates the problem.

Your trigger sequence is unusual,

Agreed. I think you're likely to miss the pulse by waiting 100ms.

These cheap sensors work more often than they don't but I've read (and observed personally) about 20% of the sensors are bad (if purchasing the sensor from ebay).

The Maxbotix sensors and Parallax's Ping sensors work consistently well but they cost nearly ten times as much as these cheap sensors. For a while you could find Ping sensors for as low as $5 at RadioShack. If you have a RS nearby you might want to check if they still have any on clearance.