Example Code for Arduino-Distance Detection
Get measurement data by PROXIMITY and DISTANCE hybrid mode. note: TMF8801 only suport one mode, PROXIMITY and DISTANCE hybrid mode.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- TMF8801 Sensor x1
- Jumper wires
Software Preparation
- Arduino IDE
- Download and install the TMF8×01 Library. (About how to install the library?)
Wiring Diagram

Other Preparation Work
Initialization sensor, set calibration data, config measurement params to enable measurement.
Sample Code
/*!
* @file getDistance.ino
* @brief Get measurement data by PROXIMITY and DISTANCE hybrid mode.
* @n note: TMF8801 only suport one mode, PROXIMITY and DISTANCE hybrid mode.
* *
* Ranging mode configuration table:
* --------------------------------------------------------------------------------|
* | Type | suport ranging mode | ranging ranges | Accuracy |
* |---------------------------------------|-----------------|---------------------|
* | TMF8801 | PROXIMITY and DISTANCE | | 20~100mm: +/-15mm |
* | | hybrid mode(only one) | 20~240cm | 100~200mm: +/-10mm |
* | | | | >=200: +/-%5 |
* |---------------------------------------|-----------------|---------------------|
* | | PROXIMITY mode | 0~10cm | |
* | |---------------------------|-----------------| >=200: +/-%5 |
* | TMF8701 | DISTANCE mode | 10~60cm | 100~200mm: +/-10mm |
* | |---------------------------|-----------------| |
* | | PROXIMITY and DISTANCE | 0~60cm | |
* | | hybrid mode | | |
* |---------------------------------------|-----------------|----------------------
* *
* @n hardware conneted table:
* ------------------------------------------
* | TMF8x01 | MCU |
* |-----------------------------------------|
* | I2C | I2C Interface |
* |-----------------------------------------|
* | EN | not connected, floating |
* |-----------------------------------------|
* | INT | not connected, floating |
* |-----------------------------------------|
* | PIN0 | not connected, floating |
* |-----------------------------------------|
* | PIN1 | not connected, floating |
* |-----------------------------------------|
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [Arya]([email protected])
* @version V1.0
* @data 2021-03-26
* @get from https://www.dfrobot.com
* @url https://github.com/DFRobot/DFRobot_TMF8x01
*/
#include "DFRobot_TMF8x01.h"
#define EN -1 //EN pin of of TMF8x01 module is floating, not used in this demo
#define INT -1 //INT pin of of TMF8x01 module is floating, not used in this demo
DFRobot_TMF8801 tof(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8701 tof(/*enPin =*/EN,/*intPin=*/INT);
uint8_t caliDataBuf[14] = {0x41,0x57,0x01,0xFD,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04};//The 14 bytes calibration data which you can get by calibration.ino demo.
void setup() {
Serial.begin(115200); //Serial Initialization
while(!Serial){ //Wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initialization ranging sensor TMF8x01......");
while(tof.begin() != 0){ //Initialization sensor,sucess return 0, fail return -1
Serial.println("failed.");
delay(1000);
}
Serial.println("done.");
Serial.print("Software Version: ");
Serial.println(tof.getSoftwareVersion());
Serial.print("Unique ID: ");
Serial.println(tof.getUniqueID(),HEX);
Serial.print("Model: ");
Serial.println(tof.getSensorModel());
tof.setCalibrationData(caliDataBuf, sizeof(caliDataBuf)); //Set calibration data.
/**
* @brief Config measurement params to enable measurement. Need to call stopMeasurement to stop ranging action.
* @param cailbMode: Is an enumerated variable of eCalibModeConfig_t, which is to config measurement cailibration mode.
* @n eModeNoCalib : Measuring without any calibration data.
* @n eModeCalib : Measuring with calibration data.
* @n eModeCalibAndAlgoState : Measuring with calibration and algorithm state.
* @param disMode : the ranging mode of TMF8701 sensor.(this mode only TMF8701 support)
* @n ePROXIMITY: Raing in PROXIMITY mode,ranging range 0~10cm
* @n eDISTANCE: Raing in distance mode,ranging range 10~60cm
* @n eCOMBINE: Raing in PROXIMITY and DISTANCE hybrid mode,ranging range 0~60cm
*/
tof.startMeasurement(/*cailbMode =*/tof.eModeCalib); //Enable measuring with Calibration data.
//tof.startMeasurement(/*cailbMode =*/tof.eModeCalib, /*disMode =*/tof.ePROXIMITY); //only support TMF8701
}
void loop() {
if (tof.isDataReady()) { //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
Serial.print("Distance = ");
Serial.print(tof.getDistance_mm()); //Print measurement data to USB Serial COM, unit mm, in eCOMBINE mode.
Serial.println(" mm");
}
}
Result
The sensor provides detection range of 0~2500cm. It outputs 0 when out of range.
Note: The measured data may not be accurate in 0-20mm and 240cm-250cm.

Additional Information
Ranging mode configuration table for TMF8801 and TMF8701.
Was this article helpful?
