In this tutorial you will learn how to use the TFmini-S Laser Range Distance Sensor with an Arduino to measure distances.
The TFmini-S 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-S up and running with Arduino.
Required Parts
You will need an TFmini-S 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 measured by the TFmini-S on a little display.
TFmini-S Distance Sensor
Arduino Uno
USB Cable for Arduino UNO
Dupont Wire Set
Breadboard
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-S
The TFmini-S 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 Δφ:
The TFmini-S can measure distances from 0.1 to 12 meter with an accuracy of ±6cm for 0.1-6m and ±1% for the 6m-12m range. The blind zone of the sensor is 10 cm. The resolution is 1 cm and the measurement speed (frame rate) is adjustable between 1 and 1000 Hz.
Range and accuracy will depend on the reflectivity of the object light conditions. Generally the outdoor range is much shorter than the indoor range. For more details see the product manual and datasheet for the TFmini-S linked below:
Pinout of TFmini-S
The TFmini-S can communicate via UART or I2C, depending on the software configuration. We are going to use the sensor with UART. The following picture shows the pinout of the TFmini-S with its UART/I2C interface and power supply pins.
Pin 3 and 4 operate either as RX and TX for UART, or as SDA and SCL for I2C. Pin 1 is ground (GND) and pin 2 is the supply voltage of 5V±0.1V. The average current ≤140mA with a peak current of 200mA.
TFmini-S vs TFmini
On a side note: The TFmini-S is an improved version of the TFmini. They look very similar but their performance features are different. See the comparison below:
If you buy a TFmini make sure it is the TFmini-S and not the older TFmini version, since the TFMini-Plus Library we are going to use is not compatible with the TFmini (but with the TFmini-S and the TFmini Plus).
Connecting the TFmini-S to Arduino
As mentioned we are going to use the UART interface of the TFmini-S. But first let’s connect the power supply.
Connect the black wire (pin 1) of the TFmini-S to GND of the Arduino. Then, connect the red wire (pin 2) of the TFmini-S to the 5V output of the Arduino.
For UART, connect the white wire (TX, pin 3) of the TFmini-S to pin 3 of the Arduino. Finally, connect the green wire (RX, pin 4) of the TFmini-S to pin 2 of the Arduino. See the complete wiring diagram below:
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.
In the next section we are going to write some simple code to test the sensor.
Code for measuring distances with TFmini-S
To measure distances with the TFmini-S, it is best to use a library. I tried several ones and liked the TFMPlus library by Bud Ryerson. Despite its name, this library works with the TFmini-S and the TFmini-Plus. 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 out. The following code reads distances measured by the TFmini-S 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-S 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-S 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-S 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 print the signal strength, and temperature data as well. The signal strength indicates how much of the infrared signal is reflected from the detected object and the temperature is the internal temperature of the sensor, which is used for temperature compensation.
If you upload and run the code you should see the measured distances in centimeters printed on the Serial Monitor:
In the next section we are going to add an OLED to our circuit and display the distances on that, instead of printing to the Serial Monitor.
Adding an OLED to display TFmini-S 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:
Note that the TFmini-S will not work with 3.3V.
Code to display distances measured by TFmini-S on OLED
The following code reads distance measurements from the TFmini-S sensor and displays them on the OLED. The code is a simple extension of the code above. Have a quick look at the complete code first, and then we will discuss the 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.setTextSize(2); oled.setTextColor(WHITE); } void display() { static char text[30]; static int16_t dist, strength, temp; if (tfmini.getData(dist, strength, temp)) { sprintf(text, "%4d cm", dist); oled.clearDisplay(); oled.setCursor(20, 25); 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 the TFmini-S 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:
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 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 change to 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.
display function
The display()
function calls tfmini.getData
to get the distance reading from the TFmini-S sensor. It then clears the display and prints the distance. The sprintf()
function is used to convert the distance value into a formatted text.
void display() { static char text[30]; static int16_t dist, strength, temp; if (tfmini.getData(dist, strength, temp)) { sprintf(text, "%4d cm", dist); oled.clearDisplay(); oled.setCursor(20, 25); oled.print(text); oled.display(); } }
If you put a object in front of the sensor, the display should show the measured distance in centimetres. See the following example output below.
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-S you can measure distances between 0.1 to 12 meters.
Conclusions
In this tutorial you learned how to use the TFmini-S Distance Sensor with an Arduino to measure distances and to display them on an OLED.
The TFmini-S is similar to the TF-Luna but the TFmini-S is a little bit bigger and can measure distance up to 12 meter, while the TF-Luna is limited to 8 meters. The TFmini-S is also a bit more accurate (and more expensive) but both sensors have a resolution of 1 cm.
If a shorter range is fine but 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 and they are much cheaper.
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 ; )
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.