Development Routines of TF LiDAR on Arduino

Hello, everyone. I am sharing how to connect Benewake TF LiDAR to Arduino.

May this helpful. 

Step 1: Hardware Connection

    The linear order for the LiDAR products of our company is defined as: red +5V, black GND, green
TX, white RX; the power supply voltage for TF02 is 5V, so it is permissible to directly connect 5V and
GND on the Arduino board. For other LiDARs, please refer to product specification to ensure normal
power supply; For serial communication connection, please note that with regard to Arduino UNO board, the TX end of LiDAR connects to pin 2 port (soft serial port Serial1’s RX) and the RX end of LiDAR connects to pin 3 (soft serial port Serial1’s TX). This is relevant to the programming hereinafter; For Arduino DUE board, the TX end of LiDAR connects to RX1 port and the RX end of LiDAR connects to TX1 port.

3691361513?profile=original

Step 2: Programming


The implementation of this routine functions requires at least two serial ports: one for receiving
data from LiDAR and the other for sending data to PC for display. You can copy the following code to
IDE syntax editor or directly open corresponding attachment files.

/* This program is a parsing routine of TF02 product standard output protocol on Arduino.

The format of data package is 0x59 0x59 Dist_L Dist_H Strength_L Strength_H Sequence_L Sequence_H CheckSum_L

Refer to the product specification for detailed description.

For Arduino board with one serial port, use software to virtualize serial port’s functions: such as UNO board.

*/

#include<SoftwareSerial.h>// soft serial port header file

SoftwareSerial Serial1(2,3); // define the soft serial port as Serial1, pin2 as RX, and pin3 as TX

/*For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use Serial1 port*/

int dist;// LiDAR actually measured distance value

int strength;// LiDAR signal strength

int check;// check numerical value storage

int i;

int uart[9];// store data measured by LiDAR

const int HEADER=0x59;// data package frame header

void setup()

{

  Serial.begin(9600);//set the Baud rate of Arduino and computer serial port

  Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port

}

void loop()

{

  if (Serial1.available())//check whether the serial port has data input

  {

    if(Serial1.read()==HEADER)// determine data package frame header 0x59

    {

      uart[0]=HEADER;

      if(Serial1.read()==HEADER)//determine data package frame header 0x59

      {

        uart[1]=HEADER;

        for(i=2;i<9;i++)// store data to array

        {

          uart[i]=Serial1.read();

        }

      check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];

      if(uart[8]==(check&0xff))// check the received data as per protocols

      {

        dist=uart[2]+uart[3]*256;// calculate distance value

        strength=uart[4]+uart[5]*256;// calculate signal strength value

        Serial.print("dist = ");

        Serial.print(dist);// output LiDAR tests distance value

        Serial.print('\t');

        Serial.print("strength = ");

        Serial.print(strength);// output signal strength value

        Serial.print('\n');

       }

     }

   }

 }

}

Step 3: Data View
    Upload the program to the Arduino board and open the serial monitor to check the distance value
detected by LiDAR in real time and corresponding signal strength as in Figure 3;
    Besides, you can also check the data curves on the serial curve plotter, but for that you need to
modify the code related to serial printing (Serial.print):

//         Serial.print("dist = ");

            Serial.print(dist);// output LiDAR tests distance value

            Serial.print(' ');

//         Serial.print("strength = ");

            Serial.print(strength);// output signal strength value

            Serial.print('\n');

     Re-compile and upload them to Arduino board. Open the serial curve plotter and you can see two
curves representing distance and strength as in Figure 4.

3691361439?profile=original

3691361534?profile=original

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • I have a TF Mini from Amazon.  I have the sketch running but my range outdoors is at best 6 feet.

    And that was over grass. Concrete is more like 3 feet.

    Signal strength is also very low. About 35 and below.

    Are there any other good threads on this Lidar unit.

    I am hoping to hook it up to a DF mp3 player to announce altitude during the last 15feet before touchdown...

     https://www.amazon.com/gp/product/B075V5TZRY/ref=oh_aui_detailpage_...

    Robot Check
  • Thank you David.

    Yes, TFmini LiDAR works well with Pixhawk and Arduino.

    Would you please leave your e-mail address. Or you can contact me huaxiaocheng@benewake.com at ease. 

    I will send you the instrume data.



    David said:

    Hi, you have done a very good work, with arduino one its also possible wire the tf mini lidar?
    Where i can find your. zip library so i can try with my sensor
  • Is it also compatible with pixhawk 2.4.6,?
  • Hi, you have done a very good work, with arduino one its also possible wire the tf mini lidar?
    Where i can find your. zip library so i can try with my sensor
This reply was deleted.

Activity

DIY Robocars via Twitter
23 hours ago
DIY Robocars via Twitter
RT @_JonMyer: 🚨Attention DeepRacer's including #UndergroundDeepRacer🚨 Check our our LIVE stream that including @IAM_dbro Take a few moments…
Aug 5
DIY Drones via Twitter
RT @MarvelmindMaxim: Extreme precision for 60 swarming robots. #marvelmind #autonomousrobotics #robotics #swarmrobotics #rtls #ips #indoor…
Aug 3
DIY Drones via Twitter
RT @MarvelmindMaxim: Precise (±2cm) tracking for racing boats and autonomous boats. Works outdoor and indoor. #autonomous #AutonomousVehic…
Aug 3
DIY Drones via Twitter
RT @MarvelmindMaxim: Helping PixHawk folks to fly autonomous quadcopters using PX4 and ArduPilot. https://marvelmind.com/drones/ Equally suitab…
Aug 3
DIY Robocars via Twitter
RT @chr1sa: The @DIYRobocars @donkey_car virtual AI car race is starting in 15 minutes! Watch it live on Twitch https://www.twitch.tv/mossmann3333 htt…
Aug 1
DIY Robocars via Twitter
RT @chr1sa: Don't miss our monthly @DIYRobocars @donkey_car virtual AI car race tomorrow at 10:00am PT live on Twitch. Head-to-head racing…
Jul 31
DIY Robocars via Twitter
RT @sparkfun: Our completed tutorial on building an @NVIDIA Jetson Nano-powered @Sphero RVR gets your bot up and running via teleoperation…
Jul 30
DIY Robocars via Twitter
RT @SmallpixelCar: Freeway test https://t.co/4V5tV9lhIP
Jul 29
DIY Robocars via Twitter
Very small autonomous cars racing, thanks to an overhead camera: https://control.ee.ethz.ch/research/team-projects/autonomous-rc-car-racing.html
Jul 29
DIY Robocars via Twitter
Jul 29
DIY Robocars via Twitter
Jul 29
DIY Robocars via Twitter
RT @chr1sa: Don't miss our virtual AI car race this Saturday! Real developers + virtual cars =🏎️🏎️🏎️ Head-to-head battles with thrills, sp…
Jul 28
DIY Robocars via Twitter
Jul 27
DIY Robocars via Twitter
RT @usashirou1: Jetson nano by Isaac Kaya #jetson https://t.co/Mu1N0CyQkN
Jul 23
DIY Robocars via Twitter
RT @GPUsolution: JetRacer mady by Iflytek company #JetsonNANO #Nvidia https://t.co/MimTymIwge
Jul 23
More…