Benewake TFmini - Inexpensive LiDAR With Teensy 3.5

Introduction: Benewake TFmini - Inexpensive LiDAR With Teensy 3.5

The Benewake TFmini LiDAR unit is a small, very light weight LiDAR sensor for about $50 Canadian. Documentation was good, but incomplete. It provided details on receiving data from the sensor, but forgot to mention the signal needed to put the sensor into the default mode so that it actually sends the data. Luckily that was in the debugging document.

So this is what worked for me, and it's really a easy device to work with.

I chose to use a Teensy 3.5 as it has multiple HW Serial ports, it's more than fast enough to receive data and process it without letting data pile up. Just for fun I used the Teensy Threading library to separate getting the data from the rest of the code.

Step 1: Connecting TFmini to Teensy 3.5 (similar for Arduino Mega)

This example requires two serial connections: one to the TFmini, and one to display results on your computer. For this reason, and as far as I can tell, this reason only, this particular example won't work on anything below an Arduino Mega or Teensy 3.x.

That being said, for applications not requiring serial output to print to computer screen, the same project should be adaptable.

Using included wire harness:

1) connect black wire to Teensy GND (if using difference VDC source, ensure ground also goes to GND on Teensy)

2) connect red wire to Teensy Vin (or 5VDC source)

3) connect white wire (TFmini RX) to pin 1 on Teensy (Serial1 TX)

4) connect green wire (TFmini TX) to pin 0 on Teensy (Serial RX)

The included wire harness was too small for me to work with on a bread board, so I cut off the end opposite the TFmini, and soldered the wires to a breadboard, add a JST connector to the breakboard, and made a JST to male jumper wire harness.

Step 2: Code to Run It

Use the following code (for Teensy 3.5) or download the attached file:

For Arduino Mega, threading likely won't work. Move code from readLiDAR function to main loop, and remove anything related to threading.

#include <arduino.h><br>#include "TeensyThreads.h"
// Using supplied cable:
// - Black = GND (connected to GND)
// - Red = 5V (4.5 - 6.0V) (connected to Vin on Teensy 3.5, or 5V on Arduino)
// - White = TFmini RX (aka. connect to microcontroller TX, pin1 on Teensy 3.5)
// - Green = TFmini TX (aka. connect to microcontroller RX, pin0 on Teensy 3.5)
// NOTE: for this sketch you need a microcontroller with additional serial ports beyond the one connected to the USB cable
// This includes Arduino MEGA (use Serial1), Teensy (3.x) (use one of the available HW Serial connections)
<br>
volatile int liDARval = 0;
<br>
void readLiDAR(){
  // Data Format for Benewake TFmini
  // ===============================
  // 9 bytes total per message:
  // 1) 0x59
  // 2) 0x59
  // 3) Dist_L (low 8bit)
  // 4) Dist_H (high 8bit)
  // 5) Strength_L (low 8bit)
  // 6) Strength_H (high 8bit)
  // 7) Reserved bytes
  // 8) Original signal quality degree
  // 9) Checksum parity bit (low 8bit), Checksum = Byte1 + Byte2 +...+Byte8. This is only a low 8bit though
  while(1){ // Keep going for ever
    while(Serial1.available()>=9) // When at least 9 bytes of data available (expected number of bytes for 1 signal), then read
    {
      if((0x59 == Serial1.read()) && (0x59 == Serial1.read())) // byte 1 and byte 2
      {
        unsigned int t1 = Serial1.read(); // byte 3 = Dist_L
        unsigned int t2 = Serial1.read(); // byte 4 = Dist_H
        t2 <<= 8;
        t2 += t1;
        liDARval = t2;
        t1 = Serial1.read(); // byte 5 = Strength_L
        t2 = Serial1.read(); // byte 6 = Strength_H
        t2 <<= 8;
        t2 += t1;
        for(int i=0; i<3; i++)Serial1.read(); // byte 7, 8, 9 are ignored
      }
    }
  }
}
void setup()
{
    Serial1.begin(115200); // HW Serial for TFmini
    Serial.begin(115200); // Serial output through USB to computer
    delay (100); // Give a little time for things to start
    // Set to Standard Output mode
    Serial1.write(0x42);
    Serial1.write(0x57);
    Serial1.write(0x02);
    Serial1.write(0x00);
    Serial1.write(0x00);
    Serial1.write(0x00);
    Serial1.write(0x01);
    Serial1.write(0x06);
    // Setup thread for reading serial input from TFmini
    threads.addThread(readLiDAR);
}
void loop()
{
  delay(10);  // Don't want to read too often as TFmini samples at 100Hz
  Serial.println(liDARval);
}

Step 3: Using Arduino IDE View Results in Serial Plotter

You can use whatever method you'd like, but Arduino's IDE will plot the results nicely.

Connect to the Teensy, and open Serial Monitor. Ensure Baudrate is set to 115200.

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest

    2 Discussions

    1.Can I use this TFmini with more than 1 sensor??

    2.Is this code correct?? I use pin 0,1 (Serial 1) and pin 9,10 (Serial 2)

    3.And can teensy 3.5 handle 4 TFmini with 4 3.3V vibration motors??

    note : I've never used teensy board...

    Here is my code for 2 TFmini pl.It's your code, i just copied and changed a little bit...

    #include

    #include "TeensyThreads.h"

    volatile int liDARval = 0;

    volatile int liDARval2 = 0;

    void readLiDAR(){

    while(1){

    while(Serial1.available()>=9)

    {

    if((0x59 == Serial1.read()) && (0x59 == Serial1.read()))

    {

    unsigned int t1 = Serial1.read();

    unsigned int t2 = Serial1.read();

    t2 <<= 8;

    t2 += t1;

    liDARval = t2;

    t1 = Serial1.read();

    t2 = Serial1.read();

    t2 <<= 8;

    t2 += t1;

    for(int i=0; i<3; i++)Serial1.read();

    }

    }

    while(Serial2.available()>=9)

    {

    if((0x59 == Serial2.read()) && (0x59 == Serial2.read()))

    {

    unsigned int t3 = Serial2.read();

    unsigned int t4 = Serial2.read();

    t4 <<= 8;

    t4 += t3;

    liDARval2 = t4;

    t3 = Serial2.read();

    t4 = Serial2.read();

    t4 <<= 8;

    t4 += t3;

    for(int i=0; i<3; i++)Serial2.read();

    }

    }

    }

    }

    void setup()

    {

    Serial2.begin(115200);

    Serial1.begin(115200);

    Serial.begin(115200);

    delay (100);

    Serial1.write(0x42);

    Serial1.write(0x57);

    Serial1.write(0x02);

    Serial1.write(0x00);

    Serial1.write(0x00);

    Serial1.write(0x00);

    Serial1.write(0x01);

    Serial1.write(0x06);

    Serial2.write(0x42);

    Serial2.write(0x57);

    Serial2.write(0x02);

    Serial2.write(0x00);

    Serial2.write(0x00);

    Serial2.write(0x00);

    Serial2.write(0x01);

    Serial2.write(0x06);

    threads.addThread(readLiDAR);

    }

    void loop()

    {

    delay(10);

    Serial.println(liDARval);

    Serial.println(liDARval2);

    }

    This TFmini connected with Teensy 3.5 steps are very clear and easy to understand. Very helpful for build own application. Great job !

    Let's check the TFmini LiDAR real performance video from manufacturer Benewake. : )

    http://www.benewake.com/en/video/show-171.html