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.
This entry was posted in Arduino, code, sensors and tagged HC-SR04, ultrasonic.

Comments
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’
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?
I am having the same issue, is there any way to get more than one sensor connected thanks
Yes, use the NewPing library instead. It even has sample sketches that ping 15 sensors!
NewPing
I also would like to use multiple hc-sr04s with one board.
Any help would be appreciated.
NewPing library will do it. Site also has a sample sketche that pings 15 sensors!
NewPing
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
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.
NewPing the library to ping multiple sensors. There’s also a sample sketch that pings 15 sensors.
NewPing
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.
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?
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.