Ydlidar x4 Serial Communication Problem

Hello, we are using a ydlidar x4 for our final year project. At first when we connected with lidar with arduino nano ( providing external supply of 5v to lidar) the lidar starts spinning giving out 0 readings on both parameters (distance and degree)

Here is the Code we are trying to run:

#include <YDLidar.h>
#include “QueueList.h”
#define SIZE_OF_SCAN_BUFFER 32
// You need to create an driver instance
YDLidar lidar;

#define MOTOR_ENABLE 7
#define MOTOR_SENABLE 3

void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
Serial.begin(115200);
lidar.begin(Serial);
// set pin modes
pinMode(MOTOR_ENABLE, OUTPUT);
pinMode(MOTOR_SENABLE, OUTPUT);

}

void loop() {
lidar.startScan();
float distance = lidar.getCurrentScanPoint().distance; //distance value in mm unit
float angle = lidar.getCurrentScanPoint().angle; //anglue value in degree

Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" Angle: ");
Serial.println(angle);



// try to detect RPLIDAR...


   // detected...
   
   
   // start motor rotating at max allowed speed

}

Here is pin diagram:

Help Needed

Did you run the library examples?

Did you try a different SoftSerial for the device?

Yes we tried to run the example but getting this error, also the lidar stops working after sometime with the example code.

This is the code we tried to run : Update ConnectYDLidar.ino by GuyCoast · Pull Request #4 · EAIBOT/ydlidar_arduino · GitHub

Error we are getting

That’s no usable code, do you mean this?

Please learn how to present code, error messages and logs - see the sticky topics on top of the forum.

If deviceinfo is not available then something is very wrong :frowning:

Which is your Serial Monitor baudrate?

Have you tried running Serial at 128000 baud as used in the example? The ydlidar likely expects that specific baud rate, although I am not able to check if the IDE serial monitor supports that at the moment.

The Serial Monitor allows to set and inspect the current baudrate. I wonder if it’s 9600 or 128000 in your case.

I just looked at the IDE on my computer and 128,000 baud is not an option, so another serial monitor program may be necessary. Cannot find any information on changing the baud rate of the Ydlidar X4, it may be fixed at 128,000.

You showed a listing of the error messages, in Serial Monitor or what else?