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
RT @chr1sa: Donkeycar 4.4 released with tons of new features, including path learning (useful with GPS outdoors), better Web and Lidar supp…
Sunday
DIY Robocars via Twitter
RT @NXP: We are already biting our nails in anticipation of the #NXPCupEMEA challenge! 😉 Did you know there are great cash prizes to be won…
Friday
DIY Robocars via Twitter
RT @gclue_akira: レースまであと3日。今回のコースは激ムズかも。あと一歩 #jetracer https://t.co/GKcEjImQ3t
Friday
DIY Robocars via Twitter
UC Berkeley's DIY robocar program https://roar.berkeley.edu/
Friday
DIY Robocars via Twitter
RT @chr1sa: The next @DIYRobocars autonomous car race at @circuitlaunch will be on Sat, Dec 10. Thrills, spills and a Brazilian BBQ. Fun…
Friday
DIY Robocars via Twitter
RT @arthiak_tc: Donkey car platform ... Still training uses behavioral cloning #TCXpo #diyrobocar @OttawaAVGroup https://t.co/PHBYwlFlnE
Nov 20
DIY Robocars via Twitter
RT @emurmur77: Points for style. @donkeycar racing in @diyrobocars at @UCSDJacobs thanks @chr1sa for taking the video. https://t.co/Y2hMyj1…
Nov 20
DIY Robocars via Twitter
RT @SmallpixelCar: Going to @diyrobocars race at @UCSDJacobs https://t.co/Rrf9vDJ8TJ
Nov 8
DIY Robocars via Twitter
RT @SmallpixelCar: Race @diyrobocars at @UCSDJacobs thanks @chr1sa for taking the video. https://t.co/kK686Hb9Ej
Nov 8
DIY Robocars via Twitter
RT @PiWarsRobotics: Presenting: the Hacky Racers Robotic Racing Series in collaboration with #PiWars. Find out more and register your inter…
Oct 23
DIY Robocars via Twitter
RT @Hacky_Racers: There will be three classes at this event: A4, A2, and Hacky Racer! A4 and A2 are based around UK paper sizing and existi…
Oct 23
DIY Robocars via Twitter
Oct 23
DIY Robocars via Twitter
Oct 19
DIY Robocars via Twitter
Oct 18
DIY Robocars via Twitter
RT @NeaveEng: Calling all UK based folks interested in @diyrobocars, @f1tenth, @donkey_car, and similar robot racing competitions! @hacky_r…
Oct 13
DIY Robocars via Twitter
RT @araffin2: 🏎️ After hours of video editing, I'm happy to share a best of my Twitch videos on learning to race with RL. 🏎️ Each part is…
Oct 13
More…