Arduino Library For Ultrasonic Ranging Module HC-SR04

We have already sold many HC-SR04 modules , though it’s easy use and we has published a demo ,still some user mail us for a specialized code of his own use — even just change the different Arduino pins for module.

So we decide to release a Arduino Library for those beginning user . You just need to add the library into your Arduino , and call for a function , you can get the distance.

Download the Library , unzip the release package to arduino-0018/libraries/ folder. Open the Arduino IDE , you can include the library by Sketch-Import library-Ultrasonic . And you can find the example sketch in File-Examples-Ultrasonic-UltrasonicDemo .

Here we give out a brief introduction of this ultrasonic ranging library.

It includes 3 function for user  :

1. Ultrasonic(int TP, int EP)

This is a initial function for ultrasonic ranging module, choose the pins for module Trig and Echo pin.
example: Ultrasonic(13,12);
then you define the digital pin 13 of Arduino for the Trig pin of HC-SR04. And the pin 12 for Echo pin.

2. long Timing()

Trigger the ultrasonic ranging module work and return the duration that Echo pin keep high level.
example: long time; Ultrasonic hcsr; time = hcsr.Timing();
Then you start the HC-SR04 for ranging and you get the time the Echo pin keep high, you can change the time corresponds to the distance : Distance = ((Duration of high level)*(Sonic :340m/s))/2

3. long Ranging(int sys) — (sys : CM / INC)

If you don’t want to change the time into distance yourself  , this function will help you get the distance immediate. And the function has a parameter sys, you can use CM or ICN , than you get the distance show as centimeter or inch. This function will call the Timing() and you don’t need to use the Timing() before it.
example: long distance; Ultrasonic hcsr; distance = hcsr.Ranging(CM);
OK, now you get the distance in centimeter . It’s very easy , right ?

Download the library for HC-SR04 ultrasonic ranging module here :

  Ultrasonic ranging library (unknown, 24,837 hits)

Responses to “Arduino Library For Ultrasonic Ranging Module HC-SR04

  1. I was womdering if you had that library for the “HC-SR04″ in a different archive format (ex.’.zip’) or in a text format. I purchased a unit and would really love to move forward with this model and if useful possibly purchase more. Thank You

  2. This is great and thank you a lot for sharing.
    Ultrasonic sensors are very useful but take a lot or lines, and this sensor is one of the cheapiest.
    Your library will be very useful. Personally i am trying to use it with a hacked robo sapien:-)
    Best regards and thanks from France !

  3. I keep getting the error:
    ‘class Ultrasonic’ has no member named ‘Ranging’
    UltrasonicDemo.pde: In function ‘void loop()':
    UltrasonicDemo:15: error: ‘class Ultrasonic’ has no member named ‘Ranging’
    UltrasonicDemo:15: error: ‘CM’ was not declared in this scope
    Would you post the Ultrasonic.h file your using?

  4. Hey, I have hc-sr04. The distance read is a bit short meaning it doesn’t seem to read anything past 40 inches. I have the max distance set at 300 cm which should be plenty. I also noticed that within the <40 inches range the sensor doesnt detect objects (a 16"x20" board) consistently.
    I am using an arduino uno r3, adafruit motor shield, PING library, and there is 18" of 22 awg soild wire between the 10 and 9 pins of my shield and hc-sr04 trig, and echo pins. All connections have been soldered. I am measuring from 40" off the floor-in line of the sensor.
    Do I need to shield my wire? Is it too long?
    Do I need to make a cone for my sensor to pick up sound?

    Thank you in advance,

  5. Can anyone help me in converting a program written with NewPing library to one compatible with my atmega8 in ultrasonic.h library? Ithis is my mail id, pls contact me so that i can send u my program.Email:-
    I need some urgent help.

Leave a Reply

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>