RPLidar A2M6 with Arduino Due

Hi guys!

I’ve been trying to connect the RPLidar A2M6 to my Arduino Due but I can’t get it to work.

Datasheet RPLidar: https://www.robotshop.com/media/files/pdf2/ld206_slamtec_rplidar_datasheet_a2m6_v1.1_en_2_.pdf

I use the library for the RPLidar and the provided sample code:

/*
 * RoboPeak RPLIDAR Arduino Example
 * This example shows the easy and common way to fetch data from an RPLIDAR
 * 
 * You may freely add your application code based on this template
 *
 * USAGE:
 * ---------------------------------
 * 1. Download this sketch code to your Arduino board
 * 2. Connect the RPLIDAR's serial port (RX/TX/GND) to your Arduino board (Pin 0 and Pin1)
 * 3. Connect the RPLIDAR's motor ctrl pin to the Arduino board pin 3 
 */
 
/* 
 * Copyright (c) 2014, RoboPeak 
 * All rights reserved.
 * RoboPeak.com
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
 
// This sketch code is based on the RPLIDAR driver library provided by RoboPeak
#include <RPLidar.h>

// You need to create an driver instance 
RPLidar lidar;

#define RPLIDAR_MOTOR A3 // The PWM pin for control the speed of RPLIDAR's motor.
                        // This pin should connected with the RPLIDAR's MOTOCTRL signal 
                       
                        
void setup() {
  // bind the RPLIDAR driver to the arduino hardware serial
  lidar.begin(Serial);
  
  // set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);
}

void loop() {
  if (IS_OK(lidar.waitPoint())) {
    float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
    float angle    = lidar.getCurrentPoint().angle; //anglue value in degree
    bool  startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
    byte  quality  = lidar.getCurrentPoint().quality; //quality of the current measurement
    
    SerialUSB.println("Distance: ");
    SerialUSB.print(distance);
    SerialUSB.println("Angle: ");
    SerialUSB.print(angle);
    SerialUSB.println("Quality: ");
    SerialUSB.print(quality);
    
  } else {
    analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
    
    // try to detect RPLIDAR... 
    rplidar_response_device_info_t info;
    SerialUSB.println(lidar.getDeviceInfo(info, 100));
    SerialUSB.println("Trying to connect ... ");
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
       // detected...
       SerialUSB.println("Detected");
       lidar.startScan();
       
       // start motor rotating at max allowed speed
       analogWrite(RPLIDAR_MOTOR, 255);
       delay(1000);
    }
  }
}

This does not create any output whatsoever.
The Lidar doesn’t spin nor does it output data on SerialUSB.

I connected RX/TX from Lidar to RX0/TX0 Due since these are the hardware serial ports.
Motor to Analog3 and VCIn to 5V and Ground to Ground.

If anyone has ever tried connecting the RPLidar A2 to the Arduino, help would be very much appreciated!
Thanks in Advance!

connected RX/TX from Lidar to RX0/TX0 Due since these are the hardware serial ports.
Motor to Analog3 and VCIn to 5V and Ground to Ground.

Try: RX from LIDAR to TX0 of DUE
TX from LIDAR to RX0 of DUE

I used a TFMini in a single trigger mode to read the LIDAR:
I could not find, initially, a library that worked for the DUE so I used, which has been modified to work on an ESP32 but the basics are still there in the getTFminiData function:

#include <HardwareSerial.h>
HardwareSerial SerialTFMini( 2 );
// serial(1) = pin12=RX, pin13=TX
// serial(2) = pin16=RX green, pin17=TX white



void getTFminiData(int* distance, int* strength) {
  static char i = 0;
  char j = 0;
  int checksum = 0; 
  static int rx[9];
  if(SerialTFMini.available())
  {  
    // Serial.println( "tfmini serial available" );
    rx[i] = SerialTFMini.read();
    if(rx[0] != 0x59) {
      i = 0;
    } else if(i == 1 && rx[1] != 0x59) {
      i = 0;
    } else if(i == 8) {
      for(j = 0; j < 8; j++) {
        checksum += rx[j];
      }
      if(rx[8] == (checksum % 256)) {
        *distance = rx[2] + rx[3] * 256;
        *strength = rx[4] + rx[5] * 256;
      }
      i = 0;
    } else 
    {
      i++;
    } 
  }  
}


void setup() {
  Serial.begin(115200);
  
SerialTFMini.begin( 115200, SERIAL_8N1, 27, 26 );

}

void loop() 
{
  int distance = 0;
  int strength = 0;

  getTFminiData(&distance, &strength);
  while(!distance) {
    getTFminiData(&distance, &strength);
    if(distance) {
      Serial.print(distance);
      Serial.print("cm\t");
      Serial.print("strength: ");
      Serial.println(strength);
    }
  }

delay(100);
}

Thanks for the help!

Do you know anything about the <HardwareSerial.h> for the DUE?
It doesn’t have a constructor like the one from the ESP32.
Do I just use Serial then without any specification?

And also, how do you get the angle of the measurement, which is quite important for a 360° Scanner.

Soerili:
Thanks for the help!

Do you know anything about the <HardwareSerial.h> for the DUE?
It doesn’t have a constructor like the one from the ESP32.
Do I just use Serial then without any specification?

And also, how do you get the angle of the measurement, which is quite important for a 360° Scanner.

For the DUE, I would just refer to the Serial port, such as Serial or Serial1 or SerialX. You should, at least, initialize the serial port in setup. I get the angle of measurement from the angle that the servo has been sent. I increment the servo 12 times per degree for +/- 5 degrees off of 90 degrees. The TFMini has a 2 degree transmission cone.