Shop OBEX P1 Docs P2 Docs Learn Events
Parallax LaserPING 2m Rangefinder — Parallax Forums

Parallax LaserPING 2m Rangefinder

JaguarJaguar Posts: 3
edited 2021-05-05 21:57 in General Discussion

Hello to everyone,

I need some help, I can't get my sensor to work properly.
I don't move anything but the values change all the time
the obstacle is 4 cm from the sensor.
thank you for your help :'(
i use arduino mega and sensor is connected in pin A7 PWM

const int pingPin = 7;
void setup() {
  // initialize serial communication
Serial.begin(9600);
}

void loop() {
  // establish variables
long duration, inches, mm;
//Initialize sensor in Serial mode
pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

//Initialize the measurement of the echo
pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

//convert into a distance
 inches = microsecondsToInches(duration);
  mm = microsecondsToMillimeters(duration);
Serial.print( "Duration: " ); Serial.println( duration );

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

  delay(1000);
}
long microsecondsToInches(long microseconds) {

  return microseconds * 6.752;
}
long microsecondsToMillimeters(long microseconds) {

 return microseconds * 171.5;

}

Comments

  • JRoarkJRoark Posts: 1,215
    edited 2021-05-05 23:04

    Question: what are you using as a target? If the target is narrow, the sensor may have difficulty “seeing” it. Remember that the field of illumination is 23° degrees, and the field of view is 55° degrees. Stick a big one out there initially for debugging and put it midway between the allowable measuring extremes... ie range should be about 75 cm.

    Perhaps you could also try putting the device into serial mode instead of using pulse mode. Then you can eliminate any possibility of your code being wonky and just read the distance value directly as serial data.

  • Thank you for your reply,
    my target is a big box, I have tried many targets, but always the same thing.

    I wrote a code for the serial mode but I have only 0 as values.

    const int pingPin = 7;
    bool fristime = false;
    void setup() {
      // initialize serial communication
    Serial.begin(9600);
    }
    
    void loop() {
    
    
    
    if (!fristime){
    pinMode(pingPin, OUTPUT);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(100);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(5);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(100);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(5);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(100);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(5);
      Serial.print("finish");
      fristime=true;
    }
      int readlaser = digitalRead(7);
      Serial.print (readlaser);
    
    }
    
  • Don't have experience with this sensor or Arduino, but according to the documentation at https://www.parallax.com/package/laserping-2m-rangefinder-product-guide/, the LaserPing sends ASCII data in Serial mode.
    Do you need to convert from ASCII to integer before printing to the terminal?

    "...In Serial mode, LaserPING will constantly send new measurement data in ASCII format. The
    value will be in millimeters, and followed by carriage return character (decimal 13). A new value
    will be transmitted each time the sensor receives a valid reading, typically once every 45 ms...."

  • Since you didn't share your code to drive the Laser Pin sensor I assume that it's not setup correctly.

    I don't have an UNO to try this with but I do have an Arduino Zero.

    Here is the code I came up with for the pulse input:

    /**
     * @brief Control Laser Ping board
     * @author Michael Burmeister
     * @date May 6, 2021
     * @version 1.0
     */
    
    #define PIN 10
    
    
    void setup() {
      SerialUSB.begin(115200);
    
      delay(5000);
    
      SerialUSB.println("Starting");
      //pinMode(PIN, OUTPUT);
    
    }
    
    void loop() {
      int i = 0;
    
      /* do triger a reading */
      pinMode(PIN, OUTPUT); 
      digitalWrite(PIN, LOW);
      digitalWrite(PIN, HIGH);
      digitalWrite(PIN, LOW);
    
      /* setup to read the pulse */
      pinMode(PIN, INPUT);
      i = pulseIn(PIN, HIGH, 15000);
      /* convert microseconds to mm * 10 */
      i = i * 1715;
      i = i / 1000;
      SerialUSB.println(i);
      delay(1000);
    }
    

    Mike

  • JaguarJaguar Posts: 3
    edited 2021-05-14 12:23

    I have tried both solutions but neither gives me correct values. Is this sensor really working or is there a problem?
    I did a lot of research on google and tried several codes but my problem is still the same.
    I have two Parallax sensors and both give me different values.

  • VonSzarvasVonSzarvas Posts: 3,484
    edited 2021-05-14 14:02

    Do you have a serial to USB converter ? Something like the PropPlug ?

    You could put LaserPing into serial mode (the user guide mentions which of the golden settings pads to short together, to make Serial mode the default).
    Then attach the LaserPing serial output pin to the PropPlug RX pin.
    And on the PC, open a serial terminal with baud rate set to whatever the LaserPing user guide says it uses.

    Then (after power cycling the LaserPing) you can see the data coming direct from the LaserPing, and you should be able to determine if the issue is with the Arduino or the LaserPing.

    (And if you have a fixed distance test object, you could compare your results to the same distance measured with your Arduino code...)

Sign In or Register to comment.