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.
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
}
}
}
#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
}
}
}
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.
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.
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?
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.
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.