Shop OBEX P1 Docs P2 Docs Learn Events
Ping))) sensor error — Parallax Forums

Ping))) sensor error

Hello I connected my ping sensor to an arduino board. This is my problem: some times the sensor readings give me values as high as 300cm when the closest object is located at less than 100 cm. I am using the sensor as a vertical movement detector. Is there any way i can filter the data in order to ignore this high values?
This is my code:

const int sigPin = 7;
int buzzPin = 8;
int ledPin = 9;

float distance;
float difference;

void setup() {
Serial.begin(9600);
pinMode(buzzPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}

long distanceOverTime(long first,long second){
return ((first-second)/1);//taking cm/s to mph
}

long holder;//store the cm from last time through loop.
long temp;//used to store the speed value after changes
int counter;

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
int tens;
int ones;
long Speed;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(sigPin, OUTPUT);
digitalWrite(sigPin, LOW);
delayMicroseconds(2);
digitalWrite(sigPin, HIGH);
delayMicroseconds(10);
digitalWrite(sigPin, LOW);


// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(sigPin, INPUT);
duration = pulseIn(sigPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
difference= distance-cm;
distance=cm;
if(abs(difference) > 3)
{
digitalWrite(ledPin,HIGH);
digitalWrite(buzzPin,HIGH);
}
else
{digitalWrite(ledPin,LOW);
digitalWrite(buzzPin,LOW);}
Speed=distanceOverTime(holder,cm);
holder=cm;//after speed caclulation so take the cm value for another calculation
Speed=abs(Speed);
if(Speed!=0 || counter==10){
temp=Speed;
counter=0;
}else{
counter++;
}



Serial.print(inches);
Serial.print(" in, ");
Serial.print(cm);
Serial.print(" cm, ");
Serial.print(Speed);
Serial.print(" cm/s");
Serial.println();
Serial.println("speed_gun_code");

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Comments

  • What kind of object are you trying to detect?

    Some objects either absorb the ultrasound or reflect the sound in the wrong direction.

    Have you looked at the "NewPing" library? It lets you set various filters for the sensor. It might give you better results.

    If you have trouble finding the library, let us know. I'm sure I could find it with a bit of searching.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2016-05-13 17:59
    jhonjairo wrote: »
    Is there any way i can filter the data in order to ignore this high values?

    That would depend on how you want to do it. Search the Arduino forums; over the years, several have posted routines and libraries for averaging, and some have included a way to throw out statistical aberrations -- values in a sequence that are way too high or low given the others in the group. In basic statistical analysis, you don't want those influencing the average.

    One such averaging sketch is Smoothing. I don't recall if it has code to toss out values above/below allowed variances.

    A single type of sensor will most likely always yield the occasional errors. If you can, combine those Ping reads with another distance measurement sensor, such as one of the Sharp IR sensors. These are really more proximity sensors that can be used over a given distance range, but they're good as "sanity checks" for ultrasonic sensors.

    You can also add a second Ping, compare the results, and throw out anything that's beyond an expected variation. Just make sure they're not both firing at the same time.

    Finally, I didn't notice it at first, but part of your code suggests you're measuring a moving object. Always a tough one for a sensor that relies on sound, and the object is moving toward or away from the sensor -- Doppler effect, and all that.

  • why wont my ping stay on my activity bot. It wont stay in place. It keeps falling out
  • Duane, could this be what you are talking about, thanks for the HU.
    I have been trying to convert BS2 Ping-Dar to the Propeller, stuck on the call ABS. Finding this library will get me back into this.

    https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
  • This is NOT a working code, This library should jog some new methods out of my feeble attempts at code conversion.
  • why wont my ping stay on my activity bot. It wont stay in place. It keeps falling out

    Are you using the breadboard to hold the Ping sensor? This is of course the easy way to do this but the little pieces of metal inside the breadboard eventually get bent out of shape. It would probably be a good idea to make some sort of bracket for the Ping.

    If you want to continue to mount the Ping in the breadboard, you might want to try cutting some foam board to use as a brace for the Ping. You might be able to hold the sensor in place with a couple pieces of tape.

    These same materials could be used to mount the Ping off of the breadboard.

    I initially mounted a pair of Ping sensors with a couple pieces of Gorilla Tape on my Cleaver robot.

    index.php?action=dlattach;topic=166.0;attach=529;image

    I mounted the sensors upside down so the pins stuck out the top.

    index.php?action=dlattach;topic=166.0;attach=553;image

    I later made some mounts using expanded PVC and a pan/tilt device.

    index.php?action=dlattach;topic=166.0;attach=500;image

    Parallax sells a variety of wires with connectors you could use with the Ping. Here's their six inch connector.

    800-00060.png?itok=h7l6agkm

    If you type in "Ping bracket" into the search box of the Parallax store you'll see a variety of mounting options. Here's an option using a servo.

    570-28015_0.png?itok=aL1FdSlb

    I should have mentioned these last options first.
Sign In or Register to comment.