Abishai Singh

Smart Technology Solutions

The Distance Meter Using Sonar

The blog I am writing is just to study the ultrasonic trans-receiver distance measuring module. Here I will be using DYP-ME 007 module, which consists of 5 pins. There are many modules available in the market such as Ping, HC – SR04, DYP-ME007, etc, but I preferred using DYP-ME007 module because it has an additional output pin which can be used for detecting its connectivity in our system. Actually, I am helping one of my cousin Suveer, in making a system. It is basically a water level monitor for water tanks and will consist of 2 different parts connected through a wire with each other. So, it is a requirement to have a feed back to the microcontroller about the presence of module in the system. Here is a picture of the trans-receiver (you can easily find it on internet):

1 HC-SR04

DYP-ME 007

You can find it at aliexpress. Although I think that the overall price for the product is very high (including the shipping charge).

So, first I needed to study the working of the module. According to the information given in the data sheet, I got to know that the module works upon 40 KHz sound waves (after all it is sonar!) It has a pair of speakers mounted on its front, as you can see in the picture the 2 eye like structures coming out from it. One of it is a transmitter and the other one is the receiver. The pin specification is shown below:

Pin Specifications

Pin Specifications

It requires 5v of operating voltage. The trigger pin is to start the transmission of ultrasonic waves from the module. The echo is a 5v pulse. Measuring the time duration of this echo pulse will help us to measure the return time of the pulse (it will be required to calculate the distance of the object). The OUT pin is very interesting. It can be used by our micro-controller to detect the presence of our module. When the module is connected, it gives 5 volt output and by monitoring this pin the module can be traced in the system. And the fifth pin is ground. The use of this module is very simple. First a trigger pulse is sent to the module through the trigger pin and then the time duration of the echo pulse is measured. This time is the time taken by our ultrasonic pulse to travel from the module to the object plus the return time. Thus time taken for sound to reach the object is half the time of the echo pulse. Now by the famous speed time relationship we can calculate the distance from the object.

Speed Distance relationship

Speed Distance relationship

Just to test the module and for fun, I connected it along with Arduino and lcd display on a hanger(also a 9v battery). Have a look at it.

The Distance Gun

The Distance Gun

Back view:

Back View of The Distance Gun

Back View of The Distance Gun

And the front view:

Front of the Gun

Front of the Gun

So, it shaped like a gun calculates its distance from anything in front of it. Well that was just for fun. The main part is the program. There is some weakness in the simple program. First I will quote the code and then the video showing the readings which were obtained. Although I was not going to give the connection diagram because it comes with the sonar’s data sheet, but due to the request of some friends I am posting the connection diagram. Hope beginners will understand in much better way.

HC_SR04+sketch2

(Image from http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html)

The code is simple and it calculates the distance from the module and display it on the lcd monitor. Have a look at it:

// This program will find the distance from the Sensor
// and display’s it on the lcd.
 
#include <LiquidCrystal.h>
 
#define ECHOPIN 7        // Pin to receive echo pulse
#define TRIGPIN 8        // Pin to send trigger pulse
float Max_Distance = 102;
float distance, r, dif;
 
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  lcd.begin(16, 2);
 
  // Start Ranging
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
  // Compute distance
  float distance = pulseIn(ECHOPIN, HIGH);
  distance = distance/58;
 
  Serial.println (distance);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(“Distance is:”);
  lcd.setCursor(0,1);
  lcd.print(distance);
  lcd.print(” cm”);
  delay(500);
 
  r = distance;
  Serial.println(r);
}
 
 
void loop()
{
    // Start Ranging
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
  // Compute distance
  float distance = pulseIn(ECHOPIN, HIGH);
  distance = distance/58;
 
  dif = r – distance;
  Serial.println(dif);
 
  if (dif > 0.3){
    Serial.println (distance);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(“Distance is:”);
  lcd.setCursor(0,1);
  lcd.print(distance);
  lcd.print(” cm”);
  delay(500);
  Serial.print(“from if:”);
  Serial.print(distance);
  r = distance;
  }
  delay(500);
}

And now take a look at the readings obtained from it: https://www.youtube.com/watch?v=zUFM32N9GEE As you can see that the device is kept on a stable surface and it measures the distance from the cupboard in front of it. Even both are stable than also the reading are not same. They are not stable and have some variations. Even if the variations are in few millimeters, variations are variations and have a negative effect on the viewer making them to doubt upon the reliability of the device. Thus I formed a new program which filter out the variations and then tested my device on the same place. It worked fine and now the readings are stable. You can watch it by yourself. http://www.youtube.com/watch?v=U3gP6v8Te8U The ultrasonic distance module is awesome and can be used in many applications. Please do suggest some ideas in the comments below. Hope it would have been useful for you.

Advertisements

13 comments on “The Distance Meter Using Sonar

  1. notrequired
    March 9, 2013

    Please post schematic and wiring diagram so that we can try it

  2. vikas
    March 24, 2013

    we are using dyp-me007 in our smart cane project.smart cane for blind people project deals in detecting obstacles and any depths.

  3. vikas
    March 24, 2013

    please help me in interfacing dyp-me007 with atmega16

  4. Abishai
    March 24, 2013

    most of the information is provided in this blog. What specifically do you require.

  5. Ranzley
    September 7, 2013

    hello sir. is dat possible to make a project that detects obstruction and then the output of the sonar sensor will give a trigger to the microcontroller to output audio?

    • Abishai
      September 7, 2013

      It could be easily made. The sonar will give the distance of any object in front of it in the range of 3 to 5 meters (it depends upon the sensor) and you can program your micro-controller to respond when the object is near to it by playing some buzzer or preinstalled audio…

      • Ranzley
        September 8, 2013

        do you have some idea what micro controller should i use for the audio output? thanks sir. can i used atmega32 microcontroller?

      • Abishai
        September 8, 2013

        I have never tried this before, but as per my knowledge their are some ic’s available in the market which can produce some recorded tunes. If you want to do it with ur uc, than u will have to connect the speaker to the analog pin and give the output in such a varying sequence that it will produce some melody.

  6. Ranzley
    September 9, 2013

    oh, thank you very much sir for your response..

  7. Ali
    September 16, 2013

    how to do the wiring??? the most important part for beginners is missing??

    • Abishai
      September 16, 2013

      Updated the post. Now you can look at the connections…

  8. sumanth gupta
    January 9, 2014

    sir
    can u please help me out in making a ultrasonic distance meter with 8052 mc and dypme007 sensor…please mail me the neccesary circuit diagram and program….it would be very helpfull…thanks in advance…hoping a quick reply

  9. yuvi
    March 20, 2014

    plz tell me how it would be interface to plc….

Leave a Reply...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on March 6, 2013 by in Projects.
Arduining

Physical Computing Mini-Projects

tronixstuff

fun and learning with electronics

Abishai Singh

Smart Technology Solutions

%d bloggers like this: