ESP32 Serial communication with A1 Lidar

hhelmbold
Posts: 7
Joined: Wed May 27, 2020 3:12 pm

ESP32 Serial communication with A1 Lidar

Postby hhelmbold » 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 :

  1. /*
  2.    RPLidar on ESP32
  3.  
  4. */
  5.  
  6. #include <HardwareSerial.h>
  7. //HardwareSerial LiSerial(1);
  8. #include "esp32-hal-ledc.h"
  9.  
  10. // This sketch code is based on the RPLIDAR driver library provided by RoboPeak
  11. #include <RPLidar.h>
  12.  
  13. // You need to create an driver instance
  14. RPLidar lidar;
  15.  
  16. #define RPLIDAR_MOTOR 21 // The PWM pin for control the speed of RPLIDAR's motor.
  17.  
  18. // setting PWM properties
  19. const int freq = 10000;
  20. const int ledChannel = 1;
  21. const int resolution = 8;
  22.  
  23. void setup() {
  24.   Serial.begin(115200);
  25.  
  26.   // bind the RPLIDAR driver to the ESP32 hardware serial2
  27.   lidar.begin(Serial2);
  28.  
  29.   // configure LED PWM functionalitites
  30.   ledcSetup(ledChannel, freq, resolution);
  31.  
  32.   // attach the channel to the GPIO to be controlled
  33.   ledcAttachPin(RPLIDAR_MOTOR, ledChannel);
  34.  
  35.   // set pin modes
  36.   pinMode(RPLIDAR_MOTOR, OUTPUT);
  37.  
  38. }
  39.  
  40. void loop() {
  41.     if (IS_OK(lidar.waitPoint())) {
  42.       float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
  43.       float angle    = lidar.getCurrentPoint().angle; //anglue value in degree
  44.       bool  startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
  45.       byte  quality  = lidar.getCurrentPoint().quality; //quality of the current measurement
  46.  
  47.       //perform data processing here...
  48.       Serial.println("Lidar Get info...");
  49.  
  50.     } else {
  51.       ledcWrite(RPLIDAR_MOTOR, 0);
  52.       delay(15);
  53.       // try to detect RPLIDAR...
  54.       rplidar_response_device_info_t info;
  55.       if (IS_OK(lidar.getDeviceInfo(info, 100))) {
  56.          // detected...
  57.          Serial.println("Lidar Detected...");
  58.          lidar.startScan();
  59.  
  60.          // start motor rotating at max allowed speed
  61.          ledcWrite(RPLIDAR_MOTOR, 255);
  62.          delay(1000);
  63.       }
  64.     }
  65.     Serial.println(lidar.getCurrentPoint().distance);
  66. }
Attachments
ESP-Lidar_bb.jpg
ESP32 wiring diagram
ESP-Lidar_bb.jpg (418.65 KiB) Viewed 7496 times
UNO-Lidar_bb.jpg
Arduino Wiring Diagram
UNO-Lidar_bb.jpg (440.38 KiB) Viewed 7496 times

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: ESP32 Serial communication with A1 Lidar

Postby GeorgeFlorian1 » Mon May 24, 2021 7:56 am

Hello. Have you ever made this work on the ESP32 ?

bhalbach
Posts: 1
Joined: Sun Oct 10, 2021 4:23 am

Re: ESP32 Serial communication with A1 Lidar

Postby bhalbach » Sun Oct 10, 2021 4:26 am

Did you ever find a solution to your issues with the ESP32.

Who is online

Users browsing this forum: No registered users and 37 guests