Lidar shows garbage with arduino nano

When I am trying to interface Tf-mini lidar with Arduino nano by using Tf-mini example code by Peter Jansen.
On the serial monitor, it shows the correct distance for the first time but after some time it shows the wrong distance i.e. garbage on the serial monitor.

Code is given below:-

#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(50);
}

Anyone, please help me to solve this problem.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose “copy for forum”
  3. paste clipboard into write-window of a posting

Executing the next call for getDistance() every 0,05 seconds is pretty fast.

what happends if you slow it down from

delay(50);

// to 

delay(1000); // ?

What does the datasheet say how fast you can do readings?

To what power-supply is the Lidar-sensor connected?

best regards Stefan

A recognized finding for SoftwareSerial: The library runs reliably up to 38400 bps. You operate SoftwareSerial with 115200 bps. You are lucky when some values arrive.
I changed the bit rate of the lidar with the software tool to 57600 bps and used an optimized (!) Version of the SoftwareSerial. It works pretty well with that. But not sure with the “normal” SoftwareSerial. One piece of advice: get a more powerful processor!

Which type of optimized version is used by you?
Or, What you optimized in it ?
Can you please share this with me?

Hello, with a late answer you get my library “” with an expample sketch. the Lidar is connected with 74880 bps! This is not the full speed of 115200 bps, but fast enough.
The zip-file should be importable with the Arduino IDE.
SerialHSP.zip (8.4 KB)