How to Use the RPLIDAR 360° Laser Scanner With Arduino

51K5518

Intro: How to Use the RPLIDAR 360° Laser Scanner With Arduino

I'm a big fan of building sumo robots and I'm always on the look for new interesting sensors and materials to use to build a better, faster, smarter robot. I found out about the RPLIDAR A1 which you can get for $99 at DFROBOT.com . I said I was interested in trying out this sensor and they gave me the chance to try one. After I got the LIDAR I found out I was not allowed to use this type of sensor in the competition I'm planning to attend because it's too expensive.

In this tutorial I'll give you a basic understanding of how this sensor works and how you can use it with an arduino.

STEP 1: LIDAR What?

Wikipedia had two different meanings for LIDAR:
- LIght Detection And Ranging
- Laser Imaging Detection And Ranging

In general it can be seen as a distance sensor on a rotating base, which samples range data while continuously rotating. For each successful sample the measured distance is sent with the according angle.
In this case the data is sent over UART.

STEP 2: Using an Arduino to Interface With the LIDAR

The LIDAR needs to be connected to the hardware serial port (UART) of the Arduino, for an basic arduino this means you can't send any UART data to the serial console. Because they have only one Serial port. For basic Arduino's with only 1 hardware serial port, you can add an extra software serial port. The library that'll be used in this instructable doesn't support a software serial port (at the moment of writing).

So if you want to be able to send send serial data to the serial console for debugging purposes you'll need an arduino with more than one Serial port. Like there are: Arduino Mega, Arduino M0 / Zero (SAMD21).
If you don't need to send data to the computer to debug you can pick any arduino.

For this instructable I'll be using a more modern and more powerful arduino based on the SAMD21 microcontroller, The Arduino M0 / Zero.

Required parts:

  1. First you'll need to install the RPLIDAR library from robopeak:

    https://github.com/robopeak/rplidar_arduino, this library offers 2 interesting examples on how to use the LIDAR. You can find more information on how to install a library in the arduino IDE in this article: https://www.arduino.cc/en/Guide/Libraries

  2. Now open the "simple_connect" example code that is included in the library.
  3. Make the necessary hardware connections from the LIDAR to the arduino:
    - Connect the RPLIDAR's serial port (RX/TX/GND) to your Arduino board (Pin 0 and Pin1)

    - Connect the RPLIDAR's motor ctrl pin to the Arduino board pin 3
    - Use an external 5V power supply to power the motor control pin and the LIDAR.

  4. In the void setup you'll have to change following code: "lidar.begin(Serial);" according to the used serial port. When using an Arduino M0 you should change "Serial" to "Serial1". This can be different for other Arduino's.
  5. The first picture is what the example sketch should look like, lines 60-63 contain the variable names which you can use to get the data from the LIDAR.
  6. If you want to know what this data looks like you can print this data to the serial monitor, with the code in the second picture.

In the following step we'll make a very basic working project with the LIDAR and a LED ring.

STEP 3: Making the LIDAR LED Ring Project


For this project we will mount an addressable led ring onto the LIDAR. This way we can visualize the LIDAR data.
In this specific program the LED will be turned on in the direction of the closest detected signal.

The code for this project is based on one of the examples from robopeak:

https://github.com/robopeak/rplidar_arduino/tree/m...

The changed code for this project is included in the zip file in this step.

Needed parts:

- LED ring: with 24 LEDS big enough to fit over the LIDAR, inner diameter 70mm
- Arduino Zero
- LIDAR
- Separate 5V power supply
- 3D printed part: https://www.thingiverse.com/thing:3185216

  1. Get all the needed parts
  2. Solder the wires to the LED ring
  3. Glue the LED ring to the 3d printed part
  4. Mount the 3D printed part onto the LIDAR, there are holes in the 3Dprinted part for M2.5 screws but I didn't had them laying around to I just used hot glue
  5. Connect the wires from the LIDAR to the arduino:
    GND -> GND
    5V -> 5V of separate power supply
    Di -> pin D5 of arduino
  6. Upload the sketch and power up the external power supply

Final result can be viewed here on youtube:

https://www.youtube.com/watch?v=L1iulgiau0E

11 Comments

Hello, I have the following question: How can I measure the distance of an object with the RPLidar at a defined angle (e.g. 80 degrees - 100 degrees) and display it on an LCD? Can someone help me .... Thanks XXAL Frage
Hi. Awesome job. I would like to know how can I get the data of distance and angle in a text file or at least in the serial monitor. And how can I programm the scannings in a certain amount of time. i.e. I want it to scan every 30 seconds.
Hi. Nice idea. Thank you to share it.
May be some small errors get into your text, please tell me if I'm wrong (see enclosing multiple stars *** below):

Error1: May be replace LIDAR->LED RING
Phrase: Connect the wires from the ***LIDAR*** to the arduino:


Error2: May be replace "motor control pin and the LIDAR" with "LIDAR's V5.0 and VMOTO pin"
Phrase: Use an external 5V power supply to power the ***motor control pin and the LIDAR***.

So, can i resume the pin mapping as the following ?:
RPLIDAR GND: ARDUINO-GND
RPLIDAR TX:: ARDUINO RX (hardware serial0 or serial1 or serial2)
RPLIDAR RX: ARDUINO TX (hardware serial0 or serial1 or serial2)
RPLIDAR V5.0: POWER-+5V
RPLIDAR GND: POWER-GND
RPLIDAR MOTO CTL: ARDUINO-PIN-3
RPLIDAR VMOTO: POWER-+5V
RING-24LEDS GND: POWER-GND
RING-24LEDS VCC: POWER-+5V
RING-24LEDS D1: ARDUINO-PIN-5
ARDUINO-VIN: POWER-+5V
ARDUINO-GND: POWER-GND

Best regards
Where would you connect the Analog pin?
Do you need to use a lvl shifter in order to make this work? Also would it be possible to get a better angle of the set up?
Hallo, könnte man den code so ändern dass nicht die LED`s leuchten sondern der Wert des Winkels und der Distance auf dem seriellen Monitor dargestellt wird ?
Yep, just use Serial.print(distance); and Serial.print(angle);, check out the author's screenshots in step 2.
Hallo, vielen Dank. Das werde ich sofort umsetzen...Hätte ich auch allein drauf kommen können....Danke. Mal eine Frage nebenbei - wie alt sind Sie eigentlich ? Sie sehen auf dem Bild noch sehr jung aus - und schon so erfahren im Umgang mit dem Arduino.....super. Ich bin 70 und soll nun ein Projekt umsetzen von dem ich so gut wie nichts weiß. Der RPLidar ist absolutes Neuland - na ja, wenn man so schlaue Menschen neben sich hat geht das schon irgendwie......also nochmal vielen Dank.
Hallo, genau das, was ich gesucht habe. Leider zeigt er mir beim kompilieren an (ich beziehe mich auf den rechten Sketch) ..SerialUSB is not declared..Bin mal einfach rein geworfen worden in diese Problematik und versuche nun mich rein zu finden. Wir möchten die Daten des RPLidar auslesen und auf dem seriellen Monitor darstellen...erst mal. Kann mir bitte jemand helfen ? Ich habe den RPLidar A1, Arduino Mega.

Vielen Dank
XXAL
Interesting 'ible. Thanks.