Skip to Content

TFmini-Plus Distance Sensor with Arduino

TFmini-Plus Distance Sensor with Arduino

In this tutorial you will learn how to use the TFmini Plus Laser Range Distance Sensor with an Arduino to measure distances.

The TFmini Plus is a small LiDAR/Proximity sensor that can measure distances of up to 12 meters. It uses infrared laser light and the Time-of-Flight (ToF) principle. By measuring the time it takes for the light to be reflected from an objects, it can compute distances with high accuracy and speed.

In this guide, we’ll cover the setup, wiring, and coding needed to get your TFmini Plus up and running with Arduino.

Required Parts

You will need an TFmini Plus Sensor (listed below). As for the microcontroller, I used an Arduino Uno for this project, but any other Arduino will work as well. We also will use an OLED to show the distances and other information returned by the TFmini Plus on a little display.

TFmini Plus Distance Sensor

Arduino

Arduino Uno

USB Data Sync cable Arduino

USB Cable for Arduino UNO

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

OLED display

OLED Display

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

Features of the TFmini Plus

The TFmini Plus is an infrared laser distance sensor that uses the ToF (time of flight) principle to measure distances. The sensor emits Infrared light pulses on a periodic basis, which will be reflected when contacting an object. The time of flight is measured by computing the round-trip phase difference Δφ:

ToF Principle
ToF Principle (source)

The TFmini Plus can measure distances from 0.1 to 12 meter with an accuracy of ±5cm for 0.1-6m and ±1% for the 6m-12m range. The blind zone of the sensor is 10 cm. The resolution is 5mm and the measurement speed (frame rate) is adjustable between 1 and 1000 Hz.

The detection range of the TFmini Plus also depends on the reflectivity and the size of the object to detect. The following diagram illustrates this:

Distance measurement characteristics of TFmini Plus
Distance measurement characteristics of TFmini Plus (source)

① Detection blind zone of 0-10cm, within which the output data is unreliable.
② Operating range for detecting a black target with 10% reflectivity, 0.1-5m.
③ Operating range for detecting white target with 90% reflectivity, 0.1-12m.

Note surprisingly, the more reflective and the larger the object the greater is the range it can be detected within. For more details see the datasheet and product manual for the TFmini Plus linked below:

Pinout of the TFmini Plus

The TFmini Plus can communicate via UART, I2C or IO, depending on the software configuration. The following picture shows the pinout of the TFmini Plus:

Pinout of TFmini Plus
Pinout of TFmini Plus

The TFmini Plus is IP65 rated and therefore uses a sealed cable with a GH1.25-4P connector at the end. Luckily, it usually comes with a Dupont cable adapter that allows you to connect it easily to an Arduino or a breadboard. Here is a table with the connector pins, their wire colors and functions:

Pin NumberWire ColorFunction
1Red+5V
2WhiteRX/SDA
3Green/BlueTX/SCL/IO
4BlackGround

Pin 1 is ground (GND) and pin 2 is the supply voltage of 5V±0.5V. The average current is ≤110mA and the peak current is up to 500mA. Pin 3 and 4 operate either as RX and TX for UART, or as SDA and SCL for I2C. Note that the TX/SCL wire can be green or a blue. My model of the TFmini Plus has a green wire for TX/SCL.

TFmini-S vs TFmini Plus

There is a TFmini, a TFmini-S and the TFmini Plus. So it can get a bit confusing. The TFmini is not produced anymore and the TFmini-S is an upgraded version of the TFmini. The TFmini-Plus is a further upgrade, with lower power consumption, better housing and better performance under strong ambient light (e.g. outdoors). The following table compares the main features of the TFmini-S and the TFmini Plus:

Comparison TFmini-S vs TFmini Plus
Comparison TFmini-S vs TFmini Plus (source)

In this tutorial we are going to use the TFmini Plus. But you could also use the TFmini-S, since software and wiring are essentially identical.

Connecting the TFmini Plus to Arduino

We are going to use the UART interface of the TFmini Plus. But first let’s connect the power supply.

Connect the black wire (pin 4) of the TFmini Plus to GND of the Arduino. Then, connect the red wire (pin 1 of the TFmini Plus to the 5V output of the Arduino.

For UART, connect the white wire (TX, pin 2) of the TFmini Plus to pin 3 of the Arduino. Finally, connect the green wire (RX, pin 3) of the TFmini Plus to pin 3 of the Arduino. See the complete wiring diagram below:

Connecting TFmini Plus to Arduino
Connecting TFmini Plus to Arduino

Note that the TX and RX pins for the TFmini and the pins for TX and RX used by the Arduino are reversed because that is the way serial communication needs to be wired. Don’t get confused by this.

TX and RX wiring
TX and RX wiring

In the next section we are going to write some simple code to test the sensor.

Code for measuring distances with TFmini Plus

To measure distances with the TFmini Plus, it is best to use a library. I used the TFMPlus library by Bud Ryerson, which works for the TFmini Plus and the TFmini-S. To install it, open the Library Manager, search for “tfmini”, find the TFMPlus library by Bud Ryerson and install it by pressing the “INSTALL” button.

With the library installed, let’s try the sensor. The following code reads distances measured by the TFmini Plus sensor and prints them to the Serial monitor.

#include "TFMPlus.h" 
#include "SoftwareSerial.h"

TFMPlus tfmini;       
SoftwareSerial TFSerial(2, 3);

void setup() {
  Serial.begin(9600);
  TFSerial.begin(115200);
  tfmini.begin(&TFSerial);
}

void loop() {
  int16_t dist = 0;
  int16_t strength = 0;
  int16_t temp = 0;

  if (tfmini.getData(dist, strength, temp)) {
    Serial.print("Distance ");
    Serial.print(dist);
    Serial.println(" cm");
    delay(200);
  }
}

Let’s break down the code to understand its components in detail.

Library Inclusions

We start by including the necessary libraries for our project. The TFMPlus.h library is used to communicate with the TFmini Plus sensor, while the SoftwareSerial.h library allows us to create a serial communication on other digital pins than the default hardware pins.

#include "TFMPlus.h" 
#include "SoftwareSerial.h"

Object Creation

Next, we create instances of the required classes. The TFMPlus object tfmini will be used to interact with the TFmini Plus sensor. We also create a SoftwareSerial object named TFSerial that will communicate with the sensor using the digital pins 2 (RX) and 3 (TX).

TFMPlus tfmini;       
SoftwareSerial TFSerial(2, 3);

Setup Function

In the setup() function, we initialize the serial communication. The Serial.begin(9600) command sets up the default serial port at a baud rate of 9600 bps for printing to the Serial Monitor. The TFSerial.begin(115200) command initializes the software serial port at a higher baud rate of 115200 bps, which is required by the TFmini Plus sensor. Finally, we call tfmini.begin(&TFSerial) to initialize the sensor with the software serial port.

void setup() {
  Serial.begin(9600);
  TFSerial.begin(115200);
  tfmini.begin(&TFSerial);
}

Loop Function

In the loop() function, we declare three integer variables: dist, strength, and temp. These will store the distance measurement, signal strength, and temperature data from the sensor, respectively.

We then check if the sensor successfully retrieves data using the tfmini.getData(dist, strength, temp) method. If the data is available, we print the distance to the Serial Monitor using Serial.print(). The distance is displayed in centimeters. After printing the data, we introduce a delay of 200 milliseconds before the next reading.

void loop() {
  int16_t dist = 0;
  int16_t strength = 0;
  int16_t temp = 0;

  if (tfmini.getData(dist, strength, temp)) {
    Serial.print("Distance ");
    Serial.print(dist);
    Serial.println(" cm");
    delay(200);
  }
}

In this example we print only the distance, but you could also print the signal strength, and temperature data and we will do this when connecting the OLED. For now, we just print the distance and you should see it printed in centimeters on the Serial Monitor:

Distances measured with TFmini Plus printed on Serial Monitor
Distances measured with TFmini Plus printed on Serial Monitor

In the next section we are going to add an OLED to our circuit.

Adding an OLED to display TFmini Plus distance data

Adding OLED an is straightforward. Simply connect SDA and SCL of the OLED to the corresponding pins of the Arduino. As for the power supply: since the OLED can run on 5V, we can share the power supply lines. Connect VCC to 5V and GND to GND. The picture below shows the complete wiring:

Connecting OLED and TFmini Plus with Arduino
Connecting OLED and TFmini Plus with Arduino

Note that the TFmini Plus will not work with 3.3V.

Code to display distances measured by TFmini Plus on OLED

The following code reads distance, strength and temperature measurements from the TFmini Plus sensor and displays them on the OLED. Have a quick look at the complete code first, and then we will discuss its details.

#include "TFMPlus.h" 
#include "SoftwareSerial.h"
#include "Adafruit_SSD1306.h"

TFMPlus tfmini;       
SoftwareSerial TFSerial(2, 3);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

void oled_init() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  oled.setTextColor(WHITE);
  
}

void display() {
  static char text[30];
  static int16_t dist, strength, temp;

  if (tfmini.getData(dist, strength, temp)) {
    oled.clearDisplay();

    sprintf(text, " %d cm ", dist);
    oled.setTextSize(2);
    oled.setCursor(20, 15);
    oled.print(text);

    sprintf(text, " %d ", strength);
    oled.setTextSize(1);
    oled.setCursor(50, 40);
    oled.print(text);

    sprintf(text, " %d C ", temp);
    oled.setTextSize(1);
    oled.setCursor(50, 50);
    oled.print(text);    

    oled.display();
  }
}

void setup() {
  TFSerial.begin(115200);
  tfmini.begin(&TFSerial);
  oled_init();
}

void loop() {
  display();
  delay(20);
}

Libraries and Display Initialization

We start by including the TFMPlus.h library to communicate with theTFmini Plus sensor. SoftwareSerial.h library is used to create the serial communication and the Adafruit_SSD1306 Library is used for the OLED display.

We then create the tfmini sensor object, the TFSerial object, and the oled object for the diplay:

#include "TFMPlus.h" 
#include "SoftwareSerial.h"
#include "Adafruit_SSD1306.h"

TFMPlus tfmini;       
SoftwareSerial TFSerial(2, 3);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

If you haven’t already installed the Adafruit_SSD1306 Library, you will have to. Just install it via the Library Manager as usual:

Adafruit_SSD1306 library installed in Library Manager
Adafruit_SSD1306 library installed in Library Manager

oled_init function

The oled_init() function initializes the display, clears it, sets the text size, and color for the text.

void oled_init() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
}

Note that the I2C address for the OLED display is set to 0x3C in oled.begin(). Most of these small OLEDs use this address (or 0x27) but yours might be different. If you don’t see anything on the OLED, it most likely has a different I2C address and you have to change the address.

If you don’t know the I2C address have a look at the How to Interface the SSD1306 I2C OLED Graphic Display With Arduino tutorial to find it. Also the Use SSD1306 I2C OLED Display With Arduino tutorial will tell you more about how to use an OLED.

display function

The display() function calls tfmini.getData to get the measured distance, strength and temperature values from the TFmini Plus sensor. It then clears the display and prints the different values with different font sizes. The sprintf() function is used to convert the values into a formatted text.

void display() {
  static char text[30];
  static int16_t dist, strength, temp;

  if (tfmini.getData(dist, strength, temp)) {
    oled.clearDisplay();

    sprintf(text, " %d cm ", dist);
    oled.setTextSize(2);
    oled.setCursor(20, 15);
    oled.print(text);

    sprintf(text, " %d ", strength);
    oled.setTextSize(1);
    oled.setCursor(50, 40);
    oled.print(text);

    sprintf(text, " %d C ", temp);
    oled.setTextSize(1);
    oled.setCursor(50, 50);
    oled.print(text);    

    oled.display();
  }
}

If you put a object in front of the sensor, you should see distance, strength and temperature displayed on the OLED:

Distance, Strength and Temperature shown on OLED
Distance, Strength and Temperature shown on OLED

The distance is shown in centimeters. If no object could be detected a value of 0 will be displayed.

The signal strength (0-65535) indicates how much of the infrared signal is reflected from the detected object. When the signal strength is lower than 100 or equal to 65535, the detection is unreliable, TFmini Plus will set the distance value to 0.

Finally, the internal chip temperature of TFmini Plus is displayed in degree centigrades. It is normal for the sensor to become quite warm after a while (70C).

setup function

In the setup() function we first call TFSerial.begin() to initialize the serial UART communication, then call tfmini.begin() to initialize the sensor, and finally call oled_init() to initialize the OLED.

void setup() {
  TFSerial.begin(115200);
  tfmini.begin(&TFSerial);
  oled_init();
}

loop function

The loop() function simply calls the display() function every 20 msec.

void loop() {
  display();
  delay(20);
}

And that’s it. With this code and the TFmini Plus you can measure distances of up to 12 meters.

Conclusions

In this tutorial you learned how to use the TFmini Plus Distance Sensor with an Arduino to measure distances and to display them on an OLED.

The TFmini-Plus is very similar to the TFmini-S. Both run on 5V, can measure distance of up to 12 meters, and can be controlled by the same software. However, the TFmini-Plus comes in an IP65 housing, works better outdoors and is therefore more expensive. If you don’t need the full range a cheaper alternative to both sensors is the TF-Luna, which is smaller and has a range of 8 meters.

If you need millimeter resolution have a look at the VL53L0X, the VL53L1X, or the TOF10120. They also have the advantages that they can operate with 3.3V. However, their ranges are much shorter.

Other common infrared distance sensors such as the GP2Y0A710K0F or the GP2Y0A21YK0F are similar in size but use triangulation to determine distance based on the angle of the reflected IR light. They are even cheaper but also have an even shorter range.

If you have any questions feel free to leave them in the comment section.

Happy Tinkering ; )