HC-SR04 ultrasonic Arduino library

Posted on Monday, October 17th, 2011 in Arduino, code, sensors by the machinegeek

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.

This entry was posted on Monday, October 17th, 2011 at 4:44 pm and is filed under Arduino, code, sensors. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

12 Responses to “HC-SR04 ultrasonic Arduino library”

  1. Tiago José says:


    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. Maria Elisa Mandarim says:

    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. Elac says:

    I also would like to use multiple hc-sr04s with one board.
    Any help would be appreciated.

  4. Emin Durak says:

    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

  5. Jim Finnis says:

    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.

  6. Tim Eckel says:

    NewPing the library to ping multiple sensors. There’s also a sample sketch that pings 15 sensors.


  7. Nader says:

    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.

  8. John Varga says:

    I’m reading the distances on a LCD but only have room for a maximum of 4 digits for CM and 4 digits for IN. How would I round off these entries?

  9. Tim Eckel says:

    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 Reply

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

Recent Comments

  • Joe Desbonnet: Ya, I can recommend the low melting point solder. I used brand 'ChipQuik' and it's amazingly easy to use.
  • Jerome: I need a new BusPirate for the Fablab ;) Many thanks!
  • Max: Seems like an unexpectedly violent way to remove the chip indeed. A hot air station should of course do the job just fine, but in...
  • jose: Part removal described here is pure butchery, the cheapest hot air station will do a fast and clean job removing the QFP, heat air to...
  • Cody: Yes please