Fermion: TMF8801 ToF Distance Ranging Sensor 20-2500mm Wiki - DFRobot

[](Product Link)

Introduction

The TMF8801 is a time-of-flight (ToF) distance ranging sensor that uses the time difference between light pulses to measure distance. It provides single zone detection of an object irrespective of the color, reflectivity and texture of the object. The TMF8801 offers high dynamic range and detection sensing measurements from 2-250cm distances. The device can make highly accurate distance measurements within ±5% and is capable of operation in dark environments and in the presence of sunlight. A built-in histogram is featured to delivers dynamic cover glass calibration and crosstalk compensation and background light noise is minimized through on-chip sunlight rejection filters. The TMF8801 outputs data through a I2C fast-mode communications interface and an integrated micro controller, which is featured with all algorithms included on-chip with no need for external optics.

Features

Applications

Specification

Board Overview

Board Overview

Name Function
VCC +
GND -
SCL Clock line
SDA Data line
INT Alert Interrupt
EN Reset
PIN0 Interrupt output pin0
PIN1 Interrupt output pin1

Tutorial

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

Connection Diagram

SEN0430 Connection

Sample Code 1 - Distance Detection

/*!
 * @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.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");
  }
}

Expected Results

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-20cm.

Sample Code 2 - Interrupt Output

You can connect the INT of TMF8×01 to the MCU's external interrupt pin. When there is data output from the sensor, the INT will generate a LOW level and MCU can determine the coming data by detect that low level.

/*!
 * @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 When there is data output from the sensor, the INT will generate a LOW level and MCU can determine the coming data by detect that low level.
 * *
 * Ranging mode configuration table: 
 * TMF8X01_MODE_PROXIMITY: PROXIMITY mode
 * TMF8X01_MODE_DISTANCE: DISTANCE mode  
 * TMF8X01_MODE_COMBINE: PROXIMITY and DISTANCE hybrid mode
 * default mode: TMF8X01_MODE_COMBINE
 * --------------------------------------------------------------------------------|
 * |  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_TMF8701 tmf8x01(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8801 tmf8x01(/*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(tmf8x01.begin() != 0){                                                                        //Initialization sensor,sucess return 0, fail return -1
      Serial.println("failed.");
      delay(1000);
  }
  Serial.println("done.");

  Serial.print("Sensor Version info: ");
  Serial.println(tmf8x01.getVersion());                                                               //Print sensor info, fomat:major_patch_HW_SERIAL NUMBER 

  tmf8x01.enableIntPin();                                                                             //Enable INT pin to check measurement data. Sending a low signal to host if measurement distance completed.

  #ifdef ARDUINO_ARCH_MPYTHON 
  /* mPython Interrupt Pin vs Interrupt NO
   * ----------------------------------------------------------------------------------------------------
   * |                    |  DigitalPin  |        P0~P20 can be used as external interrupt              |
   * |    mPython           |---------------------------------------------------------------------------|
   * |                    | Interrupt No |  use digitalPinToInterrupt(Pn) to query interrupt number     |
   * |--------------------------------------------------------------------------------------------------|
   */
  attachInterrupt(digitalPinToInterrupt(INT)/*query Interrupt NO of P0*/,notifyFun,FALLING);            //Enable the external interrupt of mPython P0; rising edge trigger; connect INTA to P0
  #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

  tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalib);                                         //Enable measuring with Calibration data.
  //tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeNoCalib);                                     //Enable measuring with no Calibration data.
  //tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalibAndAlgoState);                           //Enable measuring with Calibration data and Algorithm state.

}

void loop() {
  if(irqFlag){
      irqFlag = false;
      if (tmf8x01.isDataReady()) {                                                                        //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
          Serial.print("Distance = ");
          Serial.print(tmf8x01.getDistance_mm());                                                         //Print measurement data to USB Serial COM, unit mm, in TMF8X01_MODE_COMBINE mode.
          Serial.println(" mm");
      }
  }
}

Expected Result

Sample Code - Sleep Mode

The sensor sleeps for 2s when finishing 20 measurements. In sleep mode, it stops ranging. We can activate it by wakeup function.

Note:it consumes 37.9mA current in ranging mode, 1.2mA in sleep mode.

/*!
 * @file sleep.ino
 * @brief The sensor sleeps for 2s when finishing 20 measurements. In sleep mode, it stops ranging. We can activate it by wakeup function.
 * @n note: it consumes 37.9mA current in ranging mode, 1.2mA in sleep mode.
 * *
 * Ranging mode configuration table: 
 * TMF8X01_MODE_PROXIMITY: PROXIMITY mode
 * TMF8X01_MODE_DISTANCE: DISTANCE mode  
 * TMF8X01_MODE_COMBINE: PROXIMITY and DISTANCE hybrid mode
 * default mode: TMF8X01_MODE_COMBINE
 * --------------------------------------------------------------------------------|
 * |  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 tmf8x01(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8801 tmf8x01(/*enPin =*/EN,/*intPin=*/INT);

#define NUM_OF_MEASUREMENT   20                                                //20 measurements
#define SLEEP_TIME           2000                                              //sleep 2000ms
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(tmf8x01.begin() != 0){                                                 //Initialization sensor,sucess return 0, fail return -1
      Serial.println("failed.");
      delay(1000);
  }
  Serial.println("done.");

  Serial.print("Sensor Version info: ");
  Serial.println(tmf8x01.getVersion());                                        //Print sensor info, fomat:major_patch_HW_SERIAL NUMBER 

  tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalib);                 //Enable measuring with Calibration data.
  //tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeNoCalib);             //Enable measuring with no Calibration data.
  //tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalibAndAlgoState);   //Enable measuring with Calibration data and Algorithm state.
}

void loop() {
  if (tmf8x01.isDataReady()) {                                                 //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
      Serial.print("Distance = ");
      Serial.print(tmf8x01.getDistance_mm());                                  //Print measurement data to USB Serial COM, unit mm, in TMF8X01_MODE_COMBINE mode.
      Serial.println(" mm");
      count++;
  }
  if(count > NUM_OF_MEASUREMENT){
      count = 0;
      tmf8x01.sleep();                                                         //sensor enter sleep mode.
      Serial.println("sleep...");
      delay(SLEEP_TIME);
      Serial.println("wakeup...");
      tmf8x01.wakeup();                                                       //wakeup sensor from sleep mode to enter ranging mode.
  }
}

Expected Result

Sample Code 4 - Calibration Mode

The demo shows us how to obtain 14byte calibration and set calibration range.

Experiment condition: dark environment, no objects within 40cm around the sensor.

/*!
 /*!
 * @file calibration.ino
 * @brief This demo tells how to get 14 bytes calibration, and how to set calibration ranging.
 * @n This demo application scenario: no target within 40cm of the sensor, in dark conditions.
 * *
 * Ranging mode configuration table: 
 * TMF8X01_MODE_PROXIMITY: PROXIMITY mode
 * TMF8X01_MODE_DISTANCE: DISTANCE mode  
 * TMF8X01_MODE_COMBINE: PROXIMITY and DISTANCE hybrid mode
 * default mode: TMF8X01_MODE_COMBINE
 * --------------------------------------------------------------------------------|
 * |  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 tmf8x01(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8801 tmf8x01(/*enPin =*/EN,/*intPin=*/INT);

uint8_t calibrationDataBuffer[14] = {0x41,0x57,0x01,0xFD,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04};  //The calibration data needs to be obtained for every sensor by call the function getCalibrationData.
int distance = 0;

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(tmf8x01.begin() != 0){                                                                        //Initialization sensor,sucess return 0, fail return -1
      Serial.println("failed.");
      delay(1000);
  }
  Serial.println("done.");

  Serial.print("Sensor Version info: ");
  Serial.println(tmf8x01.getVersion());                                                               //Print sensor info, fomat:major_patch_HW_SERIAL NUMBER 

/*
  while(tmf8x01.getCalibrationData(calibrationDataBuffer, sizeof(calibrationDataBuffer)) != true){    //get calibration data to update calibrationDataBuffer buffer. You needs to be obtained for every sensor by call the function getCalibrationData.
      Serial.println("Get and print calibration data...");
      delay(1000);
  }
  Serial.println("Get and print calibration...sucess");
  for(int i = 0; i < sizeof(calibrationDataBuffer); i++){
      Serial.print("0x");
      if(calibrationDataBuffer[i] < 16) Serial.print("0");
      Serial.print(calibrationDataBuffer[i]);
      Serial.print(",");
  }
  Serial.println();
*/

  tmf8x01.setCalibrationData(calibrationDataBuffer, sizeof(calibrationDataBuffer));                   //Set calibration data to sensor.
  tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalib);                                        //Enable measuring with Calibration data.
  //tmf8x01.startMeasurement(/*cailbMode =*/tmf8x01.eModeCalibAndAlgoState);                          //Enable measuring with Calibration data and Algorithm state.
}

void loop() {
  if (tmf8x01.isDataReady()) {                                                                        //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
      Serial.print("Distance = ");
      Serial.print(tmf8x01.getDistance_mm());                                                         //Print measurement data to USB Serial COM, unit mm, in TMF8X01_MODE_COMBINE mode.
      Serial.println(" mm");
  }
}

Expected Result

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFshopping_car1.png Get Fermion: TMF8801 TOF Distance Ranging Sensor (20~2500mm) from DFRobot Store or DFRobot Distributor.

Turn to the Top