Example Code for Arduino-Interrupt Output
If you enable INT pin, MCU will capture a interrupt signal when the measure is completed. You can attach the INT pin of TMF8x01 to MCU 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.
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, enable INT pin, attach external interrupt.
Sample Code
/*!
* @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]([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 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");
}
}
}
Result

Additional Information
Hardware connected table for interrupt output mode.
Was this article helpful?
