Skip to Content

TF-Luna Distance Sensor with Arduino 

TF-Luna Distance Sensor with Arduino 

In this tutorial you will learn how to use the TF-Luna Laser Range Distance Sensor with an Arduino or any other common microcontroller (ESP32/ESP8266) to measure distances.

The TF-Luna is a compact and accurate LiDAR/Proximity sensor that measures distances of up to 8 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 TF-Luna up and running with Arduino.

Required Parts

You will need an TF-Luna Sensor (listed below). As for the microcontroller, I used an Arduino Uno for this project, but any other Arduino or any ESP32/ESP8266 will work as well. We also will use an OLED to show the distances measured by the TF-Luna on a little display.

TF-Luna 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 TF-Luna

The TF-Luna sensor is using the Time of Flight (TOF) principle to measure distances. It periodically emits near infrared modulated waves and calculates the time by measuring the phase difference between the
original wave and the reflection wave. See the following illustration below:

Time of Flight (TOF) principle
Time of Flight (TOF) principle (source)

The operation range (indoor) of the sensor is 0.2m~8m with an accuracy of ±6cm@ (0.2-3m) or ±2%@ (3m-8m) and a resolution of 1 cm. The TF-Luna needs 3.7V-5.2V as power supply and the average current is around 70mA, with a peak current of up to 150mA. Communication is via UART or I2C. For more details have a look at the datasheet linked below:

TF-Luna as part of a LiDAR system

There are many applications for applications for laser range sensors such as parking proximity warning, intruder detection, obstacle avoidance in robotics and more. Most importantly laser range sensors are commonly used in LiDAR systems, which stands for Light Detection and Ranging and is used to create highly accurate 3D maps and models.

In addition to the laser range sensor a LiDAR system typically has a GPS unit, and an inertial measurement unit (IMU). The scanner is a mechanism that moves the laser beam across a wide area, allowing the system to capture data from different angles and positions. The GPS unit provides accurate location data. Finally, the IMU tracks the orientation and movement of the LiDAR system, which is crucial for maintaining accuracy during scanning.

As the laser pulses are emitted and reflected, the system continuously records the distance measurements along with their corresponding angles and positions. This data is then used to create a point cloud, which represents the spatial distribution of objects in the scanned area.

You can build a simple LiDAR yourself by attaching the laser range sensor on a servo and use the servo to scan the environment, while the laser range sensor measures distances. However, in this tutorial we will only be using the laser range sensor itself, without a scanning device.

Pinout of TF-Luna

The TF-Luna has 6 pins. The picture below shows the side of the sensor with the connector and its six pins:

Pinout of TF Luna
Pinout of TF-Luna

Pin 1 (5V) and Pin 4 (GND) are for the power supply. Pins 2 and 3 are for I2C or UART communication. Which protocol is used depends on the state of Pin 5 (CFG). If pulled to ground, I2C is enabled. Pin 6 (MUX) provides a data-ready signal, if I2C is enabled.

Note that the connector cable that typically comes with TF-Luna sensor uses arbitrary colors. Don’t rely on the colors to infer, which cable goes to which Arduino pin.

Connecting the TF-Luna

The TF-Luna sensor can communicate via UART or I2C. We are going to use the I2C interface. First, connect pin 1 (VCC) of the TF-Luna to the 5V output of the Arduino. Next connect SDA (pin 2) of the TF-Luna to the SDA pin of the Arduino. Then connect SCL (pin 3) of the TF-Luna to Arduino’s SCL.

Finally, you need to connect pins 4 and 5 of the TF-Luna to ground (GND). Pin 6 remains unconnected. See the complete wiring diagram below:

Connecting TF-Luna with Arduino
Connecting TF-Luna with Arduino

The following table lists all the connection you need to make.

TF-LunaArduino
Pin 1 (VCC)5V
Pin 2 (SDA)SDA
Pin 3 (SCL)SCL
Pin 4 (GND)GND
Pin 5 (CFG)GND
Pin 6 (MUX)not connected

Note that the TF-Luna requires 5V as power supply. In contrast to many other small IR distance sensors, it will not work with 3.3V.

However, you can use it with a 3.3V microcontroller, e.g. an ESP32. Just make sure to use the 5V output or another power source with 5V. The picture below shows you, how to connect the TF-Luna to an ESP32 C3 Mini, for instance:

Connecting TF-Luna with ESP32 C3 Mini
Connecting TF-Luna with ESP32 C3 Mini

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

Code for measuring distance with TF-Luna

Before you can measure distances with the TF-Luna sensor, you will have to install a library. We are going to use the TFL22C library by Bud Ryerson. To install it, open the Library Manager, search for “TFLuna”, find the library by Bud Ryerson and install it by pressing the “INSTALL” button.

Installing TFL22C library by Bud Ryerson via Library Manager
Installing TFL22C library by Bud Ryerson via Library Manager

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

#include "Wire.h"    
#include "TFLI2C.h"  

TFLI2C sensor;

void setup() {
  Serial.begin(9600);  
  Wire.begin();          
}

void loop() {
  int16_t dist; 
  if (sensor.getData(dist, 0x10)) {
    Serial.print("dist:");
    Serial.println(dist);
  }
  delay(100);
}

The code starts by including the necessary libraries for I2C communication and the TF-Luna sensor.

#include "Wire.h"    
#include "TFLI2C.h"  

The Wire.h library is used for I2C communication, while TFLI2C.h is a specific library for interfacing with the TF-Luna sensor.

Next, we create an instance of the TFLI2C class which represents our distance sensor.

TFLI2C sensor;

setup function

In the setup() function, we initialize the Serial communication and the I2C bus.

void setup() {
  Serial.begin(9600);  
  Wire.begin();          
}

The Serial.begin(9600) command initializes the Serial Monitor at a baud rate of 9600 bps, allowing us to send and receive data. The Wire.begin() command initializes the I2C bus, enabling communication with the sensor.

loop function

In the loop() function, we continuously read the distance data from the sensor.

void loop() {
  int16_t dist; 
  if (sensor.getData(dist, 0x10)) {
    Serial.print("dist:");
    Serial.println(dist);
  }
  delay(100);
}

Here, we declare a variable dist of type int16_t to hold the distance value. The sensor.getData(dist, 0x10) function attempts to read the distance data from the sensor. Should the getData() call fail, you can find the meanings of the error codes in the header file of the library.

The second parameter 0x10 is the default I2C address of the sensor. The TF-Luna sensor has a configurable I2C address (0x080x77). If you want to change it you can call the Set_I2C_Addr() function.

If the data is successfully retrieved, we print the distance value to the Serial Monitor using Serial.print() and Serial.println(). The delay(100) command introduces a 100 millisecond pause before the next reading.

This loop runs indefinitely, continuously measuring and displaying the distance detected by the TF-Luna sensor. If you upload and run the code you should see the measured distances in centimeters printed on the Serial Monitor:

Distances measured with TF Luna printed on Serial Monitor
Distances measured with TF-Luna printed on Serial Monitor

The TF-Luna sensor can measure distance of up to 8 m (=800 cm). Beyond that the code will print a distance of 900, indicating that no object could be detected.

Below some distance measurements of TF-Luna shown on the Serial Plotter when moving my hand closer and further away:

Distances measured with TF-Luna on Serial Plotter
Distances measured with TF-Luna on Serial Plotter

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 TF-Luna distance data

Since the OLED is also an I2C device, connecting it is straightforward. We simply connect SDA and SCL to the same pins the TF-Luna sensor is connected to. And since the OLED can run on 5V, we can also share the power supply lines.

Connecting OLED and TF-Luna with Arduino
Connecting OLED and TF-Luna with Arduino

Code to display distances measured by TF-Luna on OLED

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

#include "Wire.h"
#include "TFLI2C.h"
#include "Adafruit_SSD1306.h"

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
TFLI2C sensor;

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

void display() {
  static char text[30];
  static int16_t dist;
  if (sensor.getData(dist, 0x10)) {
    sprintf(text, "%4d cm", dist);

    oled.clearDisplay();
    oled.setCursor(20, 25);
    oled.print(text);

    oled.display();
  }
}

void setup() {
  Wire.begin();
  oled_init();
}

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

Libraries and Display Initialization

We start by including the Wire library for I2C communication, TFL22C library for the sensor and the Adafruit_SSD1306 Library for the OLED display. We then create the oled object and the sensor object.

#include "Wire.h"
#include "TFLI2C.h"
#include "Adafruit_SSD1306.h"

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
TFLI2C sensor;

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 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 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 sensor.getData() to get the distance reading from the TF-Luna 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;
  if (sensor.getData(dist, 0x10)) {
    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.

Measured Distance shown on OLED
Measured Distance shown on OLED

setup function

In the setup() function we first call Wire.begin() to start the I2C communication, and then call oled_init(), which initializes the OLED.

void setup() {
  Wire.begin();
  oled_init();
}

loop function

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

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

And that’s it. With the TF-Luna you can measure distances between 3 cm up to 800 cm, though distance below 20 cm are not very accurate.

Conclusions

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

In contrast to other Time-of-Flight sensors such as the VL53L0X, the VL53L1X, or the TOF10120, the TF-Luna is bigger, needs 5V for power supply, but has a much larger range. However, the resolution is only in centimeters (not millimeters) and the sensor is more expensive.

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 and have a much shorter range.

The TF-Luna is most suitable for long range detection of objects in outdoor environments. Note, however, that the back-side of the sensor is not encapsulated and you will need to protect its electronics, when using it outdoors.

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

Happy Tinkering ; )