名称介绍

简介

TMF8701 是一个飞行时间 (ToF) 激光测距传感器,通过测量光波的脉冲差测量距离。无论物体的颜色、反射率和纹理如何,它都能提供物体的单区域检测。
TMF8701提供 1-10cm的接近检查和10-60cm的距离检查 距离的高动态范围测量,能够在 ±5% 范围内进行高精度距离测量,并且能够在黑暗环境和有阳光的情况下运行。
TMF8701内置直方图提供动态盖板玻璃校准和串扰补偿,并且通过片上阳光抑制滤波器将背景光噪声降至最低。
TMF8701通过I2C 快速模式通信接口和一个集成微控制器,所有算法都包含在片上,无需外部光学器件。

特色

适用场景:

技术规格

引脚说明

名称 功能描述
VCC 电源正极
GND 电源负极
SCL 时钟线
SDA 数据线
INT 报警中断
EN 复位
PIN0 中断输出引脚0
PIN1 中断输出引脚1

使用教程

准备

int begin();

/**
 * @brief sleep sensor by software, the sensor enter sleep mode(bootloader). Need to call wakeup function to wakeup sensor to enter APP0
 */
  void sleep();
/**
 * @brief wakeup device from sleep mode, it will running app0
 * @return enter app0 return true, or return false.
 */
  bool wakeup();

/**
 * @brief get a unique number of sensor .Each sensor has a unique identifier.
 * @return return 4bytes unique number:
 * @n  the byte0 of return: serial_number_0
 * @n  the byte1 of return: serial_number_1
 * @n  the byte2 of return: identification_number_1
 * @n  the byte2 of return: identification_number_0
 */
  uint32_t getUniqueID();
/**
 * @brief get sensor's model.
 * @return return a String:
 * @n  TMF8801: the sensor is TMF8801
 * @n  TMF8701: the sensor is TMF8701
 * @n  unknown : unknown device
 */
  String getSensorModel();
/**
 * @brief get software version of patch.
 * @return return string of device software version,format:
 * @n major.minor.patch numbers.chip id version
 */
  String getSoftwareVersion();
/**
 * @brief Get 14 bytes of calibration data.
 * @param data Cache for storing calibration data
 * @param len The bytes of calibration data,its value can only be 14 bytes
 * @return Vail data return true, or return false.
 */
  bool getCalibrationData(uint8_t *data, uint8_t len = SENSOR_MTF8x01_CALIBRATION_SIZE);
/**
 * @brief set 14 bytes of calibration data.
 * @param data Pointer to calibration data.
 * @param len The bytes of calibration data,its value can only be 14 bytes
 * @return set sucess return true, or return false.
 */
  bool setCalibrationData(uint8_t *data, uint8_t len = SENSOR_MTF8x01_CALIBRATION_SIZE);

/**
 * @brief disable measurement config. Need to call startMeasurement before using this function. 
 */
  void stopMeasurement();
/**
 * @brief Waiting for data ready.
 * @return if data is valid, return true, or return false.
 */
  bool isDataReady();
/**
 * @brief get distance, unit mm. Before using this function, you need to call isDataReady().
 * @return return distance value, unit mm.
 */
  uint16_t getDistance_mm();
/**
 * @brief enable INT pin. If you call this function,which will report a interrupt
 * @n signal to host by INT pin when measure data is ready.
 */
  void enableIntPin();
/**
 * @brief disable INT pin.
 */
  void disableIntPin();
/**
 * @brief power on sensor when power down sensor by EN pin.
 * @return sucess return True, or return False
 */
  bool powerOn();
/**
 * @brief power down sensor by EN pin.
 * @return sucess return True, or return False
 */
  bool powerDown();

/**
 * @brief get I2C address.
 * @return return 7 bits I2C address
 */
  uint8_t getI2CAddress();
/**
 * @brief Config the pin of sensor.
 * @param pin: The pin of sensor, example PIN0 and PIN1,which is an enumerated variable of ePin_t.
 * @n     ePIN0:  The PIN0 of sensor config.
 * @n     ePIN1:  The PIN1 of sensor.
 * @n     eGPIOTotal:  both of PIN0 and PIN1.
 * @param config:  The config of pin, which is an enumerated variable of ePinControl_t.
 */
  void pinConfig(ePin_t pin, ePinControl_t config);

接线图

样例代码1 - 距离检测

/*!
 * @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](xue.peng@dfrobot.com)
 * @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.eCOMBINE);      //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");
  }
}

结果

通过接近模式和测距模式测量距离。
接近模式可以测量0-10cm
测距模式可以测量10-60cm
混合模式可以测量0-60cm
超出测量范围显示0mm
注意:0-10mm的测量数据可能不准确

样例代码2 - 中断输出

你可以把TMF8x01的INT引脚连接到MCU外部中断引脚。
当传感器有数据时,INT引脚将会产生一个低电平信号,MCU通过检测低电平可以判断是否有数据到来

/*!
 * @file interrupt.ino
 * @brief If you enable INT pin, MCU will capture a interrupt signal when the measure is completed.
 * @n You can attach the INT pin of TMF8x01 to MCU external interrupt pin.
 * @n 当传感器有数据时,INT引脚将会产生一个低电平信号,MCU通过检测低电平可以判断是否有数据到来
 * *
 * 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    |   to the external interrupt pin of MCU   |
 * |------------------------------------------------------|
 * |    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](xue.peng@dfrobot.com)
 * @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      2                                                       //connected INT pin of module1 to the external interrupt pin of MCU

//DFRobot_TMF8801 tof(/*enPin =*/EN,/*intPin=*/INT);
DFRobot_TMF8701 tof(/*enPin =*/EN,/*intPin=*/INT);

bool irqFlag = false;
void notifyFun(){
  irqFlag = true;
}

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.enableIntPin();                                                                             //Enable INT pin to check measurement data. Sending a low signal to host if measurement distance completed.

  #if defined(ESP32)||defined(ESP8266)
  /* mPython Interrupt Pin vs Interrupt NO
   * --------------------------------------------------------------------------------------------------
   * |                    |  DigitalPin  |             can be used as external interrupt              |
   * |    ESP32           |---------------------------------------------------------------------------|
   * |    ESP8266         | Interrupt No |  use digitalPinToInterrupt(pin numbers)                    |
   * |------------------------------------------------------------------------------------------------|
   */
  attachInterrupt(/*Interrupt NO=*/digitalPinToInterrupt(INT),notifyFun,FALLING);            //Enable the external interrupt of ESP32'D9 or ESP8266's D5; rising edge trigger; connect INT to D9 in ESP32, connected INT to D5 in ESP8266
  #else
  /* Main-board of AVR series    Interrupt Pin vs Interrupt NO
   * ---------------------------------------------------------------------------------------
   * |                                        |  DigitalPin  | 2  | 3  |                   |
   * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  |                   |
   * |-------------------------------------------------------------------------------------|
   * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
   * |               Mega2560                 |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
   * |-------------------------------------------------------------------------------------|
   * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
   * |    Leonardo, other 32u4-based          |--------------------------------------------|
   * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
   * |--------------------------------------------------------------------------------------
   */
  /* microbit  Interrupt Pin vs Interrupt NO
   * ---------------------------------------------------------------------------------------------------------------
   * |                                                   |  DigitalPin  | P0~P20 can be used as external interrupt |
   * |                  microbit                         |---------------------------------------------------------|
   * |(when used as external interrupt, do not need to   | Interrupt No | Interrupt NO is pin value, for instance, |
   * | set it to input mode via pinMode)                 |              | the Interrupt NO of P0 is 0, P1 is 1.    |
   * |-------------------------------------------------------------------------------------------------------------|
   */
  attachInterrupt(/*Interrupt NO=*/0,notifyFun,FALLING);                                                 //Enable external interrupt 0, connect INTA to the main-controller's digital pin: UNO(2),Mega2560(2),Leonardo(3),microbit(P0)
  #endif

/**
 * @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);
  //tof.startMeasurement(/*cailbMode =*/tof.eModeCalib, /*disMode =*/tof.ePROXIMITY);      //only support TMF8701
}

void loop() {
  if(irqFlag){
      irqFlag = false;
      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");
      }
  }
}

结果

样例代码3 - 睡眠模式

测距20次,睡眠2s,再测距,TMF8x01传感器可以进入睡眠模式,在该模式下,传感器将停止测距.我们可以通过wakeup去唤醒该传感器,重新进入测距模式。
注意:测距模式下电流大概为37.9mA, 睡眠模式下电流为1.2mA

/*!
 * @file sleep.ino
 * @brief 测距20次,睡眠2ms,再测距,TMF8x01传感器可以进入睡眠模式,在该模式下,传感器将停止测距.我们可以通过wakeup去唤醒该传感器,重新进入测距模式。
 * @n note: 测距模式下电流大概为37.9mA, 睡眠模式下电流为1.2mA
 * *
 * 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](xue.peng@dfrobot.com)
 * @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_TMF8701 tof(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8801 tof(/*enPin =*/EN,/*intPin=*/INT);

#define NUM_OF_MEASUREMENT   20                                                //20 measurements
#define SLEEP_TIME           1000                                              //sleep 1000ms
uint8_t count = 0;                                                             //Measurement count

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());

/**
 * @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);
  //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");
      count++;
  }
  if(count > NUM_OF_MEASUREMENT){
      count = 0;
      tof.sleep();                                                         //sensor enter sleep mode.
      Serial.println("sleep...");
      delay(SLEEP_TIME);
      Serial.println("wakeup...");
      tof.wakeup();                                                       //wakeup sensor from sleep mode to enter ranging mode.
  }
}

结果

样例代码4 - 校准模式

这个演示告诉如何获得14字节校准,以及如何设置校准范围。
注意:本演示应用场景:在黑暗条件下,传感器周围40cm内无目标

/*!
 * @file calibration.ino
 * @brief This demo tells how to get 14 bytes calibration data.
 * @n If you want to obtain reliable calibration data, you need to calibrate under the following conditions: 
 * @ 1. no target within 40cm of the sensor;
 * @ 2. in dark conditions.
 * 
 * @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](xue.peng@dfrobot.com)
 * @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);

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.");

  /* If you want to obtain reliable calibration data, you need to calibrate under the following conditions: 
     1. no target within 40cm of the sensor;
     2. in dark conditions.*/

  // while(!Serial.available());
  // while(!Serial.available()){
      // Serial.read();
  // }

  uint8_t caliDataBuf[14] = {0};                                                                   //Store 14 bytes of calibration data.
  Serial.print("Calibration ");
  while(tof.getCalibrationData(caliDataBuf, sizeof(caliDataBuf)) != true){                         //get calibration data to update caliDataBuf buffer. You needs to be obtained for every sensor by call the function getCalibrationData.
      Serial.print(".");
      delay(1000);
  }

  Serial.println("Calibration complete.");
  Serial.print("caliDataBuf[0-13] = {0x");
  for(int i = 0; i < sizeof(caliDataBuf); i++){
       if(caliDataBuf[i] < 16) Serial.print("0");
       Serial.print(caliDataBuf[i],HEX);
       if(i < 13) Serial.print(", 0x");
  }
  Serial.println("}");

   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
 */
  if(!tof.startMeasurement(/*cailbMode =*/tof.eModeCalib)){                 //Enable measuring with Calibration data.
  //if(!tof.startMeasurement(/*cailbMode =*/tof.eModeCalib, /*disMode =*/tof.eCOMBINE)){      //only support TMF8701
      Serial.println("Enable measurement faild.\nPlease check the calibration data and recalibrate!!!");
      return;
  }
}

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");
  }
}

结果

样例代码5 - 校准模式

setRangingMode.ino(此演示仅支持TMF8701传感器)。
在TMF8701中设置测距模式为调整测量距离

/*!
 * @file setRangingMode.ino(This demo is only suport TMF8701 sensor)
 * @brief Set Ranging mode to adjustment measurement distance in TMF8701
 * @n note: TMF8801 only suport one mode, PROXIMITY and DISTANCE hybrid mode.
 * *
 * --------------------------------------------------------------------------------|
 * |  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](xue.peng@dfrobot.com)
 * @version  V1.0
 * @data   2021-04-02
 * @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_TMF8701 tof(/*enPin =*/EN,/*intPin=*/INT);

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());

/**
 * @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, /*disMode =*/tof.ePROXIMITY);
}

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");
  }
}

结果

常见问题

还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!

更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。

更多

DFshopping_car1.png DFRobot商城购买链接