ESP32 Serial communication with A1 Lidar
Posted: Sat Jul 04, 2020 8:08 am
I have been working on a project for a while now and I am trying to move it from Arduino over to an ESP32. I am using a A1 Lidar by slamtec and I have it working perfectly on the Arduino but I just cannot get it working on the ESP32. I am still learning a lot about both platforms and I will really appreciate it if someone can help to point out my newby errors and why things aren't working. I am attaching my wiring diagrams for Arduino and for ESP32, and also my sketch for the ESP32.
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 :
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);
- }