LIDAR scanner and Arduino board

Hello everyone,

I am working on an interactive device project, by collecting the distance data from Lidar and transforming it into prompt to other software on Mac Os.

Now I have trouble to get measurements from the RPLidar C1. When I connect to Arduino Mega 2560, I struggle to get any values beyond the error code 'x??xx??' repeatedly. The baud has been set the same both in code and serial monitor.

Hardware using, Arduino Mega 2560, RPLidar C1, Ran the code on Mac OS
The link of the Lidar unit is https://www.slamtec.ai/wp-content/uploads/2023/11/SLAMTEC_rplidar_datasheet_C1_v1.0_en.pdf.

Below I will attach the code and the mating diagram.
Thanks for all the advice and support.
//
update 16 May 17:22, 2024

Arduino Uno changed to Arduino Mega
Still generate Error code

#include <RPLidar.h>

// Create an instance of RPLidar
RPLidar lidar;

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.

void setup() {
  // Begin serial communication
  Serial.begin(460800);

  // Bind the RPLIDAR driver to the Arduino hardware serial
  lidar.begin(Serial1);

  // Set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);

  // Start the motor
  analogWrite(RPLIDAR_MOTOR, 255);
}

void loop() {
  if (IS_OK(lidar.waitPoint())) {
    float distance = lidar.getCurrentPoint().distance; // Distance in mm
    float angle = lidar.getCurrentPoint().angle; // Angle in degrees
    bool startBit = lidar.getCurrentPoint().startBit; // Whether this point is part of a new scan
    byte quality = lidar.getCurrentPoint().quality; // Quality of the current measurement

    // Output all data to the serial port
    Serial.print("Angle: ");
    Serial.print(angle, 2);
    Serial.print(" Distance: ");
    Serial.print(distance, 2);
    Serial.print(" Start Bit: ");
    Serial.print(startBit);
    Serial.print(" Quality: ");
    Serial.println(quality);

  } else {
    // Stop the RPLIDAR motor
    analogWrite(RPLIDAR_MOTOR, 0);

    // Try to detect RPLIDAR...
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
      // Detected...
      lidar.startScan();
      // Restart the motor
      analogWrite(RPLIDAR_MOTOR, 255);
    } else {
      // If RPLIDAR is not detected, print an error message
      Serial.println("RPLIDAR not detected. Trying again...");
      delay(1000); // Add a delay to avoid flooding the serial output
    }
  }
}


circuit-3

Try after editing your code like this:

#include <RPLidar.h>

// Create an instance of RPLidar
RPLidar lidar;

#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.

void setup() {
  // Begin serial communication
  Serial.begin(115200);

  // Bind the RPLIDAR driver to the Arduino hardware serial
  lidar.begin(Serial);

  // Set pin modes
  pinMode(RPLIDAR_MOTOR, OUTPUT);

  // Start the motor
  analogWrite(RPLIDAR_MOTOR, 255);
}

void loop() {
  if (IS_OK(lidar.waitPoint())) {
    float distance = lidar.getCurrentPoint().distance; // Distance in mm
    float angle = lidar.getCurrentPoint().angle; // Angle in degrees
    bool startBit = lidar.getCurrentPoint().startBit; // Whether this point is part of a new scan
    byte quality = lidar.getCurrentPoint().quality; // Quality of the current measurement

    // Output all data to the serial port
    Serial.print("Angle: ");
    Serial.print(angle, 2);
    Serial.print(" Distance: ");
    Serial.print(distance, 2);
    Serial.print(" Start Bit: ");
    Serial.print(startBit);
    Serial.print(" Quality: ");
    Serial.println(quality);

  } else {
    // Stop the RPLIDAR motor
    analogWrite(RPLIDAR_MOTOR, 0);

    // Try to detect RPLIDAR...
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
      // Detected...
      lidar.startScan();
      // Restart the motor
      analogWrite(RPLIDAR_MOTOR, 255);
    } else {
      // If RPLIDAR is not detected, print an error message
      Serial.println("RPLIDAR not detected. Trying again...");
      delay(1000); // Add a delay to avoid flooding the serial output
    }
  }
}

1 Like

hi many thanks for the fast support, I just tried again, but the outcome still kept repeating 'x??'. Messy code.
I'm using Adruino uno, connected with RPLidar c1, running on the Mac OS platform.
Mating connect with JST XH 5-pin.

Even the outcome of examples code from RPLidar official web is error code

After I talked with the tech support from Slamtec, the company which designed the RPlidar, they said to change the baud rate to 460800, but it was still generate the error code as before.

remember to set the same baudrate in serial monitor as well
Also, you are using same serial to communicate with lidar and your serial monitor. It can't work.
You need another serial for lidar. Which you don't have in uno. You could try softwareserial, but I doubt it works with high speed.

1 Like

Thanks, I did set the same baud rate on the monitor. But for another serial port, does it mean I need to get the Arduino Mega?

Whatever board that has more than one serial. Mega, Esp32, Nano every....

1 Like

Many thx, one more thing is my RPLidar C1 has four interfaces, shall I maintain the same connection later on Mega and only change the operation code?

Hi, @hanczhangserial1
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What pins of the UNO are you using to communicate with the Lidar?
If you are using pins 0 and 1, they are the UNO programming pins and the pins used by Serial print to the IDE monitor.

Can you please post a link to data/specs of the Lidar unit?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Hey Tom, many thanks for the suggestion, just update the post based on the guidance of the Forum standard. The link of the Lidar unit is https://www.slamtec.ai/wp-content/uploads/2023/11/SLAMTEC_rplidar_datasheet_C1_v1.0_en.pdf.

800ma at startup. You need power supply for this, like phone charger 5V1A minimum. I would not power it from arduino pins.

1 Like

Hi Kmin, do you mean to connect the Power Vcc (RED) cable with the individual power supply instead plug on the Arduino uno?

I don't know how you are willing to power your arduino...
Anyway both Vcc and Gnd has to come from power supply, also arduino Gnd and Lidar Gnd have to be connected, if they are powered from different power supplies.
I personally would power them from same 5V1A or 5V2A supply, splitting the wires (5v and Gnd) to both of them.

1 Like

The power situation for now is through the Macbook pro USB C port (around 15W), for both Lidar and Arduino. Thanks, I will take a look at the connect plan for that.

You could try from board 5V pin, but 800mA is out of specs. If weird things happen, it's becoming headache.
But first you need a board with 2 UARTs...

Already switch to Arduino Mega, but it still generate error code...

what error code precisely?
Can you demonstrate your wiring..

This is the wiring situation, the code is "����!k����)����!k�����)�����!k����-������!k����-)��"
circuit-3

I mean the whole actual wiring... :roll_eyes: