Laser Rangefinder & Max485 Communication

Hello, I'm working on creating a system that utilizes two U81 Laser Rangefinder (LRF) sensors from Chengdu JRT, transmits the data via RS485 to two Max485 modules, and then to my Arduino Mega 2560 r3. I've been using the datasheet from the manufacturer but am only able to get the LRF's to return zeros. I have verified voltages at all critical locations, and tested the circuit at each stage of increasing complexity.
My test program is as follows:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(18, 19); // RX, TX - Pins for the laser range finder

// COMMANDS FROM DATASHEETs
byte one_shot_auto[9]  = {0xAA, 0x00, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x21};
byte read_measurement_result[9] = {0xAA, 0x00, 0x00, 0x22, 0x00, 0x03, 0x00, 0x00, 0x00};
byte laser_on[9] = {0xAA, 0x00, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x01, 0xC1};   
byte laser_off[9]  = {0xAA, 0x00, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x00, 0xC0};  

// VARs FOR WORKING WITH DATA
unsigned long Data = 0;
byte Read[13]; // Increased to 14 to accommodate the reply from the sensor

const int PWRENPin = 23; // PWREN pin connected to Pin 23

void setup() {
  Serial.begin(19200); // Set baud rate for Serial Monitor
  pinMode(PWRENPin, OUTPUT); // Set PWRENPin as an output pin

  digitalWrite(PWRENPin, HIGH); // Power on the laser range finder
  delay(1000); // Wait for the sensor to initialize

  mySerial.begin(19200); // Set baud rate for communication with the laser range finder
  mySerial.setTimeout(0.001);
   //test ON and OFF pointer
  Serial.println("LASER POINT ON...");
  mySerial.write(laser_on, 9);
  delay(1000);
  Serial.println("LASER POINT OFF...");
  mySerial.write(laser_off, 9);
  delay(200);
}

void loop() {
  Serial.println("read one shot mode: ");
  Data = 0;
  mySerial.write(one_shot_auto, 9);

   //Received command in HEX format
  while (mySerial.available()>0){
  mySerial.readBytes(Read,13);
  }

//Print Data for test
  Serial.println(" ");
  Serial.print("Value in HEX:");
  for (int i = 0; i <= 12; i++){
  Serial.print(Read[i],HEX);  
  }
  //Print only measure data
  Serial.println(" ");
  Serial.print("Measured Data in HEX:");
  for (int i = 6; i <= 9; i++){
  Serial.print(Read[i], HEX);
  }
  // If you want convert to Decimal
  for (int i = 6; i <= 9; i++){
  Data = (Data |= Read[i]) << 8 ;
  }
  Data = Data >> 8;
  //Print in mm
  Serial.println(" ");
  Serial.print("Measurement in mm: "); 
  Serial.println(Data, DEC);
  delay(1000);
}

I was going to upload datasheets with this post but was blocked d/t being a new user, will try again in a couple days.
I've looked at countless forum articles, and feel like the answer is staring me in the face.
Thanks in advance for any and all help! :slight_smile:

Why are you using software serial?

1 Like

Software serial is shaky at best especially at the faster baud rates. It will only operate in simplex mode (It can transmit or receive) but not concurrently. It is also partly blocking. The mega has four serial ports, one being committed to the serial monitor, the rest are open. Simple hint, it is almost always easier to use a hardware function then a software one.

Okay I was just using software serial b/c that's what was used in the test code provided by the manufacturer. Would it just be easier to wire the laser range finder directly into the TX / RX ports on my Arduino then from the sounds of it? Bit of an arduino noob, just trying to stumble my way through xD

That sets the timeout in milliseconds, which does not allow fractions of milliseconds.

I'm concerned about the use of the past participle there.

Try a simple sketch to trigger a reading and then print whatever comes back

Yes, and that should be the first test of the setup, with a hardware serial port.

The RS485 modules are an added complication and should be tested independently of the LRF.

1 Like

Will fix that, honestly just pulled that from the example test code from the manufacturer. Thank you!

Will try this right away, thank you!

The LRF's just came with the RS485 modules soldered on but I'll detach them and try it directly into the TX / RX ports. Thanks for all of the suggestions folks, I really appreciate all of the help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.