# Arduino interface with laser rangefinder

**URL:** <https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583>\
**Category:** Sensors\
**Created:** [May 16, 2023, 1:23pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583 "2023-05-16T13:23:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 16, 2023, 1:23pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/1 "2023-05-16T13:23:57Z")

</div>

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

> **[J52 800m Mini Laser Range Finder Module - Buy Mini Laser Range,Laser Range...](https://www.alibaba.com/product-detail/J52-800m-MINI-laser-range-finder_62586173334.html?fromMSite=true)**
>
> J52 800m Mini Laser Range Finder Module - Buy Mini Laser Range,Laser Range Module Product on Alibaba.com

This is the product link.

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

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [May 16, 2023, 3:22pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/2 "2023-05-16T15:22:53Z")

</div>

> [@Sonam\_123](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [May 16, 2023, 5:27pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/3 "2023-05-16T17:27:50Z")

</div>

> [@Sonam\_123](#):
>
> interface

[Send a request](https://settall.en.alibaba.com/contactinfo.html?spm=a2700.shop_index.88.26) to the supplier for contacts or documentation.

([or try not-so-high-price](https://www.electroniclinic.com/tof10120-laser-rangefinder-arduino-display-interfacing-code/))

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 3:55am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/4 "2023-05-17T03:55:10Z")

</div>

![Screenshot_20230517-092414_Office](https://europe1.discourse-cdn.com/arduino/original/4X/0/6/e/06e413ab56a20f68e26e40586e78f0880f4b92a4.jpeg)

They have shared this communication document

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 3:57am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/5 "2023-05-17T03:57:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [May 17, 2023, 5:31am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/7 "2023-05-17T05:31:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 6:26am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/8 "2023-05-17T06:26:49Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [May 17, 2023, 6:43am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/9 "2023-05-17T06:43:53Z")

</div>

> [@Sonam\_123](#):
>
> SoftwareSerial mySerial(0, 1); // your arduino RX, TX pins

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.

> [@Sonam\_123](#):
>
> mySerial.begin(115200)

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.

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 6:49am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/10 "2023-05-17T06:49:25Z")

</div>

Okay .I will try this

---

<div class="post-metadata">

**Author:** ![JohnLincoln](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@JohnLincoln](https://forum.arduino.cc/u/JohnLincoln)\
**Post date:** [May 17, 2023, 6:54am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/11 "2023-05-17T06:54:12Z")

</div>

The command for continuous measurement is not just

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

```

You have to send an eight byte command:

 ![Screenshot_20230517-092414_Office_EE](https://europe1.discourse-cdn.com/arduino/original/4X/1/c/3/1c357a2cddfed62eb45bc6592f266ab9d4b89963.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 6:56am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/12 "2023-05-17T06:56:38Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/0/9/10962c9c1e03a9b4d42f16e2477c399fd0ad7ea6.png)  
Its mentioned baud rate is 115200

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 7:00am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/13 "2023-05-17T07:00:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 12:17pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/14 "2023-05-17T12:17:35Z")

</div>

\*\*#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](https://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

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [May 17, 2023, 12:43pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/16 "2023-05-17T12:43:22Z")

</div>

> [@Sonam\_123](#):
>
> SoftwareSerial mySerial(0,1);//Define software serial, 3 is TX, 2 is RX

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

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 17, 2023, 12:46pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/17 "2023-05-17T12:46:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [May 17, 2023, 1:24pm UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/18 "2023-05-17T13:24:31Z")

</div>

> [@Sonam\_123](#):
>
> I referred to a different code

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?

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 18, 2023, 5:17am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/19 "2023-05-18T05:17:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 18, 2023, 5:17am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/20 "2023-05-18T05:17:33Z")

</div>

```arduino
#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);
}
}
}

```

---

<div class="post-metadata">

**Author:** ![Sonam\_123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonam_123/32/947087_2.png) [@Sonam\_123](https://forum.arduino.cc/u/Sonam_123)\
**Post date:** [May 18, 2023, 5:19am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/21 "2023-05-18T05:19:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [May 18, 2023, 6:27am UTC](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583/22 "2023-05-18T06:27:23Z")

</div>

> [@Sonam\_123](#):
>
> `SoftwareSerial mySerial(0,1);`

See post #9.

[Next page](https://forum.arduino.cc/t/arduino-interface-with-laser-rangefinder/1127583.md?page=2)
