TFMini - Micro LiDAR Module (Qwiic) Hookup Guide
Introduction
Heads up! This tutorial is for the Qwiic enabled TFMini. Serial data is output via I2C. If you are using the TFMini that outputs serial data via UART [ SEN-14588 ], please refer to the TFMini - Micro LiDAR Module Hookup Guide.
The TFMini is a ToF (Time of Flight) LiDAR sensor capable of measuring the distance to an object as close as 30 cm and as far as 12 meters! The TFMini allows you to integrate LiDAR into applications traditionally reserved for smaller sensors such as the SHARP GP-series infrared rangefinders. With the added Qwiic feature, you can quickly connect to the sensor via I2C! In this tutorial, you will learn how to connect to the TFMini using an Arduino microcontroller with the Qwiic system.
Required Materials
To follow along with this tutorial, you will need the following materials. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
Tools
Depending on your setup, you will need a soldering iron, solder, and general soldering accessories.
Suggested Reading
If you aren't familiar with the Qwiic system, we recommend reading here for an overview.
Qwiic Connect System |
We would also recommend taking a look at the following tutorials if you aren't familiar with them.
Using the Arduino Pro Mini 3.3V
Hardware Overview
The sensor works by sending a modulated near-infrared light out. The light that is reflected from the object returns to the sensor's receiver. The distance between the two can be converted using the sensor by calculating the time and phase difference. The distance measured may vary depending on the environment and the reflectivity of object.
Input Power
According to the datasheet (pg 1), the input voltage is 5V. In this tutorial, we will be using the included boost converter by applying a 3.3V input voltage from the Qwiic side to boost power to 5V on the TFMini side.
Make sure to use either the Beefy 3 with a 3.3V Arduino Pro Mini, RedBoard Qwiic, or the LD1117V33 3.3V voltage regulator to provide sufficient current for the TFMini. You may need to power the Qwiic TFMini with an external power supply.
Logic Levels
While the sensor can be powered at 5V, the I2C pins are only 3.3V logic. Make sure to use a logic level converter when reading the sensor with a 5V microcontroller.
Pinout
There is a marking next to the polarized connector to indicate the polarity as "J1" as indicated in the image below. This is useful when referencing sensor's pinout. As opposed to the original TFMini, the green and white wires for the Qwiic enabled TFMini uses an I2C serial. The default address of the TFMini is 0x10.
Pin Number | Wire Color | Qwiic TFMini Pinout | Wire Color |
---|---|---|---|
1 | Green | SCL (3.3V TTL) | Yellow |
2 | White | SDA (3.3V TTL) | Blue |
3 | Red | 5V | Red |
4 | Black | GND | Black |
Hardware Hookup
If you haven't yet assembled your 3.3V Pro Mini, now would be the time to head on over to that tutorial to solder the header pins. Once soldered, connect the power and I2C pins between the Arduino Pro Mini 3.3V/8MHz and Qwiic adapter.
Arduino Pins | Qwiic Adapter Pins | Wire Color |
---|---|---|
A5 (SCL) | SCL (3.3V TTL) | Yellow |
A4 (SDA) | SDA (3.3V TTL) | Blue |
3.3V | 3.3V | Red |
GND | GND | Black |
Then connect the Qwiic cable that was included in the Power Input side between the Qwiic adapter and the boost circuit. On the other side, insert the second cable that was included in the Power Output side between from the boost circuit to the TFMini. The connectors are different on each side of the boost converter so it should not be Finally, power the circuit up with a micro-B USB cable and the Beefy 3. The connection should look like the image below.
If you are using a SparkX BlackBoard or RedBoard Qwiic with a USB cable, try powering the boards with a 9V wall adapter in addition to USB. Don't worry, it is acceptable to connect both a barrel jack and a USB connector at the same time. The boards have power-control circuitry to automatically select the best power source.
Example Code
Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.
Grab a micro-B USB cable and connect the Arduino to your computer. Copy, paste, and upload the code below. Make sure to use the correct COM port and board selection.
language:c
/*
LidarTest.ino
Written in collaboration by Nate Seidle and Benewake
Example sketch for the Qwiic Enabled TFMini
(https://www.sparkfun.com/products/14786)
*/
#include <Wire.h>
uint16_t distance = 0; //distance
uint16_t strength = 0; // signal strength
uint8_t rangeType = 0; //range scale
/*Value range:
00 (short distance)
03 (intermediate distance)
07 (long distance) */
boolean valid_data = false; //ignore invalid ranging data
const byte sensor1 = 0x10; //TFMini I2C Address
void setup()
{
Wire.begin();
Serial.begin(115200);
Serial.println("TFMini I2C Test");
}
void loop()
{
if (readDistance(sensor1) == true)
{
if (valid_data == true) {
Serial.print("\tDist[");
Serial.print(distance);
Serial.print("]\tstrength[");
Serial.print(strength);
Serial.print("]\tmode[");
Serial.print(rangeType);
Serial.print("]");
Serial.println();
}
else {
//don't print invalid data
}
}
else {
Serial.println("Read fail");
}
delay(50); //Delay small amount between readings
}
//Write two bytes to a spot
boolean readDistance(uint8_t deviceAddress)
{
Wire.beginTransmission(deviceAddress);
Wire.write(0x01); //MSB
Wire.write(0x02); //LSB
Wire.write(7); //Data length: 7 bytes for distance data
if (Wire.endTransmission(false) != 0) {
return (false); //Sensor did not ACK
}
Wire.requestFrom(deviceAddress, (uint8_t)7); //Ask for 7 bytes
if (Wire.available())
{
for (uint8_t x = 0 ; x < 7 ; x++)
{
uint8_t incoming = Wire.read();
if (x == 0)
{
//Trigger done
if (incoming == 0x00)
{
//Serial.print("Data not valid: ");//for debugging
valid_data = false;
//return(false);
}
else if (incoming == 0x01)
{
Serial.print("Data valid: ");
valid_data = true;
}
}
else if (x == 2)
distance = incoming; //LSB of the distance value "Dist_L"
else if (x == 3)
distance |= incoming << 8; //MSB of the distance value "Dist_H"
else if (x == 4)
strength = incoming; //LSB of signal strength value
else if (x == 5)
strength |= incoming << 8; //MSB of signal strength value
else if (x == 6)
rangeType = incoming; //range scale
}
}
else
{
Serial.println("No wire data avail");
return (false);
}
return (true);
}
Once uploaded, try moving an object in front of the sensor to test. In the example below, a third hand was used to hold the TFMini when detecting an object at a certain distance away from the sensor. Since the sensor is not able to detect an object when less than 11.8 inches (or 30cm = 0.3m) away, the object under test was placed at 20 inches and 30 inches.
Qwiic Enabled TFMini Reading an Object at 20 Inches | Qwiic Enabled TFMini Reading an Object at 30 Inches |
Opening the serial monitor at 115200, you may see an output similar to the values printed below. Using a yard stick, the values responded as expected when moving an object between 30 inches and 20 inches. The Qwiic enabled TFMini indicated that the object was at an "intermediate distance".
TFMini I2C Test
Data valid: Dist[72] strength[274] mode[3]
Data valid: Dist[72] strength[275] mode[3]
Data valid: Dist[73] strength[267] mode[3]
Data valid: Dist[72] strength[265] mode[3]
Data valid: Dist[71] strength[275] mode[3]
Data valid: Dist[70] strength[284] mode[3]
Data valid: Dist[68] strength[305] mode[3]
Data valid: Dist[67] strength[311] mode[3]
Data valid: Dist[65] strength[329] mode[3]
Data valid: Dist[65] strength[335] mode[3]
Data valid: Dist[65] strength[341] mode[3]
Data valid: Dist[65] strength[361] mode[3]
Data valid: Dist[64] strength[367] mode[3]
Data valid: Dist[64] strength[383] mode[3]
Data valid: Dist[64] strength[387] mode[3]
Data valid: Dist[64] strength[386] mode[3]
Data valid: Dist[64] strength[385] mode[3]
Data valid: Dist[64] strength[384] mode[3]
Data valid: Dist[64] strength[386] mode[3]
Data valid: Dist[64] strength[394] mode[3]
Data valid: Dist[64] strength[398] mode[3]
Data valid: Dist[64] strength[400] mode[3]
Data valid: Dist[64] strength[402] mode[3]
Resources and Going Further
Now that you've successfully got your Quick enabled TFMini up and running, it's time to incorporate it into your own project! For more on the TFMini, check out the links below:
Need some inspiration for your next project? Check out some of these related tutorials:
Building an Autonomous Vehicle: The Batmobile
LIDAR-Lite v3 Hookup Guide
Qwiic Distance Sensor (RFD77402) Hookup Guide
Or check out this related blog post for ideas.