IOT

How to use TFMini-S LiDAR Distance Sensor with Arduino

This article will show you how to connect an Arduino to a TFMini-S Micro LiDAR Distance Sensor. This tutorial also includes a quick overview of LiDAR technology, including how it works and what applications it can be used for. Light detection and ranging, or LiDAR, is a popular remote sensing technique for determining an object’s exact distance on the earth’s surface. You may have noticed that the latest iPhones and Samsung phones include a LiDAR sensor on the back to improve camera performance.

Since LiDAR has a wide range of uses, we’ll look at the TFMini-S Micro LiDAR Distance Sensor by Benewake. This LiDAR sensor has a distance measurement range of up to 12 meters. 

First, we’ll go over how to connect the TFMini-S LiDAR Sensor to an Arduino. The distance and Signal Strength can be estimated using the TFMini library and a simple example. To display the measured distance on an LCD screen, we can utilize a display unit such as a 16X2 LCD Display.

Components Required

  • Arduino Nano Board Atmega328
  • TFMini-S Micro LiDAR Distance Sensor
  • JHD162A 16X2 LCD Display
  • Potentiometer-10k
  • Jumper Wires
  • Breadboard

What is LiDAR & How does it work?

Light detection and ranging, or LiDAR, is a popular remote sensing technique for determining an object’s exact distance on the earth’s surface. LiDAR calculates an object’s varying distances from the earth’s surface using a pulsed laser.

It can be used to map regional structures, such as vegetation height, density, and other properties. A liDAR is an effective tool for scientists investigating vegetation over wide areas since it directly measures the height and density of vegetation on the ground.

How do LiDAR works?

LiDAR Working

LiDAR works on the basis of a simple principle: shoot laser light at an object on the ground and calculate how long it takes for the light to return to the LiDAR source. Given the speed of light (about 186,000 miles per second), the process of measuring the exact distance using LiDAR looks to be extremely quick. It is, however, extremely technical. Analysts use the following calculation to determine the exact distance of an object:

Distance =(Speed of Light x Time of Flight)/ 2

TFMini-S Micro LiDAR Distance Sensor

TFMini-S Micro LiDAR Distance Sensor

Benewake’s TFMini-S Micro LiDAR Distance Sensor comes in attractive packaging. Benewake is a Chinese firm that specialises in solid-state LIDAR sensors and solutions. Micro Single-point Ranging Module is another name for the TFMini-S Micro LiDAR Sensor.

TFMini-S LiDAR Sensor

The TFMini-S is a more advanced version of the TFmini. The optical, structural, and electronic designs of the TFMini-S are all one-of-a-kind. Low cost, small volume, and low power consumption are all advantages of the TFmini-S. It also includes a variety of interfaces to satisfy a variety of needs.

UART and I2C interfaces are available on the board. A microcontroller, such as the STM32 Series controller or something similar, is used on the board. The LiDAR data can be retrieved by attaching the sensor to any external microcontroller. It features four pins for power and four pins for UART. The VCC is the RED wire, and the Ground is the BLACK wire. The RX colour wire is white, whereas the TX colour wire is green.

TFMini-S LiDAR Sensor Pinout

The blind zone has been reduced from 30cm to 10cm, and outdoor performance and reflectivity accuracy have been increased. Ambient light has no effect on the distance range, allowing it to be constant with the indoor range and improving accuracy. At 10% reflectivity, the error performance approaches that of a 90% reflectivity background. It has the ability to detect in a stable, accurate, sensitive, and high-frequency range.

Specifications

  • Operating Range is 0.1 ~ 12m
  • Supply Voltage – 5V
  • Frame Rate is 10 – 1000Hz
  • Operating Temperature is 0℃~60℃
  • Communication Interface – UART
  • Accuracy – ±6cm@(0.1-6m), ±1%@(6m-12m)
  • FOV – 2°

Applications

  • Unidirectional ranging LiDAR.
  • Obstacle avoidance.
  • Assisted landing.
  • Terrain following.
  • Robots outdoor obstacle avoidance.
  • Intelligent parking. Crane operations.
  • Vehicle position sensing.
  • Vehicle detection for barrier gate control.
  • High-precision & High-speed measurement.
  • Eye-safe infrared LED light.
  • Compact size, lightweight.
  • Good performance for height fixing and terrain-following drones.

Interfacing TFMini-S Micro LiDAR Distance Sensor with Arduino

Let’s connect an Arduino to a TFMini-S Micro LiDAR Distance Sensor. You can use either a UART or an I2C interface to connect to the sensor. However, I prefer the UART interface. TFmini LiDAR Arduino Connection

Connect the TFMini Sensor’s VCC and GND Pins to the Arduino’s 5V and GND. Connect the Tx and RX to Arduino’s D2 and D3 in the same way. Any other pins that allow Software Serial can be used for Tx and Rx.

Interfacing TFMini-S Micro LiDAR Distance Sensor with Arduino

Source Code/Program with Library

There is a dedicated library for Benewake TFMini LiDAR Sensor. Check the Github repository for Arduino TFMini Library.

I initially posted the code using the TFMini Library’s example code. The code was uploaded, but the Sensor was unable to check the distance, resulting in an error message.

Then, based on some Github links, I added some lines to the Code that explained why the sensor doesn’t work with the default library. This time the sensor worked well. Here’s how the code has been changed.

include <SoftwareSerial.h>
#include “TFMini.h”
TFMini tfmini;

SoftwareSerial SerialTFMini(2, 3); //The only value that matters here is the first one, 2, Rx

void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (SerialTFMini.available())
{
rx[i] = SerialTFMini.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}


void setup()
{
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.println (“Initializing…”);
SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor
}

void loop()
{
int distance = 0;
int strength = 0;

getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.print(distance);
Serial.print(“cm\t”);
Serial.print(“strength: “);
Serial.println(strength);
}
}
delay(100);
}

The Signal Strength and the distance to my ceiling were printed on the Serial monitor. I moved my hand closer and farther away from the sensor to see how the distance changed. The blind zone has been reduced from 30 to 10 centimeters. As a result, it will only detect something once this distance has been reached.

Source Code/Program without Library

Check out this code if you wish to use the TFMini-S LiDAR Distance Sensor with Arduino and without a library. There’s no need to put any libraries in the Library folder. All of the parameters for retrieving data via the UART interface are discussed and defined in the code.

#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(2, 3); //define software serial port name as Serial1 and define pin2 as RX & pin3 as TX

int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER = 0x59; //frame header of data package


void setup()
{
Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer
Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
}


void loop() {
if (Serial1.available()) //check if serial port has data input
{
if (Serial1.read() == HEADER) //assess data package frame header 0x59
{
uart[0] = HEADER;
if (Serial1.read() == HEADER) //assess data package frame header 0x59
{
uart[1] = HEADER;
for (i = 2; i < 9; i++) //save data in 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)) //verify the received data as per protocol
{
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 measure distance value of LiDAR
Serial.print(‘\t’);
Serial.print(“strength = “);
Serial.print(strength); //output signal strength value
Serial.print(‘\n’);
}
}
}
}
}

With this code, I re-tested the sensor’s performance. For me, the code worked perfectly.

Using TFMini-S LiDAR Distance Sensor with Arduino & LCD Display

Now I need to go outside and test the TFMini sensor. To do so, I’ll need to hook up any display. I’d rather use a 16×2 LCD display.

TFMini LCD Arduino

So, on a breadboard, I attached a 16×2 LCD Display to the previous circuit using the circuit schematic. The LCD Contrast was adjusted with a 10K Potentiometer.

Now, this device is portable and we can take it outside for testing applications.

Source Code/Program

To the prior code, I added various LCD codes, such as adding an LCD library and defining the LCD pins. The measured distance was then printed on LCD. The code was then posted, and it worked perfectly.



#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#include <SoftwareSerial.h>
#include “TFMini.h”
TFMini tfmini;

SoftwareSerial SerialTFMini(2, 3); //The only value that matters here is the first one, 2, Rx

void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (SerialTFMini.available())
{
rx[i] = SerialTFMini.read();
if (rx[0] != 0x59)
{
i = 0;
}
else if (i == 1 && rx[1] != 0x59)
{
i = 0;
}
else if (i == 8)
{
for (j = 0; j < 8; j++)
{
checksum += rx[j];
}
if (rx[8] == (checksum % 256))
{
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
}
else
{
i++;
}
}
}


void setup()
{
lcd.begin(16, 2);
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.println (“Initializing…”);
SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor
}

void loop()
{
int distance = 0;
int strength = 0;

getTFminiData(&distance, &strength);
while (!distance)
{
getTFminiData(&distance, &strength);
if (distance)
{
Serial.print(distance);
Serial.print(“cm\t”);
Serial.print(“strength: “);
Serial.println(strength);

lcd.setCursor(0, 0);
lcd.print(“Dis: “);
lcd.print(distance);
lcd.print(” cm”);

lcd.setCursor(0, 1);
lcd.print(“Str: “);
lcd.print(strength);
}
}
delay(500);
lcd.clear();
}

To test the sensor, I used my own balcony. To check the distance fluctuation, there were numerous barriers at various distances. To face different impediments, I moved the sensor in different directions. And I discovered that the sensor identified a distant object and used reflection to calculate the distance.

The sensor can measure the maximum distance up to 1200 centimeters. According to the datasheet, the accuracy is around 90%. So we can use this sensor as a demo or for basic applications.

Conclusion

I hope all of you understand How to use TFMini-S LiDAR Distance Sensor with Arduino. We MATHA ELECTRONICS will be back soon with more informative blogs soon.

Leave a Reply

Your email address will not be published.