TF mini Multiple sensors on the Arduino Mega.

I have applied the arduino Mega 5V logic Tx pin to the TFmini 3.3V logic as when i looked at the wiring diagram on tehe Benewake instruction sheet they had it wired this way. they mentioned nothing og arduino Mega's 5V tx pin.

Have i damaged the sensor now? do i need to buy a new one?

It is likely that the sensor has been damaged, but you can always hope.

Many people use 5V to 3.3V bidirectional level shifters like this one.

Mitchell_Storrie:
I have applied the arduino Mega 5V logic Tx pin to the TFmini 3.3V logic as when i looked at the wiring diagram on tehe Benewake instruction sheet they had it wired this way. they mentioned nothing og arduino Mega's 5V tx pin.

Have i damaged the sensor now? do i need to buy a new one?

The TFMini takes 5V as its V+ but puts out 0ish to 3.3ish volts on tx/rx. I have used the TFMini on the Uno, MiniPro, Mega, Due, STM32 Bluepill, ESP32 supplying it with 5V and hooking it up to 5V and 3.3V devices without issue.

The Sparkfun TFmini instructional page features this VERY CLEAR WARNING:

Logic Levels

While the sensor can be powered at 5V, the serial UART pins are only 3.3V logic. Make sure to use a logic level converter when reading the sensor with a 5V microcontroller.

Disregarding this warning will damage the TFmini, and possibly also the 5V processor, but both may continue to function for a while.

Thanks Guys! the sensor is working fine. but i will be careful not to use the 5V. I do have logic level converters but found this site which specifies reading the data from the TFmini at 3.3V from the unit and simply displaying it. So i am thinking this might be a work around with the Mega.

I am trying to find some code and good turorials on how to do this with the mega but all I am finding is code for the UNO. I am fairly new to working with arduino. I got two readings working accross the UNO however when i try to do this on the MEGA i have no luck. I think it is something to do with SoftwareSerial not being used on the Mega.

Can someone please guide me onto a tutorial or code for connecting 2 TFmini's to the arduino Mega? Please do not point me towards UNO as these will not work with the Mega.

I have read over one code however I keep getting the error function with HardwareSerial and would rather just do this using the generic library.

This is for my thesis and i would be so greatful for any help. I am trying to learn allot about UART interfacing with arduino Mega at once.

#include <Wire.h>

//Lidar data
int dist;//actual distance measurements of LiDAR
int strength;//signal strength of LiDAR
int check;//save check value
int i;
int uart[9];//save data measured by LiDAR
const int HEADER=0x59;//frame header of data package

void setup() {
Serial.begin(9600);//set bit rate of serial port connecting Arduino with computer
Serial2.begin(115200);//set bit rate of serial port connecting LiDAR with Arduino

}

void loop()
{
if (Serial2.available())//check if serial port has data input
{
if(Serial2.read()==HEADER)//assess data package frame header 0x59
{
uart[0]=HEADER;
if(Serial2.read()==HEADER)//assess data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)//save data in array
{
uart*=Serial2.read();*

  • }*
  • check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];*
  • if(uart[8]==(check&0xff))//verify the received data as per protocol*
  • {*
    _ dist=uart[2]+uart[3]*256;//calculate distance value_
    _ strength=uart[4]+uart[5]*256;//calculate signal strength value_
  • if(dist>=30 && dist<1200)*
  • {*
  • Serial.print(dist);//output measure distance value of LiDAR, z-coordinate*
  • Serial.print("\n");*
  • Serial.flush();*
  • }*
  • }*
  • }*
  • }*
  • }*
    }[/quote]

Really struggling here to find any working code for this on the arduino Mega please help guys. I have tried coding it myself but it is just not working.

For informed help, please read and follow the directions in the "How to use this forum" post.

Here is some basic code to get data from the TFMini:

#include <HardwareSerial.h>
HardwareSerial SerialTFMini( 1 );
// serial(1) = pin27=RX, pin26=TX
// serial(2) = pin16=RX green, pin17=TX white




void getTFminiData(int* distance, int* strength) {
  static char i = 0;
  char j = 0;
  int checksum = 0; 
  static int rx[9];
  if(SerialTFMini.available())
  {  
    // Serial.println( "tfmini serial available" );
    rx[i] = SerialTFMini.read();
    if(rx[0] != 0x59) {
      i = 0;
    } else if(i == 1 && rx[1] != 0x59) {
      i = 0;
    } else if(i == 8) {
      for(j = 0; j < 8; j++) {
        checksum += rx[j];
      }
      if(rx[8] == (checksum % 256)) {
        *distance = rx[2] + rx[3] * 256;
        *strength = rx[4] + rx[5] * 256;
      }
      i = 0;
    } else 
    {
      i++;
    } 
  }  
}


void setup() {
  Serial.begin(115200);
  
SerialTFMini.begin( 115200, SERIAL_8N1, 27, 26 );

}

void loop() 
{
  int distance = 0;
  int strength = 0;

  getTFminiData(&distance, &strength);
  while(!distance) {
    getTFminiData(&distance, &strength);
    if(distance) {
      Serial.print(distance);
      Serial.print("cm\t");
      Serial.print("strength: ");
      Serial.println(strength);
    }
  }

delay(100);
}

The code was orginally written to work on a UNO, I have used the code to test the Uno, Mega, Due, STM32, and ESP32. You'll have to change the serial stuff to match your uController.

I, also, have code that will do single trigger but its no use to post that if you cannot get the basic TFMini to work.

Post your well formatted code in code tags so it can be looked over.

Can someone please guide me onto a tutorial or code for connecting 2 TFmini's to the arduino Mega?

What's the problem? The Mega has 4 hardware serial interfaces so it's very easy. You just have to ensure you only connect the TX signal of the TFMini to the RX of the Mega. Do not connect TX of the Mega to the TFMini's RX as that might destroy the sensor. This assumes you use the UART version of the TFMini (you failed to provide a link to the product!), the I2C version is connected differently.

Please do not cross post.

Topics merged.

You were asked to read the forum guidelines.
Further cross posting could result in loss of forum privileges.

Please read that link and proceed with caution.

Bob.

void getTFminiData(int* distance, int* strength) {
  static char i = 0;
  char j = 0;
  int checksum = 0; 
  static int rx[9];
  if(Serial1.available()) //<<<< SEE SERIAL PORT CHANGED TO 1
  {  
    // Serial.println( "tfmini serial available" );
    rx[i] = Serial1.read(); //<<<< SEE SERIAL PORT CHANGED TO 1
    if(rx[0] != 0x59) {
      i = 0;
    } else if(i == 1 && rx[1] != 0x59) {
      i = 0;
    } else if(i == 8) {
      for(j = 0; j < 8; j++) {
        checksum += rx[j];
      }
      if(rx[8] == (checksum % 256)) {
        *distance = rx[2] + rx[3] * 256;
        *strength = rx[4] + rx[5] * 256;
      }
      i = 0;
    } else 
    {
      i++;
    } 
  }  
}

You really could not figure out how to make the above change? You should fail.

I have done 2 arduino projects before this. My mainstay is C Matlab, Inventor, FEA, very new to this stuff, I am also a SCADA engineer. literally learning this stuff before i interface the sensors in LABview. I have tried the following code. I think i have most of it right I just need to initiate the read which I am struggling to grasp the concept of. If you could help me out i would be most grateful.

Also I am new to the forums. My code compiles I am just missing something to initiate the reads. Code below.

#include <Wire.h>

int dist;//actual distance measurements of LiDAR
int strength;//signal strength of LiDAR
int check;//save check value
int i;
int uart[9];//save data measured by LiDAR
const int HEADER=0x59;//frame header of data package


void lidarData();

void setup() {
  Serial.begin(9600);//set bit rate of serial port connecting Arduino with computer
  Serial2.begin(115200);//set bit rate of serial port connecting LiDAR with Arduino
    
}

void loop(){

  
 if (Serial2.available()>0)//check if serial port has data input also greater than 0 
  {
    if(Serial2.read()==HEADER)//assess data package frame header 0x59
    {
      uart[0]=HEADER;
      if(Serial2.read()==HEADER)//assess data package frame header 0x59
      {
        uart[1]=HEADER;
        for(i=2;i<9;i++)//save data in array
        {
          uart[i]=Serial2.read();
        }
        check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
        if(uart[8]==(check&0xff))//verify the received data as per protocol
        {
          dist=uart[2]+uart[3]*256;//calculate distance value
          strength=uart[4]+uart[5]*256;//calculate signal strength value
          if(dist>=30 && dist<1200)
          {
             Serial.print(dist);//output measure distance value of LiDAR, z-coordinate
             Serial.print("\n");
             Serial.flush();
          }
        }
      }
    }
  }
}//https://www.youtube.com/watch?v=ts81ZTdY_DQ