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.

A better, faster, more powerful ultrasonic is availble here:

http://code.google.com/p/arduino-new-ping/

Thanks - I missed this somehow.  I'll try it out and let you know how it worked for me.

Curious to know how much range you guys are getting with the HC-SR04.  My max has been around 45 inches, or 114cm.  The published range on eBay is 450cm.  I know they exaggerate, but that's a lot.  Anybody getting that much distance from these?  I will say that from 1-45 inches these seem to be very accurate.  

I get 513cm as the max, and about 2cm min. Pretty accurate results too right upto 2 meters or so.

For me, the range was about 130-150 inches.

I used the library from http://code.google.com/p/arduino-new-ping/ and sensor from http://www.digibay.in/hc-sr04-ultrasonic-proximity-sensor

The sensors I've tinkered with can see the walls of my room which are between 300-350cm away.

Hi !

Can you change the library from the post with this one: Ultrasonic for Arduino IDE 1.X as this one is edited to make it work with Arduino 1.X IDE ?

 

Tnx, E

But, you really shouldn't be using the Ultrasonic library at all (any version) as it's defective.  It's just a basic library that "works".  But beyond that, it's not really good for a real-world project.  As it uses pulseIn, it can lag up to 1 second if it doesn't get a ping echo, long enough to send your robot off-course or make your project generally unresponsive.  Instead, you should use the NewPing library, which is much more advanced, faster, smaller code, more accurate, works with multiple sensors, etc. On, and it can interface with ultrasonic sensors using only one Arduino pin! So, you can use a lowly Arduino Uno, Leonardo, Nano, Pro Mini, Teensy 2.0, etc. instead of a much larger and more expensive ATmega 2560 for projects with multiple motors/sensors.

ok, so now that I have shown my frustration regarding this sensor and having left with 2 bad ones. I wanted to know if I could still use the transducers that are in the dead sensors? How can I check if they are even working? Can I desolder and use them for something else? If so, how and for what?

I had bought 2 of these from 2 seperate ebay sellers in HK. One was DOA so the seller sent me another one. That worked for about a day and died. Second one never worked either. No response from the seller. I kicked myself and bought the real Parallax PING sensor and it still works after number a robot switching and testing!

Spend the extra money, you will thank yourself in the end. Both the HC-SR04 that I received has the chip number scratched/sanded off the back. I am glad people are having good experiences with them, but I dont know if I should take any chances, but the $3.82 + $1 shipping does sounds tempting! ebay link