I have a HC-SR04 connected to my Arduino. its all working very nice. Yet with a constant interval a weird value appears (see the Serial Plot picture). I am wondering what causes this. I will soon upload my Sketch (Making it bit more readable).

Or are there to many possible factors that could influence the HC-SR04 behaviour that it is impossible to determine the cause?

Y-axis shows distance in cm, x-axis iteration (once a second)

Y-axis shows distance in cm, x-axis shows iteration (once a second)

Thanks in advance!

Kind Regards, a fellower Arduino User

  • 1
    It's probably less likely the HC-SR04 and more likely Arduino interrupts and micros() jitter. – Majenko May 16 '18 at 9:43
  • Yes I am using millis() ! To make my distance sensor not lockup the remaining of the sketch – Anton van der Wel May 16 '18 at 9:49
  • I am thinking more with the pulseIn() function that is commonly used for reading the HC-SR04. That uses micros() for the timing, and if that gets interrupted (by the timer0 millis() interrupt, or a serial interrupt, for instance) it could skew the results. – Majenko May 16 '18 at 9:54
  • Sounds logic, aswell as I do use PulseIn and serial connection. Is there a way I can Predict when it occurs? – Anton van der Wel May 16 '18 at 17:15
  • 1
    For absolute precision, you should be using one of the timer Input Compare peripherals, though I don't know of an Arduino library offhand that does it. – Majenko May 16 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.