Let's Make Robots!

HC-SR04 Ultrasonic Sensor


Vendor's Description: 


The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). Similar in performance to the SRF005 but with the low-price of a Sharp infrared sensor.

 

  • Power Supply :5V DC
  • Quiescent Current : <2mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 500 cm/1" - 16ft
  • Resolution : 0.3 cm

 

Basic use of the HC-SR04 Ultrasonic Sensor 

Please see the datasheet for an excellent description of how the sensor operates. 

HC-SR04 Datasheet

 

Using the HC-SR04 with a PICAXE microcontroller 

There is a nice tutorial right here at LMR with sample code for using an ultrasonic sensor with a PICAXE 28 project board (thanks Frits!). Do not be disappointed that it’s written for the SRF05 ultrasonic sensor; the sensor operates the same way.

“How to connect SRF05 to Picaxe 28 pin Project board” tutorial

 

Using the HC-SR04 with an Arduino 

There is an Arduino library for the HC-SR04 that offers two ways to use the sensor. To install, download the “Ultrasonic Library” from this page, unzip the release package into your “arduino-0018/libraries/” folder. Open the Arduino IDE and include the library by Sketch-Import library-Ultrasonic . There is also an example sketch in File-Examples-Ultrasonic-UltrasonicDemo. 

Ultrasonic Library 

The library includes 3 functions:

1. Ultrasonic(int TP, int EP) 

This is a initial function for ultrasonic ranging module, choose the pins for module TRIG and ECHO pin. For example:

     Ultrasonic(13,12);

defines the digital pin 13 of Arduino as the TRIG pin of HC-SR04 and pin 12 for the ECHO pin. 

2. long Timing() 

This function triggers the ultrasonic module and returns the duration that the ECHO pin was held high. For example:

    long time; Ultrasonic hcsr; time = hcsr.Timing();

The distance of the object corelates to the time the ECHO pin is held high. The distance formula is:

     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 immediately. This function has a parameter (using CM or ICN) that shows the distance in centimeters or inches. This function will call Timing() and you don’t need to use the Timing() before it. For example:

     long distance; Ultrasonic hcsr; distance = hcsr.Ranging(CM); 

returns the distance in centimeters.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is good!

This is really helpful!

It took some doin' but I figured out a bracket to deal with the fact that these sensors A) have tiny mounting holes and B) the holes are wicked close to the transducers.

Here is a video explaination.

Here are some pictures:

 

You have done a beautiful job, but I am pretty sure the PING))) hardware would work fine with this sensor -- the dimensions look the same. That is a real benefit as the PING is so widely used and has some nice accessories.

When you crank out goodies like these I feel I've GOT to have a laser cutter or I'm going to die lol Very sharp looking design. A little jealous hehe

I was considering the lack of space for mounting too. My current plan is to use 0-80 screws with standoffs that measure 1/8" in diameter and 3/8" long. The 0-80s are slightly undersized for the holes (awesome for spec'ing those out too, btw thx) so hopefully the standoffs will have wiggle room if they're too big to clear the SMD bits. Probably will get some 0-80 washers too, 1/8" in diameter, now that I know the holes are 0.083".

Just curious...

Have you guys been able to actually mount these guys to anything? The mounting holes in the corners measure .083 and with a little x-acto knife action, I got them to .09 and able to accept a 2-56 screw. That said, I have found no way in which to run the screw through without the screw and/or nut touching the SMD doo-dads on the back. In fact, if the head of the screw is on the transducer side and the nut is on the back, I am actually jumping 2 resistors via the nut itself.

I think I may have to get a bit creative here with the ol' plexi...

 

I believed also when I got one for less than 10 bucks with shipping :) Mine works fine also!

I was a believer when I got mine for $10 some time ago. The current $4 price tag is preposterous! And I love the good measure. Great links, thanks!

I guess they sorta work! And my Duemilnove board does not!

I did some testing and finally got one of these guys on an O-scope when I found that I was short on juice. It seems that my Arduino board is putting out a little over 4v. I think the volt reg may be going or possibly my USB hub is crapping out in the power department. At any rate, I gave this guy a nice clean 5v from a proper power supply and what do you know? Good, clean results. As good or better (probably better) than a sharp. Yup, I think I can endorse these guys. For 4 bucks, I probably got twice that price in terms of fuctionality. Not a bad little sensor.