I am working on an RPLIDAR A1M8 and an Arduino Uno and I am trying to get measurements from the RPLIDAR. I have tested the module with its own software and it is working perfectly however, when I connect it to the Arduino Uno, I struggle to get any values and most of the time the Serial Monitor just shows "?P?P" repeatedly.
Has anyone had experience with this module and if so did you have any ideas to solve this?
#include <RPLidar.h>
// You need to create an driver instance
RPLidar lidar;
#define RPLIDAR_MOTOR 3 // The PWM pin for control the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal
void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
lidar.begin(Serial);
Serial.begin(115200);
// set pin modes
pinMode(RPLIDAR_MOTOR, 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
bool startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
byte quality = lidar.getCurrentPoint().quality; //quality of the current measurement
//perform data processing here...
if(distance>0){
Serial.print("Angle = ");
Serial.print(angle);
Serial.print(" ");
Serial.print("Distance = ");
Serial.println(distance);
}
else{
}
} else {
analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
// 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
analogWrite(RPLIDAR_MOTOR, 255);
delay(1000);
}
}
}
If I was to buy the MEGA, would I still connect the wires the same way as it shows in the datasheet or would it need to be different? There is only one RX wire and one TX wire so I am unsure as how that would work.
The Mega has four serial interfaces, Serial (which is used to talk to the host, like on a Uno), Serial1, Serial2 and Serial3.
You would use 1, 2 or 3 to talk to the LIDAR.
Check the documentation for "Serial" in the reference, for pin numbers.
You could also use a Micro, which has native USB, plus a UART.