Example Code for Arduino-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.
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, start measurement with calibration data.
Sample Code
/*!
* @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]([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);
#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.
}
}
Result

Additional Information
Note about current consumption in different modes.
Was this article helpful?
