Cannot get data from TFMini LiDAR sensor

Continuing the discussion from Nano Every issues:

I installed MegaCoreX and my code compiles and uploads with no errors but I cannot get the TFMini Lidar sensor to show anything in serial monitor. I'm using the USB cable to power the Nano Every.

#include <SoftwareSerial.h>   //header file of software serial port
SoftwareSerial Serial5(2, 3); //define software serial port name as Serial5 and define pin2 as RX (white) & pin3 (green) as TX
 
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

// Define the pins for the LEDs

const int greenLED = 8;
const int redLED = 12;
 
void setup()
{
  Serial.begin(9600);         //set bit rate of serial port connecting Arduino with computer
  Serial5.begin(115200);      //set bit rate of serial port connecting LiDAR with Arduino

   // Initialize the LED pins as outputs
  
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
}

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

        // Control the LEDs based on the distance

        if(dist < 15)
        {
          //Light the red LED
          digitalWrite(redLED, HIGH);
          digitalWrite(greenLED, LOW);
        } 
        else if (dist >= 16 && dist <= 76) 
        {
        // Light the green LED
          digitalWrite(greenLED, HIGH);
          digitalWrite(redLED, LOW);
        } 
        //else if (dist >= 31 && dist <= 76) 
        //{
        // Light the green LED
          //digitalWrite(greenLED, HIGH);
          //digitalWrite(redLED, LOW);
        //} 
        else if (dist > 76) 
        {
        // Light the red LED
          digitalWrite(greenLED, LOW);
          digitalWrite(redLED, HIGH);
        } 
        else 
        {
        // Turn off all LEDs if the distance is not within any range
          digitalWrite(greenLED, LOW);
          digitalWrite(redLED, LOW);
    }

        }
      }
    }
  }
}
    

On an Every, you have the possibility of 4 hardware Serial. Serial, Serial1, Serial2, Serial3. Why are you attempting software serial? Might as well use a plain vanilla clone Nano. I have no idea if software serial works on the Every, but it most certainly will not work at baud rates beyond about 38400. Doesn't on the Classic, either.
Suggest you try 9600 to begin with, but also consider the other 3 hardware ports instead of messing around with SW serial.
This might help:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.