Go Down

Topic: Ultrasonic range sensor -- what can I really expect? (Read 4699 times) previous topic - next topic

smorgana

I picked up an ultrasonic range sensor from Radio Shack... It looks like the same sensor is sold under other names, but basically it has 3 pins -- power, ground and signal...

You send a 5 microsecond pulse onto the signal line and then measure the pulse length that the sensor sends back -- the length of the return pulse is the time it took for the ultrasonic chirp to echo back..

Nice.     and for distances up to about 17 inches, it does a good job -- readings are a little off, but repeated readings are stable...

However, if the distance to the closest object is beyond that...  it just continually returns pulses indicating 280 inches...
Problem is, I'm pointing it at a wall 6 feet away.

Radio shack rates the part to work up to 13 feet on an Arduino.

I've tested it on both an Arduino Yun and an Arduino Mega -- get the same behaviour for both.

I'm trying to get an idea what I can expect from this part.... and trying to come up with explanations for why it can't detect a wall 6 feet away..

In my application -- this may not matter.... right now I just want to make a simple obstacle avoiding robot... 17 inches is probably plenty of advanced warning of an obstacle

But I also would like to use this part, or one like it, in a home automation setting, where it will matter if I can sense objects 6 feet away.

Any advice?

Hackscribble

Hi smorgana

Can you post your code and a link to the sensor, just so we can be sure we are talking about the same thing.

You could try the NewPing library.  I have this working to detect a person's ankles out to about 120cm.

http://playground.arduino.cc/Code/NewPing

Regards

Ray
Hackscribble.  Writing about making things
hackscribble@outlook.com

AWOL

Note also that the environment these devices operate in can affect readings a lot.
Not unsurprisingly, they're not great at detecting soft objects, because they absorb sound.
They're also not good at detecting very hard objects that are not normal to the incident pulse.
Also, too high an update rate can cause false readings, as the echoes from previous pulses are interpreted as returns from the most recent pulse.

smorgana

Thanks for both your responses.
It's a Radio Shack part -- they supply a C++ class to trigger it and measure time to echo return.
Here is the code they provided -- it looks a lot like code I've seen for other sensors that use a single pin for signaling.
/***************************************************************************/   
//   Function: Measure the distance to obstacles in front and print the distance
//           value to the serial terminal.The measured distance is from
//           the range 0 to 400cm(157 inches).
//   Hardware: Ultrasonic Range sensor
//   Arduino IDE: Arduino-1.0
//   Author:    LG      
//   Date:     Jan 17,2013
//   Version: v1.0 modified by FrankieChu
//   by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/*****************************************************************************/
#include "Arduino.h"
class Ultrasonic
{
   public:
      Ultrasonic(int pin);
        void DistanceMeasure(void);
      long microsecondsToCentimeters(void);
      long microsecondsToInches(void);
   private:
      int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
        long duration;// the Pulse time received;
};
Ultrasonic::Ultrasonic(int pin)
{
   _pin = pin;
}
/*Begin the detection and get the pulse back signal*/
void Ultrasonic::DistanceMeasure(void)
{
    pinMode(_pin, OUTPUT);
   digitalWrite(_pin, LOW);
   delayMicroseconds(2);
   digitalWrite(_pin, HIGH);
   delayMicroseconds(5);
   digitalWrite(_pin,LOW);
   pinMode(_pin,INPUT);
   duration = pulseIn(_pin,HIGH);
}
/*The measured distance from the range 0 to 400 Centimeters*/
long Ultrasonic::microsecondsToCentimeters(void)
{
   return duration/29/2;   
}
/*The measured distance from the range 0 to 157 Inches*/
long Ultrasonic::microsecondsToInches(void)
{
   return duration/74/2;   
}

Ultrasonic ultrasonic(7);
----------------------------------------------

As for my code... I've incorporated it into the Yun Bridge demo...   I added to the REST api so I can get distance measurements via a web browser...
void distanceCommand(YunClient client) {
 
  //client.println("distance Called");

   long RangeInInches;
   ultrasonic.DistanceMeasure();// get the current signal time;
    RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
  // Send feedback to client
  client.print(F("The distance to obstacles in front is: "));
  client.print(RangeInInches);
  client.println(F(" inches"));

}


jremington

You can certainly expect better performance from that sensor than you are getting, but other than "defective part" I have no idea why it is performing so poorly.


Arnesh

I have an Ultrasonic range finder HCSR004.   In the back side of these model I saw a IC named MAX232 along with the other ICs..I search at google and found that MAX232 is used for serial data communication and it is used in this module to dirive the transducer.

It is not clear to me at all.. Can anyboby please help me about that ?

AWOL

Quote
I have an Ultrasonic range finder HCSR004.   In the back side of these model I saw a IC named MAX232 along with the other ICs..I search at google and found that MAX232 is used for serial data communication and it is used in this module to dirive the transducer.

It is not clear to me at all.. Can anyboby please help me about that ?
The MAX232 is used to drive the ultrasonic transmitter at +/-12V (typically), which allows for a much louder ping than if you just used the 5V supply.

Arnesh

thanks for ur early reply..now it is little bit clearer to me... :)

Arnesh

 Can u please send me the circuit diagram of the Ultrasonic Transmitter which can help me to understand your words, how +/- 12 is more efficient then 5v to allow larger ping..

AWOL

Here's a fairly typical one.

Quote
how +/- 12 is more efficient then 5v to allow larger ping
Imagine connecting a 5V source to a loudspeaker.
Now imagine connecting a 24V source to the same loudspeaker.

Arnesh

I designed a program to sense an object within the range of 30 cm HCSR04 (the ultrasonic range finder).It was run successfully..Now I want to modify this using 2 HCSR004 connecting back to back to perform the same task. It was run properly but a little delay is noticed to sense an object.... Can anybody help me to get rid of this delay problem ????   

AWOL

Quote
I designed a program
. . . which I'm not going to show you.


AWOL

?????????????????????  :smiley-sad-blue:  :smiley-sad-blue:  :smiley-sad-blue:  :smiley-sad-blue:
What was the point of that?
Just post your code and state your problem.

Go Up