UltraSonic Distance Measurement HC-SR04

4 years ago 1964
  • Description
  • Documents
  • BOM
  • Attachments
  • Members
  • Comments


UltraSonic Distance Measurement

Small low-cost ultrasonic distance measurement modules

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.

UltraSonic Distance Measurement The module in our example has 4 pins: Vcc Operating voltage: 5.0V Trig the transmit signal pin Echo the received echo pin Gnd Ground

UltraSonic Distance Measurement

To test a module with the following Software Sketch, connect 5.0V and Ground to your Arduino, and Trig to Arduino pin 5 and Echo to Arduino pin 4. 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. 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.

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

#include "NewPing.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  5
#define  ECHO_PIN     4
#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.println("UltraSonic Distance Measurement");
  Serial.println("easyeda.com  easyeda.com");
}//--(end setup )---

void loop()   
  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.println(" cm");  


After downloading this to your Arduino, open the Serial Monitor and you should see output like this:


NewPing Library:http://playground.arduino.cc/Code/NewPing#Download




ID Name Designator Footprint Quantity
2 New Schematic Lib U1,U2 DIP 2
3 Arduino MEGA 2560 U3 DIP 1




The owner does not allow comments in this project now

Comments (0)

你现在访问的是EasyEDA海外版,使用建立访问速度更快的国内版 https://lceda.cn(需要重新注册)
如果需要转移工程请在个人中心 - 工程 - 工程高级设置 - 下载工程,下载后在https://lceda.cn/editor 打开保存即可。
有问题联系QQ 3001956291 不再提醒
svg-battery svg-battery-wifi svg-books svg-more svg-paste svg-pencil svg-plant svg-ruler svg-share svg-user svg-logo-cn svg-double-arrow
We use cookies to offer you a better experience. Detailed information on the use of cookies on this website is provided in our Privacy Policy. By using this site, you consent to the use of our cookies.