Example Code for Arduino-PC Serial Port Debugging

Last revision 2025/12/08

Since TF mini is a serial device and the ordinary Arduino only has one hardware serial port, so we recommend you to use a software serial port to match the sensor. Users can also use multi-serial port devices such as Arduino Leonardo and Arduino Mega2560. Here we choose Arduino UNO as a controller, which is very common to see and define D12 and D13 as software serial port. PC serial port tool is needed. Data been read will be displayed in the serial port tool interface.

Hardware Preparation

  • DFRduino UNO R3 x 1
  • IO Sensor Expansion Board V7.1 x 1
  • Serial port: USB to Serial x 1
  • Dupont wires

Software Preparation

Wiring Diagram

Use PC-side serial port to display the detected distance and power the entire system.

Other Preparation Work

Users can also use multi-serial port devices such as Arduino Leonardo and Arduino Mega2560. Define D12 and D13 as software serial port.

Sample Code

/*
  * @File  : DFRobot_TFmini_test.ino
  * @Brief : This example use TFmini to measure distance
  *         With initialization completed, we can get distance value and signal strength
  * @Copyright   [DFRobot](https://www.dfrobot.com), 2016
  *             GNU Lesser General Public License
  *
  * @version  V1.0
  * @date  2018-1-10
*/

#include <DFRobot_TFmini.h>

SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;

void setup(){
    Serial.begin(115200);
    TFmini.begin(mySerial);
}

void loop(){
    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        Serial.print("Distance = ");
        Serial.print(distance);
        Serial.println("cm");
        Serial.print("Strength = ");
        Serial.println(strength);
        delay(500);
    }
    delay(500);
}

Result

Serial port software shows the data format as below:

 Distance = 1000 mm
 Strength =  688

Was this article helpful?

TOP