Trouble with reading HC-S04 ultrasonic sensor

Hello! I've just started learning the arduino and I am confuse with the results of pulseIn(). From what I understood, the further the object goes the longer it will take for the sound wave to return. The problem is the result I get is inconsistent. While I drag the object away from the sensor, it gives me reasonable results followed by shorter results. So for example the first duration is 200 and if I drag the object further it will give me 191. If I drag it again it will go up again to 200-ish and go to 300-ish and if I drag it further it will go back to 200-ish and so on. I've tried to find the answer in these forums and other blogs but ended up empty handed. Is this normal or there is something wrong with my code or sensor?

I've attached the code and schematic.

BareMinimum.ino (356 Bytes)

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