Example Code for Arduino-Black and White Line Detection
Last revision 2026/06/05
The TF Mini laser ranging sensor is susceptible to surface reflectivity, making it suitable for close-range black-and-white line detection. By comparing the TF03¡¯s signal-strength output, the system can easily differentiate white (high reflectivity) from black (low reflectivity).
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- USB to Serial Cable x 1
- Jumper wires
Software Preparation
- Arduino IDE
- Click to download DFRobot TF Mini Library
Wiring Diagram
Sample Code
The code needs to be used with the TF Mini library.
/*
* @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
After uploading the program, place white and black paper at the same distance within the sensor¡¯s detection range. The signal strength values will then be displayed in the Serial Plotter interface, allowing you to compare the reflectivity difference between the two colors.
Was this article helpful?
