Multiple tf mini LIDAR sensors on arduino problem

HI, I am a beginner with the whole LIDAR sensing technology and software serial on Arduino. Essentially I hooked up one tf mini to my Arduino using a logic level converter, and it worked splendidly. I then attempted to add a second sensor. But when I tried to get a reading from either of the sensors only one showed up in the serial monitor. Through testing I relized whatever LIDAR sensor serial begin went last gave readings. I am using an Arduino Uno, 5v 3.3v bidirectional logic level converter. This is the original code with one LIDAR:

#include <SoftwareSerial.h>
#include "TFMini.h"

// Setup software serial port 
SoftwareSerial mySerial(10, 11);      // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

void setup() {
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  mySerial.begin(TFMINI_BAUDRATE);

  // Step 3: Initialize the TF Mini sensor
  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 then tried to modify for 2 LIDAR. This is my Code:

#include <SoftwareSerial.h>
#include "TFMini.h"

// Setup software serial port 
SoftwareSerial mySerial(10, 11);      // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
SoftwareSerial mySerial2(12, 13);
TFMini tfmini;
TFMini tfmini2;
void setup() {
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  mySerial2.begin(TFMINI_BAUDRATE);
  mySerial.begin(TFMINI_BAUDRATE);
  // Step 3: Initialize the TF Mini sensor
  tfmini2.begin(&mySerial);
  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(5000);  

  uint16_t dist2 = tfmini2.getDistance();
  uint16_t strength2 = tfmini2.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist2);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength2);
  delay(5000);
}

Can anyone help with this problem.
Thanks!

Hey,

After a quick first glance I noticed you may want to verify your initialization line:

// Step 3: Initialize the TF Mini sensor
tfmini2.begin(&amp;mySerial);
tfmini.begin(&amp;mySerial);

It seems like you are not using the mySerial2 that you created above.
Try changing that and see if it helps out.

Sincerely,

P.-S.: Would you mind also linking to the library you are using? Does it happen to be this one? If so, which version are you using?
P.-S.#2: I added code tags to your post for clarity. I hope you don’t mind! :wink:

Thanks very much. I managed to fix the issue by calling the listen() function before retrieving data. I also made the corrections that you listed. Thanks for the help. The library am using is the TFmini .h library.

Hey,

That was going to be my next request of stuff to try out :slight_smile:

Good to know it works now. Enjoy the sensor!

Sincerely,

Can you share me the code of two lidar with single arduino?

Hey @spiderpro it’be great if you could post your updated program :slight_smile:
Thanks!