Seeking Guidance for UART Communication on Argon or Photon2

Hello everyone,

I hope this message finds you well. I'm currently working on a project involving the Argon or Photon2 microcontrollers and the TF Luna LiDAR sensor. My aim is to utilize UART communication to process data from the TF Luna.

However, I've encountered an issue regarding compatibility with the ParticleSoftSerial library, which is not compatible with the devices we're using. As an alternative, I've attempted to implement UART communication using SoftwareSerial, but I'm facing some challenges in getting it to work effectively.

Below is the code snippet I've been working with:

#include <SoftwareSerial.h>    // Header file for software serial port
SoftwareSerial Serial1(2, 3);  // Define software serial port name as Serial1 and define pin2 as RX and pin3 as TX
/* For Arduino boards with multiple serial ports like the DUE board, interpret the above two pieces of code and directly use Serial1 serial port */

int dist;       // Actual distance measurements of LiDAR
int strength;   // Signal strength of LiDAR
float temperature;
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
          temperature = uart[6] + uart[7] * 256;  // Calculate chip temperature
          temperature = temperature / 8 - 256;
          Serial.print("dist = ");
          Serial.print(dist);  // Output measured distance value of LiDAR
          Serial.print('\t');
          Serial.print("strength = ");
          Serial.print(strength);  // Output signal strength value
          Serial.print("\t Chip Temperature = ");
          Serial.print(temperature);
          Serial.println(" Celsius degree");  // Output chip temperature of LiDAR
        }
      }
    }
  }
}

I would greatly appreciate any guidance or suggestions on how to effectively implement UART communication for this setup. If anyone has experience with similar projects or has insights into alternative approaches, your input would be invaluable.

Thank you in advance for your assistance.

Best regards,
Nayel

Software serial is not possible on the Argon or Photon 2; you will need to use a hardware UART, such as the MCU hardware UART (Serial1).

On the Photon 2, there is an additional hardware UART (Serial2) on pins D4 and D5.

Another option is to add a UART by SPI or I2C. The SC16IS7xxRK library works well on both the Argon and P2/Photon 2. There are many inexpensive breakout boards with this chip available on Amazon, eBay, and AliExpress.

Hi @rickkas7,

Thanks for the quick answer, do you have a piece of code that utiilize the serial1 for exemple for the communication between an MCU and a sensor please? Or if you have any guide that explains how to configure an utilize it?

Thank you for your time and your assistance.

Best regards,
Nayel

Using hardware Serial1 you can use the code you have, just remove the two lines containing SoftwareSerial and instead connect to the TX and RX pins. Note that TX is out of the Argon/Photon 2, so it's connected to the RX pin of your sensor.

2 Likes

Hi @rickkas7;

I did the modification that you gave me but I'm getting those errors :

 lib/SparkIntervalTimer/SparkIntervalTimer.h:37:4: #error "*** PARTICLE device not supported by this library. PLATFORM should be Core or Photon ***"
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:110:4: #error "*** PARTICLE device not supported by this library. PLATFORM should be Core or Photon ***"
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:95:26: 'NUM_SIT' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:96:23: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:96:53: 'TIMid' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:97:20: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:103:45: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:103:75: 'TIMid' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:140:39: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:146:39: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:146:69: 'TIMid' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:154:23: 'intPeriod' has not been declared
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:157:37: 'NUM_SIT' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:91:45: 'SYSCORECLOCK' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:92:45: 'SYSCORECLOCK' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:109:20: 'NUM_SIT' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:110:3: 'SIT_used' was not declared in this scope; did you mean 'SIT_id'?
error
lib/SparkIntervalTimer/SparkIntervalTimer.h:143:50: 'AUTO' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:30:6: 'bool IntervalTimer::SIT_used []' is not a static data member of 'class IntervalTimer'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:31:28: 'void (* IntervalTimer::SIT_CALLBACK [])()' is not a static data member of 'class IntervalTimer'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:6: 'bool IntervalTimer::beginCycles' is not a static data member of 'class IntervalTimer'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:40: 'isrCallback' was not declared in this scope; did you mean 'LedCallbacks'?
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:56: 'intPeriod' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:74: expected primary-expression before 'bool'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:86: 'TIMid' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:124:94: expression list treated as compound expression in initializer [-fpermissive]
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:158:6: 'bool IntervalTimer::allocate_SIT' is not a static data member of 'class IntervalTimer'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:158:34: 'intPeriod' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:158:52: expected primary-expression before 'bool'
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:158:64: 'TIMid' was not declared in this scope
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:158:72: expression list treated as compound expression in initializer [-fpermissive]
error
lib/SparkIntervalTimer/SparkIntervalTimer.cpp:190:6: variable or field 'start_SIT' declared void

I do think that I have did something but I'm not sure where, could you guide me please?

Here is the code :

int dist;      // actual distance measurements of LiDAR
int strength;  // signal strength of LiDAR
float temperature;
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
         temperature = uart[6] + uart[7] * 256;  // calculate chip temperature
         temperature = temperature / 8 - 256;
         Serial.print("dist = ");
         Serial.print(dist);  // output measured distance value of LiDAR
         Serial.print('\t');

         Serial.print("strength = ");
         Serial.print(strength);  // output signal strength value
         Serial.print("\t Chip Temperature = ");
         Serial.print(temperature);
         Serial.println(" Celsius degree");  // output chip temperature of LiDAR
       }
     }
   }
 }
}

Thanks in advance !

Best regards,
Nayel

You still have remnants of the SofltwareSerial ilibrary.

If you are using Workbench, delete the contents of the lib directory if there is one in the top level of your project, and also remove it from project.properties.

If you are using Web IDE, remove it from the list of included libraries for your project.

Hi @rickkas7,

It works ! Thank you very much for you help !

Best regards,
Nayel