Example Code for Arduino-Black & White Line Detection

Last revision 2025/12/08

TF Mini laser ranging radar is an optical sensor, but any optical sensor, the sensitivity of the light will be higher. We can use this feature to achieve close-range black and white line detection, line-tracking inspection. (NOTE: black is to absorb all the lights, do not reflect; white is to reflect all the light, do not absorb) We use the signal strength 'Strength' to distinguish between the two colors.

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

TFmini Connection Diagram(Line-tracking) TFmini Plus Connection Diagram(Line-tracking)

Other Preparation Work

NOTE: The code needs to be used in concert with TF Mini Library.

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
        strength = TFmini.getStrength();       //Get signal strength data
        Serial.print("Strength = ");
        Serial.println(strength);
    }
}

Result

Downloaded the program, when you repeatedly measure white and black paper at the same distance, a signal strength changes could be seen through the serial port drawing tool.

TFmini Plus

Was this article helpful?

TOP