Skip to Content

TOF10120 Distance Sensor with Arduino

TOF10120 Distance Sensor with Arduino

In this tutorial you will learn how to use the TOF10120 Distance Sensor with an Arduino to measure distances.

The TOF10120 is a is a very small, Time-of-Flight Distance Sensor (ToF) sensor that uses infrared laser light to measure the distance to an object. It sends outs a light pulse and measures the time it takes to be reflected. From this Time-of-Flight it then computes the distance to the object with an accuracy in millimeters. Its compact size and low power consumption make it suitable for a wide range of DIY projects, including robotics, gesture recognition, and proximity sensing.

Required Parts

Obviously, you will need an TOF10120 Distance Sensor. As for the microcontroller, I used an Arduino Uno for this project, but any other Arduino or any ESP32/ESP8266 will work fine as well. To display the measured distances, I chose an OLED but you could also go with an LCD display.

TOF10120 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 TOF10120

The TOF10120 Time-of-Flight Distance Sensor is a is a very small (10mm x 13mm), high-precision sensor that uses infrared light to measure distances.

Dimensions of TOF10120
Dimensions of TOF10120 (source)

It is based on the Time-of-Flight (ToF) principle, where the sensor emits light pulses and measures the time it takes for the light to reflect back to calculate the distance. The TOF10120 has a range of up to 180 cm and operates at a wavelength of 940nm. The picture below shows the cone of the laser LED that emits the light pulse and the view cone of the detector that registers the reflected light.

Working Principle of TOF10120
Working Principle of TOF10120 (source)

The TOF10120 operates on 3V to 5V, with a low, average current consumption of only 35 mA. The following list summarizes its main features:

  • Working range: 100-1800mm
  • Measurement error: up to 5%
  • High speed ranging: max 30ms
  • Communication interface: UART / I2C
  • UART transmission parameters: 9600 8n1
  • Wavelength: 940 nm
  • Field of view: 25°
  • Ambient light immunity: 50k lux
  • Voltage range: 3V to 5V
  • Average current consumption: 35 mA

The above specification and the datasheet state that the minimum distance that can be measured is 100mm. However, I found that you actually can get measurements as close as 10mm and as far as 2000mm but under 30mm the readings become very inaccurate.

The TOF10120 communicates with microcontrollers via an I2C or UART interface. The picture below shows the pinout. SDA and SCL are for the I2C interface and RxD and TxD for UART communication.

Pinout of TOF10120
Pinout of TOF10120

In the next section you will learn how to connect the TOF10120 to an Arduino.

Connecting the TOF10120

We are going to use the I2C interface to connect the TOF10120 sensor to an Arduino. First, connect the SCL (6) and SDA (5) pins of the TOF10120 breakout board to the corresponding pins on the Arduino board as shown below. Next, connect ground to pin 1 and 3.3V to pin 2 of the TOF10120.

Connecting TOF10120 with Arduino
Connecting TOF10120 with Arduino

The TOF10120 sensor runs on 5V or 3.3V and you can use either for VDD. In the wiring above, I am using 3.3V for VDD. Note that pin 3 and 4 (RxD, TxD) of the TOF10120 are not connected, since we are not using the UART interface but I2C.

Next, let us write some code to test the function of the TOF10120 sensor.

Code for measuring distance with TOF10120

Reading distance data from the TOF10120 via I2C is fairly easy. The following code is a shortened and cleaned-up version derived from the supplier documentation for the TOF10120.

int distance(int addr = 0x52) {
  unsigned short dist = 0;
  Wire.beginTransmission(addr);
  Wire.write(0);
  Wire.endTransmission();
  delay(1);
  Wire.requestFrom(addr, 2);
  if (Wire.available() != 2)
    return -1;
  dist = Wire.read() << 8;
  dist |= Wire.read();
  return dist;
}

It first sets the reading address where the distance data is read from via Wire.write(0). It then reads two consecutive bytes by calling Wire.read(), constructs the distance value by setting the high and low byte, and returns the distance value.

Note that according to the data sheet the I2C 8-bit address of the TOF10120 sensor is 0xA4 but since the Wire library uses only the high 7 bits for the I2C address, you have to use the corresponding 7-bit address 0x52. Also note that you should not poll the sensor faster than every 30 msec.

You could use this code as it is but I implemented a small TOF10120 library that will make your life easier.

Install TOF10120 library

To install the TOF10120 library go to the tof10120_arduino_lib repo here and click on the green “Code” button. Then click on “Download Zip” as show below:

Download TOF10120 library
Download TOF10120 library

In the Arduino IDE create the following test code:

#include "TOF10120.h"

TOF10120 sensor = TOF10120();

void setup() {
  Serial.begin(9600);
  sensor.init();
}

void loop() {
  Serial.print("distance:");
  Serial.println(sensor.distance());
  delay(100);
}

Then go “Sketch” -> “Include Library” -> “Add .Zip Library..” and select the “tof10120_arduino_lib-main.zip” file you just downloaded before:

Adding TOF10120 library to sketch
Adding TOF10120 library to sketch

The test code is very simple. First it includes the TOF10120 library and creates the TOF10120 sensor object. In the setup() function the sensor then gets initialized and in the loop() function we finally call sensor.distance() to read the distance measured by the sensor.

If you want to connect the sensor to different SDA and SCL pins, you can specify them via sensor.begin(sda, scl); for ESP32 and ESP8266 boards.

Run test code for TOF10120 sensor

If you upload the code to your Arduino you should see distance values printed to the Serial Monitor. If there is no object in front of the sensor, you will see a distance of 2000mm printed.

Distances printed on Serial Monitor
Distances printed on Serial Monitor

If you open the Serial Plotter and place your hand in front of the sensor, and move it closer or further away, you should see a graph similar to the one shown below.

Distances shown on Serial Plotter
Distances shown on Serial Plotter

If you have issues and the sensor doesn’t seem to work make sure that the wiring is correct and that the correct SDA and SCL pins are used. You can also check the laser diode of the sensor by taking a picture with a digital camera (mobile phone). While IR light is invisible to the human eye, the camera can see it. The picture below shows the TOF10120 with the IR diode clearly illuminated:

Illuminated IR Diode of TOF10120
Illuminated IR Diode of TOF10120

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

Adding an OLED to display TOF10120 data

Typically, we want the measured distance to be displayed in some way. It could be an LED bar or in this case an OLED. Since the OLED is also an I2C device, connecting it is straightforward. We simply connect SDA and SCL to the same pins the TOF10120 sensor is connected to. And since the OLED runs on 3.3V, we can also share the power supply lines. The picture below shows the complete wiring.

onnecting OLED and TOF10120 with Arduino
Connecting OLED and TOF10120 with Arduino

If you have any difficulties with the OLED, have a look at the tutorial How to Interface the SSD1306 I2C OLED Graphic Display With Arduino. The picture below shows the complete wiring on a real breadboard:

Wiring of OLED and TOF10120 with Arduino
Wiring of OLED and TOF10120 with Arduino

Code to display TOF10120 data on OLED

In this section we write the code to show the distance measured by the TOF10120 sensor on the OLED screen. To write to the OLED we will use the Adafruit_SSD1306 library. You can install it via the Library Manager as usual:

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

The code below reads the measurements from the TOF10120 sensor and displays it on the OLED. Have a look at the complete code first, and then we dive into its details.

// Measure distance with TOF10120 sensor and show on OLED
// by Makerguides
#include "Adafruit_SSD1306.h"
#include "TOF10120.h"

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

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

void display() {
  static char text[30];
  int dist = sensor.distance();
  sprintf(text, "%4d mm", dist);

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

  int w = map(dist, 0, 2000, 0, 120);
  oled.drawFastHLine(4, 45, w, WHITE);
  oled.display();
}

void setup() {
  sensor.init();
  oled_init();  
}

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

Libraries and Display Initialization

We start by including the Library for the TOF10120 sensor and the Adafruit_SSD1306 Library for the OLED display. We then create the oled object and the sensor object.

#include "Adafruit_SSD1306.h"
#include "TOF10120.h"

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

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.distance() to get the distance reading from the TOF10120 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];
  int dist = sensor.distance();
  sprintf(text, "%4d mm", dist);

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

  int w = map(dist, 0, 2000, 0, 120);
  oled.drawFastHLine(4, 45, w, WHITE);
  oled.display();
}

In addition to the text output, the display() function also draws a horizontal line under the text with a length proportional to the measured distance. If nothing is in front of the sensor (or further away than 2m) the display will show the maximum distance of 2000mm and a full-length line.

Maximum Distance shown on OLED
Maximum Distance shown on OLED

If you put a object in front of the sensor, the display will show the measured distance in millimeters and a shorter line. See the following example output below.

Measured Distance shown on OLED
Measured Distance shown on OLED

Should the sensor for some reason not be able to get a distance reading the distance() function returns -1 and this value would be shown on the OLED. However, I have never encountered this.

setup function

In the setup() function we first call sensor.init() to initialize the TOF10120 sensor, and then call oled_init(), which initializes the OLED.

void setup() {
  sensor.init();
  oled_init();  
}

loop function

The loop() function simply calls the display() function every 100 msec. As mentioned before, you should not go faster than 30 msec but you can use a larger delay.

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

And that’s it. Now you have your own distance measuring tool that can accurately measure distances between 10mm and 2000mm.

Conclusions

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

The TOF10120 Sensor is a is a very small, fast, high-precision sensor that uses infrared laser light to measure distance. Specifically it measures the Time-of-Flight of the reflected laser impulse to calculate the distance to an object. If you look for an alternative: a very similar sensor with comparable specifications is the VL53L0X.

These sensors are in contrast to other infrared distance sensors such as the GP2Y0A710K0F or the GP2Y0A21YK0F, which use triangulation to determine distance based on the angle of the reflected IR light. But the TOF10120 and VL53L0X both have a longer range and higher accuracy compared to the GP2Y0A710K0F and GP2Y0A21YK0F sensors.

IR distance sensors can be affected by strong ambient light. An alternative are Ultrasonic distance sensors such as the common HC-SR04 that use sound waves to measure distance. However, IR laser sensors are more precise and tend to have a longer range compared to ultrasonic sensors but are also a bit more expensive.

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

Happy Tinkering ; )