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.
-
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? -
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!