Arduino interface with laser rangefinder

I am trying to interface a laser rangefinder with Arduino nano. I am facing issues in coding for it.

This is the product link.

Can anyone help me interface it with arduino nano through uart interface?

Perhaps, if you will help with the documentation of the protocol being used. What are the issues you have?

Send a request to the supplier for contacts or documentation.

(or try not-so-high-price)

They have shared this communication document

int incomingByte = 0; // for incoming serial data

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

// send data only when you receive data:

if (Serial.available() > 0) {

// read the incoming byte:

incomingByte = Serial.read();



// say what you got:

Serial.print("distance: ");

Serial.println(incomingByte, DEC);

}

}
I tried this code.Its repeating 10 as output.

I think you need to send a command to get it to take either a single measurement, or continuous measurement.

I am trying this but its not working correctly

include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // your arduino RX, TX pins
String str= "";

void setup()
{
// Open serial communications to write to screen:
Serial.begin(115200);
Serial.println("Starting program:");

// set the data rate for the SoftwareSerial port
mySerial.begin(115200);
}

void loop() // run over and over
{
Serial.print("Taking measures... ");
mySerial.write(0X89); //command for continous measurement

if(mySerial.available() > 0)
{
str = mySerial.readStringUntil('\n');
}
Serial.println(str);
delay(500);
}

You don't want to use pins 0 and 1. Those pins are used by the hardware UART. Choose different pins for your software serial port.

Also note that a software serial port on a nano will not work at 115200 baud. You are limited to a maximum of 19200 baud if I recall correctly.

If you must communicate at 115200 baud AND require the use of a second serial port to display results etc, then use the hardware UART on pins 0 & 1 to handle the 115200 comms with the rangefinder and a software serial port set to 9600 to handle the results/output.

1 Like

Okay .I will try this

The command for continuous measurement is not just

mySerial.write(0X89); //command for continous measurement

You have to send an eight byte command:

Also you have shown us 2 sketches, one using 9600Bd and one using 115200Bd.

You need to find out what Baud rate it is using - It won't have changed unless you have successfully sent the command to change it!

My guess is that it would default to the lower Baud rate: 9600Bd.


Its mentioned baud rate is 115200

I am not exactly getting how to code it. The company told me that I don't need to send a command to turn the laser on as it will turn on by default.

**#include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1);//Define software serial, 3 is TX, 2 is RX
unsigned char data[8]={0};
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
}

void loop()
{
//mySerial.print(buff);
while(1)
{
if(mySerial.available()>0)//Determine whether there is data to read on the serial
{
delay(50);
for(int i=0;i<7;i++)
{
data[i]=mySerial.read();
Serial.print(data[i]);
}

  float distance=0;
      distance=data[6]*256+data[7]*0.001 ;
      Serial.print("Distance = ");
      Serial.print(distance);
      Serial.println(" M");
      
delay(20);

}
}
}
trying this code

Getting output as : 255255255255255255255 Distance = 255.00 M
255255255255255255255 Distance = 255.00 M
255255255255255255255 Distance = -256.00 M
255255255255255255255 Distance = -256.00 M
255255255255255255255 Distance = -256.00 M

I read the output port i.e the 7 bytes and the distance measutrement but its giving output like this

The code and the comment do not agree here....

I referred to a different code and made changes to it.
Forgot to change the comments.

Ok. Post the code you are now using - remember to use the code tags when posting it.
How have you now connected up the rangefinder and connected to your PC?

The Laser range finder 's Vcc -> Vin of arduino Nano
Tx -> Rx (pin 1)
Rx -> Tx (pin 0)
GND -> GND

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1);
unsigned char data[8]={0};
void setup()
{
Serial.begin(115200);
mySerial.begin(9600);
}

void loop()
{
//mySerial.print(buff);
while(1)
{
if(mySerial.available()>0)//Determine whether there is data to read on the serial
{
delay(50);
for(int i=0;i<7;i++)
{
data[i]=mySerial.read(); //read 8 bytes
Serial.print(data[i]);//print each byte
}

  float distance=0;
      distance=data[6]*256+data[7]*0.001 ; // byte 6 and 7 are specified as data_h and data_l in the document
      Serial.print("Distance = ");
      Serial.print(distance);
      Serial.println(" M");
      
delay(20);
}
}
}

I am trying with this code presently .
I am getting this as output.

255255255255255255255 Distance = -256.00 M

255255255255255255255 Distance = -256.00 M

255255255255255255255 Distance = -256.00 M

250255255255255255255 Distance = -256.00 M

252255255255255255255 Distance = -256.00 M

See post #9.