TFMini Serial Communication MKR WiFi 1010

Hello,

I am new to Arduino and coding in general but have found some issues while trying to program a TFMini to measure distance on an MKR Wifi 1010.

Using this code below (Intended for an Arduino Uno)


#include <SoftwareSerial.h>  //header file of software serial port

SoftwareSerial Serial1(2,3); //define software serial port name as Serial1 and define pin2 as RX and pin3 as TX

/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and directly use Serial1 serial port*/

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
	Serial1.begin(115200);	//set bit rate of serial port connecting LiDAR with Arduino
}

void loop() { 
	if (Serial1.available()) {	//check if serial port has data input
		if(Serial1.read() == HEADER) {	//assess data package frame header 0x59
			uart[0]=HEADER;
			if (Serial1.read() == HEADER) { //assess data package frame header 0x59
				uart[1] = HEADER;
				for (i = 2; i < 9; i++) { //save data in array
					uart[i] = Serial1.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
					Serial.print("dist = ");
					Serial.print(dist); //output measure distance value of LiDAR
					Serial.print('\t');
					Serial.print("strength = ");
					Serial.print(strength); //output signal strength value
					Serial.print('\n');
				}
			}
		}
	}
}

I replaced the pins 2 and 3 to 13 and 14 to match the RX, TX in the board but still do not get it to work. I am using an online editor instead of the Desktop App.

I greatly appreciate your knowledge on this

The Uno runs at 16 MHz clock speed. I know the Serial() function can support the 115,200 baud rate, but maybe that is difficult for the software serial. Can you try a lower baud rate?

jrdoner

I think it needs to work at that Baud Rate.

SoftwareSerial is unreliable above 38400