Teensy 4.0 w/ TF Mini LIDAR issue

cbennett

Member
Hi all, I am having issues getting a Benewake TF mini lidar unit to work on a teensy 4.0. I am using the library https://github.com/opensensinglab/tfmini . The following code works just fine on an arduino uno but I cant get this code to work on the teensy. I tried pins 0 and 1 on the teensy and putting said changes in the code as well to no avail. All i see on the serial monitor is "initializing...". Is this a library issue? Any guidance in getting this Lidar to work on the teensy is much appreciated. Thank you all for your time.

#include <SoftwareSerial.h>
#include "TFMini.h"
SoftwareSerial mySerial(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

void setup() {
Serial.begin(115200);
Serial.println ("Initializing...");
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial);
}


void loop() {
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);

delay(25);
}
 
Not sure if this page is current: pjrc.com/teensy/td_libs_SoftwareSerial.html

From that page … any better with ::
Code:
#define rxPin 0
#define txPin 1

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
// ...
 
When you said you tried to use pins 0 and 1, were you still trying to use SoftwareSerial? Also not sure software serial has been updated to work with T4.0 yet. However with that said you would probably be better off using one of H/W serial ports. So, for instance if you want to use Serial1 (pins 0 and 1):
Code:
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;

#define mySerial  Serial1

void setup() {
Serial.begin(115200);
Serial.println ("Initializing...");
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial); 
}
 
As mentioned, if you are free to use other pins.
Example pin 0, 1 than you should use the hardware serial port. As @mjs513 mentioned would be Serial1

If you have fixed pins like pins 10 and 11, than you might be able to use my FlexIO stuff, where I have Serial port emulation using Flex IO pins.
 
I put the rx and tx on the respective 0 and 1 pins and included the code you recommend. On the serial monitor it did not even display "initializing".

Code:
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;
#define mySerial Serial1


void setup() {

Serial.begin(115200);
Serial.println ("Initializing...");
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial); 

}

void loop() {
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength);

  // Wait some short time before taking the next measurement
  delay(25);  
}
 
Not sure if this page is current: pjrc.com/teensy/td_libs_SoftwareSerial.html

From that page … any better with ::
Code:
#define rxPin 0
#define txPin 1

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
// ...

I put the tx and rx on the respective pins and tried your code with...
Code:
#include <SoftwareSerial.h>
#include "TFMini.h"
#define rxPin 0
#define txPin 1
TFMini tfmini;

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup() {

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(115200);
Serial.println ("Initializing...");
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial); 

}

void loop() {
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength);

  // Wait some short time before taking the next measurement
  delay(25);  
}
I couldn't get anything on serial monitor.
 
Again as suggested, you might want to do it more like:
Code:
#include "TFMini.h"
TFMini tfmini;

void setup() {
  // wait up to 4 seconds for serial monitor to be valid
  while (!Serial && millis() < 4000) ; 
  Serial.begin(115200); 
  Serial.println ("Initializing...");
  Serial1.begin(TFMINI_BAUDRATE);
  tfmini.begin(&Serial1); 

}

void loop() {
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength);

  // Wait some short time before taking the next measurement
  delay(25);  
}

Note: the added while (!Serial...
It waits up to 4 seconds for the USB serial object to be initialized and connected to the pc before it continues. Unlike Arduinos like UNOs which have a separate processor (or interface chip) that does USB, the Teensy itself does serial (like Arduino Leonardo), so if you print something out at startup time before your PC has registered the usb object you might miss the output.

Also note, I am using the Serial1 hardware setup and not the Software Serial code.
 
White wire to pin 0 (RX1) , green wire to pin 1 (TX1) , red wire to 5 volt Vin, black wire to GND next to VIN.

14588-TFMini_-_Micro_Infrared_Module-03_Pinout_small.png
 
I was not aware that the teensy itself does serial. I copied your code verbatim. The serial monitor didn't print the "initializing" and is blank.

Appreciate all of your help. Relatively new to Arduino and completely new to teensy.
 
I was not aware that the teensy itself does serial. I copied your code verbatim. The serial monitor didn't print the "initializing" and is blank.

Appreciate all of your help. Relatively new to Arduino and completely new to teensy.

Indeed Teensy LC, 3.2, 3.5 and 3.6 and 4.0 all have 3 or more native Serial UART ports. As noted in the P#2 'out of date link':

The hardware UART Serial port should always be used, if possible, before considering SoftwareSerial. On Teensy, the hardware UART serial port completely separate from the USB port.

Some Arduino software seems to still rely on or require software Serial - but where possible using the Native Serial hardware results in better performance with less processor overhead, especially on those ports with a FIFO.

When using Serial the Rx on one side must be connected to the Tx on the other side in both directions and they must share a common GND connection. And the UART signals are delivered and expected from Teensy at 3.3 volts - and on LC, and 3.6 and 4.0 voltage must be limited to 3.3V.
 
I have written c++ code that reads from 4 x tf-minis:
 
Back
Top