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

Ping sensor problem

MadsChristiansenMadsChristiansen Posts: 2
edited 2012-07-23 19:14 in Accessories
Hello Parallax.

I just received my package from parallax yesterday, as excited as i was i started assembling a robot that i've got under construction, but when i finally came to actually test the ping sensor itself it came up with false readings.

The pingsensor seems to have a problem detecting objects further away than 14cms, as long as an object is within 14cm's it gives correct readings, but as soon as i turn it away (maybe facing a wall that is 40cms away) it comes up with the reading "14cms". I have the sensor connected to a Arduino Duemilanove board and im using port 7.

The code that im using is from arduinos own website showing you how to use the parallax ping sensor with arduino..

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin =7;

voidsetup(){
// initialize serial communication:
Serial.begin(9600);
}

voidloop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);

// The same pin is used to read the signal from the PING))): 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(pingPin,INPUT);
duration =pulseIn(pingPin,HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

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

  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-03-17 08:21
    Use this example as the baseline for code and hookup

    http://learn.parallax.com/KickStart/28015

    and be sure the Arduino is powered by a good source -- a 9V battery is okay as long as its fresh. Some USB ports are particularly noisy, so try connecting the battery (the Arduino will switch over to the battery even if USB is connected).

    Try a different room. On some occasions people here have experienced a similar problem and it turned out to be a proximity alarm system in their workplace.

    -- Gordon
  • MadsChristiansenMadsChristiansen Posts: 2
    edited 2012-03-17 08:48
    Use this example as the baseline for code and hookup

    http://learn.parallax.com/KickStart/28015

    and be sure the Arduino is powered by a good source -- a 9V battery is okay as long as its fresh. Some USB ports are particularly noisy, so try connecting the battery (the Arduino will switch over to the battery even if USB is connected).

    Try a different room. On some occasions people here have experienced a similar problem and it turned out to be a proximity alarm system in their workplace.

    -- Gordon

    Hello Gordon, thank you for the quick reply.

    I tested the code on my arduino with a battery case connected to it and it still gave the same results and even after i changed room nothing changed. Could it be due to too much voltage or amperes?
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-03-18 18:10
    It could be caused by dropping it into tomato soup! Most anything is possible. Best to describe your electrical hookup with reasonable detail, and we can go from there.

    -- Gordon
  • JwoozyJwoozy Posts: 2
    edited 2012-07-23 18:51
    So I've been having the issues with the Ping))) sensor when hooked up to my Arduino Mega 2560 board.
    I have the signal placed in the AREF, input 7 slot. and the GND and power jump-wires in the right spots. But when I plug in my board and look at the serial monitor it only shows weird characters.
    "AWAˆ˜!˜¸°ˆ!SWSSˆaGWˆWW°°AGWWAS׸°GSW×aטaSˆ˜ˆAAW°ASA" such as this. Instead of showing distance in inches or cm.

    does anybody have any suggestions on how to fix this?

    Also here is the code I am using

    //Project Ping Distance Sensor


    const int pingPin = 7;


    void setup() {
    Serial.begin(96000);
    }


    void loop() {
    /* establish variables for duration of the ping,
    and the distance result in inches and centimeters: */
    long duration, inches, cm;


    // The Ping))) is triggered by a High pulse of 2 or more
    //microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse;
    pinMode(pingPin,LOW);
    digitalWrite(pingPin,LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin,HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);


    /*The same pin is used to read the signal from the PING))
    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(pingPin,INPUT);
    duration = pulseIn(pingPin,HIGH);


    //convert the time into a distance
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);


    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
    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


    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;

    }
  • JwoozyJwoozy Posts: 2
    edited 2012-07-23 18:53
    Also when I plug my board to the PC, the Parallax Ping))) doesn't turn on or show the green light stating its on.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-07-23 19:14
    Jwoozy wrote: »
    So I've been having the issues with the Ping))) sensor when hooked up to my Arduino Mega 2560 board.
    I have the signal placed in the AREF, input 7 slot. and the GND and power jump-wires in the right spots.

    AREF shouldn't be used for something like this. In fact, plugging things into there that shouldn't be can wreck your Arduino. You use Ground, Power, and digital pin 7.
    But when I plug in my board and look at the serial monitor it only shows weird characters.
    "AWAˆ˜!˜¸°ˆ!SWSSˆaGWˆWW°°AGWWAS׸°GSW×aטaSˆ˜ˆAAW°ASA" such as this. Instead of showing distance in inches or cm.

    You don't have the baud rate set correctly in the Serial Monitor window. The code you posted sends data at 9600 baud (actually, your code shows it as "96000" -- that's not correct). Be sure the Serial Monitor window is set to the same.

    Go here:

    http://learn.parallax.com/KickStart/28015

    and use this code for testing the Ping.

    -- Gordon
  • Ping sensor malfunction
    I have new 9 ping sensors for 3 robots and all sensors are malfunction after second time of usage. I verified using simple sketch with serial.prinln function and all showed a fix number ??
    Checked voltage of usb power, checked connection
    What wrong with these ping sensor ?
    Thanks
  • Your questions are tough to answer without more information.

    For example, could be a code issue, connection issue, voltage issue...

    Can you post a photo showing the PING and microcontroller connections.
    Also, what voltage level are you using? (5V ?)
    And what code are you running ? (You can post the code as an attachment, or inline using the code tags- click the C in the toolbar)

  • Check the voltage right at the header of the PING module. Make sure you have stiff, solid +5 volts.

    The odds that 9 PING modules are bad approaches zero and there just isnt that much on a PING to go bad unless you smack it with an overvoltage on the supply rails. Maybe you could whip something up (maybe a waveform generator with an external trigger or even a little 555 timer circuit) that could emulate the PING and verify your code works?
Sign In or Register to comment.