HC-SR04 ultrasonic Arduino library

Carl Nobile has developed and released an Arduino library covering the HC-SR04 ultrasonic sensor. He writes: “It features timing, metric conversion, and standard deviation methods, plus a few support methods. It is written in C++ to conserve the namespace. The conversion formula can be easily modified to optimize the accuracy of conversion at different distances for inches and centimeters. Multiple buffers of different lengths can be created for determining standard deviation.

Look at the code and examples to understand how the library works. The standerd deviation code can be removed from the compilation resulting in a significant savings of resources. To do this comment the #define COMPILE_STD_DEV definition in the header file.”

You can download this library from cnobile’s Freshmeat webpage.

Join the Conversation


  1. Hello,

    i have this error when i try compile the code from de UltrasonicDemo.pde:

    UltrasonicDemo.cpp: In function ‘void loop()’:
    UltrasonicDemo:26: error: ‘class Ultrasonic’ has no member named ‘timing’
    UltrasonicDemo:28: error: ‘class Ultrasonic’ has no member named ‘convert’
    UltrasonicDemo:28: error: expected unqualified-id before numeric constant
    UltrasonicDemo:29: error: ‘class Ultrasonic’ has no member named ‘convert’
    UltrasonicDemo:29: error: ‘IN’ is not a member of ‘Ultrasonic’

  2. I am working with this HC-SR04 ultrasonic sensor and I got the library and the sketch to work fine. However I need to connect 2 more sensors because I need 3 sensors working at the same time. I am having some difficulty coding the other 2 sensors. My code is not working when I add the other 2 sensors. Can you provide me with some advice?

  3. Hello! Is there anybody who found a way to use multiple of those sensors?? Please inform me, if so. It will be so appreciated regarding my degree project!!! Thank you very much.

    Emin: e10durak[at]gmail[dot]com

  4. I’d imagine that to use multiple sensors, you simply instantiate multiple instances of Ultrasonic with different trigger and echo pin numbers in the constructor.

  5. Hi, I understand you ca use more than one sensor, but the number of places to put the pins is limited in my project because I am using an RJ45 to connect to the board. This would not be an issue if it were the Parallax Pin))) Sensor, since it uses only one pin. My question is, can I convert it to work just like that, so the Trig and Echo are one pin just like the Parallax one? So I could have the one wire going to the RJ45 from multiple sensors, and the trig and echo would be combined before.

  6. The maximum distance these sensors can measure is 500cm. So, both cm and inches will fit in 3 digits. Also, they’re only accurate to about 1cm, so NewPing rounds to the integer. In other words, you only ever get up to 3 digits so a 4 digit display has an extra digit and there’s no reason to worry about rounding.

Leave a comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.