There's only 1 raspberry pi shop in my country and he offers only two ultra sonic sensors HC - SR04 and RB URF02 there's a pdf on the pages I linked to for more information. RB URF02 is so much more expensive.

I have tried HC - SR04, and it gives false distances sometimes, precision is important in my project. I have never tried RB URF02 but it looks the same to me, only much more expensive. Money is no problem if the sensor is that good. Is it? Cause I have to buy 4 of them

Have anyone tried RB URF02? Is it reliable? Does it give wrong readings? Is it so much better than the cheaper one?

1 Answer 1


Are you sure the problem is with your HC-SR04 sensor? Most of the posted code I've seen contains errors.

The only oddity I've noticed with the HC-SR04 is that sometimes the echo line stays high when no echo is detected. It really ought to go low after a timeout period (which the specs say it should).

I tried a series of experiments documented at https://www.raspberrypi.org/forums/viewtopic.php?p=563957#p563957

I tend to use code similar to the following to read the sensor.

#!/usr/bin/env python

# HC_SR04.py
# 2015-08-06
# Public Domain

import time

import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

class sensor:

   def __init__(self, pi, trigger, echo):

      self._pi    = pi
      self._trig = trigger
      self._echo = echo

      self._tick = None
      self._distance = 0.0
      self._new = False

      pi.set_mode(self._trig, pigpio.OUTPUT)
      pi.set_mode(self._echo, pigpio.INPUT)

      self._cb = pi.callback(self._echo, pigpio.EITHER_EDGE, self._cbf)

   def _cbf(self, gpio, level, tick):

      if level == 1:
         self._tick = tick
         if self._tick is not None:
            ping = pigpio.tickDiff(self._tick, tick)
            self._distance = ping * 17015.0 / 1000000.0
            self._new = True

   def trigger(self):

      self._tick = None
      self._pi.gpio_trigger(self._trig, 10)

   def get_centimetres(self):

      new = self._new
      self._new = False
      return self._distance, new

   def cancel(self):


if __name__ == "__main__":

   import time
   import pigpio
   import HC_SR04

   pi = pigpio.pi()

   sonar = HC_SR04.sensor(pi, trigger=20, echo=21)

   end = time.time() + 1000.0

   r = 1
   while time.time() < end:

      cms, new = sonar.get_centimetres()
      print("{} {:.1f} {}".format(r, cms, new))
      r += 1


  • I'm not sure but since one sensor is extremely cheap and the other is extremely expensive, I took a guess, since you get what you pay for.
    – Lynob
    Commented Aug 13, 2015 at 14:05
  • thanks for your code, I'll try it tomorrow! really grateful
    – Lynob
    Commented Aug 13, 2015 at 14:05
  • 1
    @Lynob The sensors look pretty much the same to me. The RB URF02 has a range of 5 metres rather than 4 which might explain the difference in price. I don't believe paying more is any guarantee of quality or correctness. I have countless counter examples from my own experience.
    – joan
    Commented Aug 13, 2015 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.