The lidar is not spinning, which makes me think the PWM is not working or not connected correctly? Running the sketch I get the below results which tells me the Lidar is detected, so I do think the Tx and Rx is working on Serial2.
Lidar Detected...
0.00
Sketch :
- /*
- RPLidar on ESP32
- */
- #include <HardwareSerial.h>
- //HardwareSerial LiSerial(1);
- #include "esp32-hal-ledc.h"
- // This sketch code is based on the RPLIDAR driver library provided by RoboPeak
- #include <RPLidar.h>
- // You need to create an driver instance
- RPLidar lidar;
- #define RPLIDAR_MOTOR 21 // The PWM pin for control the speed of RPLIDAR's motor.
- // setting PWM properties
- const int freq = 10000;
- const int ledChannel = 1;
- const int resolution = 8;
- void setup() {
- Serial.begin(115200);
- // bind the RPLIDAR driver to the ESP32 hardware serial2
- lidar.begin(Serial2);
- // configure LED PWM functionalitites
- ledcSetup(ledChannel, freq, resolution);
- // attach the channel to the GPIO to be controlled
- ledcAttachPin(RPLIDAR_MOTOR, ledChannel);
- // 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...
- Serial.println("Lidar Get info...");
- } else {
- ledcWrite(RPLIDAR_MOTOR, 0);
- delay(15);
- // try to detect RPLIDAR...
- rplidar_response_device_info_t info;
- if (IS_OK(lidar.getDeviceInfo(info, 100))) {
- // detected...
- Serial.println("Lidar Detected...");
- lidar.startScan();
- // start motor rotating at max allowed speed
- ledcWrite(RPLIDAR_MOTOR, 255);
- delay(1000);
- }
- }
- Serial.println(lidar.getCurrentPoint().distance);
- }