Loading...

You have no items in your shopping cart.

 
 
 
×

Registration

Profile Informations

Login Datas

or login

First name is required!
Last name is required!
First name is not valid!
Last name is not valid!
This is not an email address!
Email address is required!
This email is already registered!
Password is required!
Enter a valid password!
Please enter 6 or more characters!
Please enter 16 or less characters!
Passwords are not same!
Terms and Conditions are required!
Email or Password is wrong!
Captcha is required!
Captcha is wrong!

Arduino Library For Ultrasonic Ranging Module HC-SR04

We have already sold many HC-SR04 modules and though it is easy use and we have published a demo, still some users mail us for a specialized code of their own use – even just changing the different Arduino pins for module.

So we decided to release an Arduino Library for those beginning users.  Users just need to add the library into their Arduino, and call for a function to get the distance.

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

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

It includes 3 functions, explained below :

1. Ultrasonic(int TP, int EP)

This is an initial function for the ultrasonic ranging module. Choose the pins for module Trig and Echo pin.
Example: Ultrasonic (13,12);
Then users need to 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 to work and return the duration for which the Echo pin stays on high level.
Example: long time; Ultrasonic hcsr; time = hcsr.Timing();
Then users start the HC-SR04 for ranging and they 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 users don’t want to change the time into distance yourself, this function will help you get the distance immediately. The function has a parameter sys, users can use CM or ICN , then they get the distance measured in centimeters or inches. This function will call the Timing()  so users don’t need to use the Timing() before it.
Example: long distance; Ultrasonic hcsr; distance = hcsr.Ranging(CM);
Now you get the distance in centimeters. It’s very easy , right ?

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

  Ultrasonic ranging library (unknown, 41,694 hits)