UltraSonic Distance Measurement


SRF06-450.jpg
Size about 1.75x.75 in, 4.5x2.0 cm
Small low-cost ultrasonic distance measurement modules like this: SRF-06 are an effective way to sense the presence of nearby objects and the distance to them. Often Robots use these to sense objects or collisions and take appropriate action.

Here's how these modules work:

They have two transducers, basically a speaker and a microphone.

Ultrasound is a high frequency sound (typically 40 KHz is used). A short burst of sound waves (often only 8 cycles) is sent out the "Transmit" transducer (left, above). Then the "Receive" transducer listens for an echo. Thus, the principle of ultrasonic distance measurement is the same as with Radio-based radar.

Distance is calculated as: L = C × T/2 , where L is the length, C is the speed of sound in air, T is the time difference from the transmission from the transmitter to the receiver. This is divided by 2 for the two-directions the sound travels. Speed of sound is about: C = 344m / s (20 degrees C room temperature).

Speed of sound in air velocity is affected by the air density, and for high accuracy the temperature must be taken into account, either within the module electronics (In the SRF-06 module we have) or in the Arduino software.

We have a page HERE with much more information about Ultrasonics and Echo Location.

Ultrasonic-2-512.jpgThe module in our example has 4 pins:
  1. Vcc Operating voltage: 5.0V
  2. Trig the transmit signal pin
  3. Echo the received echo pin
  4. Gnd Ground

Accuracy??

It's hard to get good information from the manufacturers. Here are numbers derived from some extensive tests done by "RiGonz" on the Arduino Forum:
As per my measurements the facts regarding the HC-SR04 are:
- FOV (full cone): horizontal ~20º, vertical ~13º
- Spatial resolution (full cone): ~0.6-1.4º
- Range: tested from 5 to 200 cm
- Accuracy: relative error ~0/5% (-1.3 ±4.6 %); absolute error ~-0.5/-1.5 cm (-0.4 ±1.2 cm).
- Precision: standard deviation ~0.1/0.5 cm (0.3 ±0.6 cm)

Note: The US-100 module has 5 pins but two are ground and 1 to 4 are the same.

To test a module with the following Software Sketch, connect 5.0V and Ground to your Arduino, and Trig to Arduino pin 11 and Echo to Arduino pin 10. This photo shows the easy way to do this using a YourDuinoRobo1 and part of a flat cable jumper:

Software will do the following:
  • Turn the Trig pin on and off to send out a sound pulse
  • Monitor and time how long until the Echo pin sees the echo
  • Calculate the distance as shown above, possibly correcting for temperature

Following is an example Arduino Software Sketch to show how this works. It uses a library called NewPing. See it / get it HERE: This library can give you the Time, or the distance in cm or Inches. You will need to copy this to your computer, UnZip the file and copy the folder and its contents to the "libraries" folder on your computer. This is typically like:
C:\Users\(yourname)\Documents\Arduino\libraries. If you're already running the Arduino development software, you will have to restart it in order for it to see the new library.

NOTE: At the bottom of this page is another example showing how to make decisions based on distance.

Detailed Information on Tim Eckel's NewPing Library and Timers etc. (Click)

Here is the Software Sketch (New Version 2.0 January 2013) you can cut and paste into a New window on your Arduino IDE:

/* YourDuino SKETCH UltraSonic Serial 2.0
 Runs HC-04 and SRF-06 and other Ultrasonic Modules
 Open Serial Monitor to see results
 Reference: http://playground.arduino.cc/Code/NewPing
 Questions?  terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  11
#define  ECHO_PIN     10
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result 
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
  
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(DistanceCm); 
  Serial.println(" cm");  

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

// None
//*********( THE END )***********



After downloading this to your Arduino, open the Serial Monitor and you should see output like this:
UltraSonic Distance Measurement
YourDuino.com  terry@yourduino.com
64cm   25inches
64cm   24inches
63cm   24inches
64cm   25inches
64cm   25inches
64cm   24inches
64cm   25inches
63cm   24inches
64cm   25inches
64cm   25inches
64cm   25inches
64cm   24inches
 
 
Move some flat object like a book in front of the sensor and move it in and out to see the changing distance.

Different objects will reflect a different amount of the Ultrasonic wave, so results will vary a lot. Here are a few examples:

1 ball-point pen, 200mm
2 Hand, 400mm
3.1mm thick plastic sleeve with a wire, 30mm
4. Vernier caliper, 450mm
5. The body (wearing thick clothes), 400mm
6. Wall, 1200mm
7. 1mm thickness soft cotton: undetectable
8. bamboo toothpick, 40mm
9. Stationary Cat ???

Questions or comments welcome
Regards, Terry King
terry@yourduino.com

Example code showing how to make decisions on distance:

/* YourDuino SKETCH UltraSonic Serial 2.0
*** Modified to find distance < 3 inches (7cm)
*** Frances Mae Gerpacio
 Runs HC-04 and SRF-06 and other Ultrasonic Modules
 Open Serial Monitor to see results
 Reference: http://playground.arduino.cc/Code/NewPing
 Questions?  terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  11
#define  ECHO_PIN     10
#define MAX_DISTANCE  14 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result 
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
  
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(DistanceCm); 
  Serial.println(" cm");  
  if ( (DistanceCm <= 7) && (DistanceCm != 0) )
  {
  Serial.println("3 Inches or closer! ");
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

// None
//*********( THE END )***********