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:
-
connect black wire to Teensy GND (if using difference VDC source, ensure ground also goes to GND on Teensy)
-
connect red wire to Teensy Vin (or 5VDC source)
-
connect white wire (TFmini RX) to pin 1 on Teensy (Serial1 TX)
-
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>
#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)
volatile int liDARval = 0;
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);
}
TFmini.inoTFmini.inoDownload
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.