HC-SR04 ultrasonic Arduino library

in Arduino, code, sensors by the machinegeek | 12 comments


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 in Arduino, code, sensors and tagged , .

Comments

  1. Tiago José says:

    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. 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.

    NewPing

  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 Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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