Has any one successfully connected a TFMini-Plus Lidar to a Z-Uno. If so, would you be willing to share your code.
Thank you,
TFMini-Plus Lidar
Re: TFMini-Plus Lidar
I will answer my own question in case someone else wants to use this device. Below is the code I used.
So far I have found the device to measure distance very accurately.... but I still need to test in the real world.
// connect the TFMini Plus to the Z-Uno: Red to 5v, Black to ground, Green to either RX0 for Serial0 or RX1 for Serial1
So far I have found the device to measure distance very accurately.... but I still need to test in the real world.
// connect the TFMini Plus to the Z-Uno: Red to 5v, Black to ground, Green to either RX0 for Serial0 or RX1 for Serial1
Code: Select all
int distance = 0; // centimeters
int getMiniData() {
int i = 0;
int pk = 0;
int rx[9];
int cnt = 0;
if(Serial0.available() > 0) {
do {
cnt = cnt + 1;
rx[0] = Serial0.read(); // look for a 0x59
pk = Serial0.peek(); // look ahead 1 byte for another 0x59
if(rx[0] == 0x59 && pk == 0x59){ // if we have 2 consecutive 0x59 then read in the rest of the bytes
for(i = 1; i < 9; i++){
rx[i] = Serial0.read();
}
return rx[2] + (rx[3] * 256); // calculate the distance in CM
// we could also get "strength" and "temp" see: https://www.mybotshop.de/Datasheet/TFmini_Plus_Datasheet.pdf
// and we could check the last byte which is a checksum... not bothering to do that.
}
} while (cnt < 1000); // we do not want to get stuck here forever
} // serial.available
return -1;
}
void setup() {
Serial0.begin(115200);
}
void loop() {
distance = getMiniData();
Serial.println(distance);
}