Example Code for Arduino-Calibration Mode

This demo tells how to get 14 bytes calibration, and how to set calibration ranging. This demo application scenario: no target within 40cm of the sensor, in dark conditions.

Hardware Preparation

Software Preparation

Wiring Diagram

SEN0430 Connection

Other Preparation Work

Initialization sensor, get and set calibration data, start measurement with calibration data.

Sample Code

/*!
 /*!
 * @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]([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_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");
  }
}

Result

Result

Additional Information

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

Was this article helpful?

TOP