Sensor won't function properly if other devices are connected to the arduino

Hello there.

For my project I am using a mega 2560, a tf mini lidar sensor, and an HC-05 bluetooth module.
I’m trying to calculate distance and speed of an incoming object using the tf mini and then outputting, with the help of the HC-05 module, the values on a app that I made using MIT App inventor.

My problem is that when I connect the HC-05 to the system, the tf mini starts outputting the value 6553. When I connect the tf mini and disconnect the HC-05, and monitor the serial monitor I get the proper distance and speed values.

Any advice would be highly appreciated!!

My code:

#include <DFRobot_TFmini.h>    //TF mini library 

SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;

long distance1,distance2,distance3,distance4,distance5,velo,velo1,velo2,velo3,velo4;

void setup(){
    Serial.begin(115200);
    TFmini.begin(mySerial);
}

void loop(){
    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        
       
        distance1 = distance;
        delay(50);



    }
    delay(50);




    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        
       
        distance2 = distance;

        velo1 = ((distance1 - distance2) / (0.1))*0.036;


        delay(100);

    }

    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        
       
        distance3 = distance;


        velo2 = ((distance2 - distance3) / (0.1))*0.036;

        delay(100);

    }


    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        
        
        distance4 = distance;


        velo3 = ((distance3 - distance4) / (0.1))*0.036;
        delay(100);

    }
    

    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        
        Serial.print(distance);
        Serial.print(" cm");
        Serial.print("|");
        distance5 = distance;


        velo4 = ((distance4 - distance5) / (0.1))*0.036;
        velo = (velo1 + velo2 + velo3 + velo4) / 4;
        
        Serial.print(velo);
        Serial.print(" kph");
        Serial.print("|");

        delay(100);

    }
    
    
    delay(50);

}
SoftwareSerial mySerial(12, 13); // RX, TX

The Mega has 4 (in words: four) hardware serial interfaces, so using SoftwareSerial on that device is not very clever.

As you get problems when you connect the HC-05 (you didn't write you changed anything in the sketch) you have to provide a complete wiring diagram of your setup and links to the used hardware.

Power supply problems.

pylon:

SoftwareSerial mySerial(12, 13); // RX, TX

The Mega has 4 (in words: four) hardware serial interfaces, so using SoftwareSerial on that device is not very clever.

As you get problems when you connect the HC-05 (you didn't write you changed anything in the sketch) you have to provide a complete wiring diagram of your setup and links to the used hardware.

This is how I have the HC-05 connected:

Then I just connect the TF mini to pins 12 & 13

This is the data sheet for the TF mini:

Don’t connect anything to pin 0 and 1.
Pin 0,1 (RX0, TX0) are already used internally by the USB<>Serial chip.

Don’t use SoftwareSerial with a Mega.

You have three other serial pairs (pin 14-19).
Use one of those pairs for the BT module, and another pair for the Lidar.
The multiSerial example in the IDE will show you how to use those pairs.
Leo…