Getting started with YDLIDAR X4

Hello,

I’ve just started working with LIDAR for the first time, and I’m having difficulty understanding how to connect it to the Arduino Mega2560 and retrieve data from it (distance and angle). I have 2 questions if anyone would be kind enough to explain them.

  1. Connections to the Arduino: so according to the datasheet https://www.generationrobots.com/media/YDLIDAR-X4.pdf
    the red wire in the picture above is motor enable, black is ground, yellow is Rx, green is Tx and blue is Vcc. This means that I should connect the red pin to a digital pin (say pin 3), black to arduino’s ground, yellow to arduino’s Tx, green to arduino’s Rx and blue to arduino’s 5V. Is that correct?

  2. Getting data: I’ve used (and edited a bit) the code from one of the ‘RPLidar examples’ in order to get distance and angle data from the LIDAR after making the above connections. Here is my code:

#include <RPLidar.h>

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

#define MOTOR_ENABLE 3 
                       
                        
void setup() {
  // bind the RPLIDAR driver to the arduino hardware serial
  lidar.begin(Serial2);
  Serial.begin(9600);
  // set pin modes
  pinMode(MOTOR_ENABLE, 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
    
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" Angle: ");
    Serial.println(angle);
    
  } else {
    digitalWrite(MOTOR_ENABLE, LOW);
    
    // try to detect RPLIDAR... 
    rplidar_response_device_info_t info;
    if (IS_OK(lidar.getDeviceInfo(info, 100))) {
       // detected...
       lidar.startScan();
       
       // start motor rotating at max allowed speed
       digitalWrite(MOTOR_ENABLE, HIGH);
       delay(1000);
    }
  }
}

However, when I’m uploading the code, I’m getting errors such as “Error while setting serial port parameters: 9,600 N 8 1” and “avrdude: ser_send(): write error: sorry no info avail avrdude: stk500_send(): failed to send command to serial port avrdude: ser_recv(): read error: The device does not recognize the command. avrdude: stk500v2_ReceiveMessage(): timeout avrdude: ser_send(): write error: sorry no info avail avrdude: stk500_send(): failed to send command to serial port”. I’ve noticed that when I remove the wire connected to digital pin 3, that the program uploads successfully. However, I’m still not able to get any data from the LIDAR.

Any help would be highly appreciated!

The data sheet you linked to indicates that average working current is 350mA and more during startup. That's too much for the Mega to supply - you'll need a separate power supply.

wildbill:
The data sheet you linked to indicates that average working current is 350mA and more during startup. That's too much for the Mega to supply - you'll need a separate power supply.

Yes that's true. If I'm not mistaken the Arduino Mega supplies around 40 mA per pin. I will be connecting it to a power supply. What is weird though is that once I connect the GND and Vcc wires to the Arduino, the motor start working and the LIDAR starts spinning. Anyways, Do you think that the connections I made made (question 1) are correct though?