Xiao Nrf52840 UART with NRF Connect SDK and Zephyr

I’m attempting to utilize the RX and TX pins on the nRF52840 XIAO as UART pins to transmit data from the XIAO to a host MCU. However, I’m encountering issues with this setup.

Could you kindly provide guidance on how to use the UART functionality on the XIAO using the NRF Connect SDK and Zephyr?

Thank you for your assistance.

Hi M-Rafay_Shams,
I’m using this sketch successfully with a TTL level device connected to the TX & RX pins on the Expansion board. You may be able to use this a starting point It basically starts and looks for a “<” to begin , sending a “N” or a “D” command to the device and it responds with the strings listed, I parsed them for what I needed, “Serial1” is the port for those pins on the Xiao 6 & 7. I have some pictures of the connections on here in other posts.

//----------------------------------------------------------------------------------------------
//Get DUT data
//Board Library : Seeed nRF52 mbed-enable Borads 2.9.1
//Board Select  : Seeed nRF52 mbed-enable Borads / Seeed XIAO BLE - (nRF52840)
//<
//N
// NS8135-0201ORafael Farinas                 LPN135M135UMT92  B8.02 
//D
//
//D0305040T1879t 16+5Z0000H0042278N0037055F9999999V010.5E+073z0000S8135-0201 
//
//
//----------------------------------------------------------------------------------------------
#include <Arduino.h>
#include <U8x8lib.h>
#define LED LEDG

//Create a instance of class
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/PIN_WIRE_SCL, /* data=*/PIN_WIRE_SDA, /* reset=*/U8X8_PIN_NONE);  // OLEDs without Reset of the Display

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
int ndx = 0;
//boolean lelsPower = false;
int lelsReady = 0;
//char string = [20];

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  while (!Serial);
  delay(3000);//relax...Get Ready for serial port
  Serial.println();
  Serial.println("Power ON \n ");  // Let's BEGIN!!
  Serial.println("Test program" __FILE__ " compiled on " __DATE__ " at " __TIME__);
  Serial.println();
  Serial.println("Processor came out of reset.");
  //Serial1.println();
  u8x8.begin();
  u8x8.setFlipMode(1);  // set number from 1 to 3, the screen word will rotary 180
  u8x8.setFont(u8x8_font_8x13B_1x2_r);
  u8x8.clearDisplay();
  u8x8.setCursor(0, 0);
  u8x8.print("Power ON ");
  //delay(3000); 
  Serial.println("Serial initialized.");
  Serial.println("Serial1 initialized.");

  // Wait for lels to power on
  delay(200);
checkLelsStartup();
//lelsPower();

}

void loop() {
   
 if (!lelsReady) {
     Serial.println("DUT is powered OFF");
    // wait for user input
  Serial.println("Press any key to continue...");
  while (!Serial.available()) {
    // do nothing
  }
  // read and discard the user input
  Serial.read();
    //lelsPower();

  } else {
    Serial.println("DUT is Powered ON!.");
       // Send "N" command to lels
    Serial1.write(0x4E);    // Send hex 4E command
    Serial1.write(0x0D);    // Send hex 0D command
    delay(150);              // Wait for response
    u8x8.print("LeL ON ");
    // Check for response
    String response = "";
    while (Serial1.available()) {
     char c = Serial1.read();
    response += c;
    if (c == 0x0D) {      // Check for carriage return
      delay(10);          // Wait for line feed
      response += Serial1.read();  // Read line feed character
      break;
      }
    
  }

  Serial.print("LeLs Fixed Data:"); // Print response to serial monitor

  // Split response into tags
  String tags[9] = {"S", "O", "L", "N", "M", "U", "T"," ","B"};
  int tagIndex = 0;
  for (int i = 0; i < response.length(); i++) {
    if (response.charAt(i) == tags[tagIndex].charAt(0)) {
      Serial.print(tags[tagIndex] + ": ");
      tagIndex++;
    }
    Serial.print(response.charAt(i));
    if (tagIndex >= 10) {
      break;
    }
    if (response.charAt(i) == ' ') {
      Serial.print("\t");
    }
  }

  Serial.println();
response.trim();
printTags1(response);

  // Wait for 20 seconds before sending "D" command
  delay(1000);

  sendThed();

  Serial.print("LeLs Variable Data:"); // Print response to serial monitor

  // Split response into tags
  String varTags[14] = {"D", "T", "z", "t", "F", "N", "M", "S", "V", "E", "H", "U", "Z", "O"};
  tagIndex = 0;
  for (int i = 0; i < response.length(); i++) {
    if (response.charAt(i) == varTags[tagIndex].charAt(0)) {
      Serial.print(varTags[tagIndex] + ": ");
      tagIndex++;
    }
    Serial.print(response.charAt(i));
    if (tagIndex >= 14) {
      break;
    }
    if (response.charAt(i) == ' ');
  }
  response.trim();
printTags(response);
  }
  delay(30000);
}


//========================functions===============

String printTags(String response) {
  // Extract the relevant fields from the response
  String s = response.substring(1, 10);
  String o = response.substring(10, 41);
  String l = response.substring(41, 42);
  String n = response.substring(42, 45);
  String m = response.substring(45, 48);
  String u = response.substring(48, 49);
  String t = response.substring(49, 51);
  String b = response.substring(54, 56);

  // Construct the formatted string
  String data2 = "Pre-Defined Set of Calculated Data \n";
  data2 += "  S:" + s + "\n";
  data2 += "  O:" + o + "\n";
  data2 += "  L:" + l + "\n";
  data2 += "  N:" + n + "\n";
  data2 += "  M:" + m + "\n";
  data2 += "  U:" + u + "\n";
  data2 += "  T:" + t + "\n";
  data2 += "  B:" + b + "\n";

  return data2;
}

void sendThed(){         // Send "D" command to lels
  
  Serial1.write(0x44);    // Send hex 44 command
  Serial1.write(0x0D);    // Send hex 0D command
  delay(150);              // Wait for response

  // Check for response
  String response = "";
  while (Serial1.available()) {
    char c = Serial1.read();
    response += c;
    if (c == 0x0D) {      // Check for carriage return
      delay(10);          // Wait for line feed
      response += Serial1.read();  // Read line feed character
      break;
    }
  }

Serial.println(response);  // Print response to serial monitor
String data2 = printTags(response); // two response strings
Serial.println(data2);  // Print formatted data to serial monitor


}

bool checkLelsStartup() {
  Serial.println("Checking Lels Communication");
   while (Serial1.available()) {
    char c = Serial1.read();
    if (c == '<') {
      delay(10);
      char next1 = Serial1.read();
      char next2 = Serial1.read();
     if (next1 == 0x0A && next2 == 0x0D) {
        Serial.println("recieved power up");
        
      lelsReady = 1;
        return true;
      }
    }
  }
/*
  // If we didn't find the powerup string, wait for baud rate change and try again
  Serial.println("Checking Lels Communication Low Speed");
    Serial1.write("\r");
  delay(10);
  Serial.println("sending l/f ");
  while (Serial1.available()) {
    char c = Serial1.read();
    if (c == '<') {
      delay(10);
      char next1 = Serial1.read();
      char next2 = Serial1.read();
      if (next1 == 0x0A && next2 == 0x0D) {
        return true;
      }
    }
  }
 */
   Serial.println("didn't find the powerup string"); 
  lelsReady = 0;
  return false;
}


bool lelsPower() {
  Serial.println("Checking Lels Power");
  String response = "";
  while (Serial1.available()) {
    char c = Serial1.read();
    if (c == '<') {
      response += c;
      delay(10);
      if (Serial1.read() == 0x0A) {
        delay(10);
        if (Serial1.read() == 0x0D) {
        }
       Serial.println("Lels READY!!");
       int lelsReady = 1;
      }return true;
    }
  }
  lelsReady = 0;
  Serial.println(" Lels is NOT powered " );
  return false;
  //Serial.println(response);
}

void printTags1(String data) {
  String tags[9] = {"S", "O", "L", "N", "M", "U", "T", " ","B"};
  int tagLengths[9] = {11, 31, 1, 3, 3, 1, 2, 2, 3};
  int dataIndex = 0;

  Serial.println("TAG\t\tVALUE");
  Serial.println("------------------------");

  for (int i = 0; i < 9; i++) {
    String tagValue = data.substring(dataIndex, dataIndex + tagLengths[i]);
    dataIndex += tagLengths[i];
    Serial.println(tags[i] + ":\t" + tagValue);
  }
}


void printTags2(String data) {
  String tags[11] = {"S", "O", "L", "N", "M", "U", "T", "B", "X", "Y", "Z"};
  int tagLengths[11] = {9, 31, 1, 3, 3, 1, 2, 2, 4, 4, 4};
  int dataIndex = 0;

  Serial.println("TAG\t\tVALUE");
  Serial.println("------------------------");

  for (int i = 0; i < 11; i++) {
    String tagValue = data.substring(dataIndex, dataIndex + tagLengths[i]);
    dataIndex += tagLengths[i];
    Serial.println(tags[i] + ":\t" + tagValue);
  }
}



void initdisplay() {
  pinMode(LEDR, OUTPUT);  // initialize the LED pin as an output:
  pinMode(LEDG, OUTPUT);  // initialize the LED pin as an output:
  pinMode(LEDB, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);  // initialize the LED pin as an output:
  u8x8.begin();
  u8x8.setFlipMode(1);  // set number from 1 to 3, the screen word will rotary 180
  u8x8.setFont(u8x8_font_8x13B_1x2_r);
  u8x8.clearDisplay();
  u8x8.setCursor(0, 0);
  u8x8.print("Power ON ");
}
void setupblink() {

  setLedRGB(false, true, false);  // Red
  delay(1000);
  setLedRGB(true, false, false);  // Green
  delay(1000);
  setLedRGB(false, false, true);  // Blue 
  delay(1000);
  setLedRGB(false, false, false);  // OFF
}

void setLedRGB(bool red, bool green, bool blue) {

  if (!blue) {
    digitalWrite(LEDB, HIGH);
  } else {
    digitalWrite(LEDB, LOW);
  }
  if (!green) {
    digitalWrite(LEDG, HIGH);
  } else {
    digitalWrite(LEDG, LOW);
  }
  if (!red) {
    digitalWrite(LEDR, HIGH);
  } else {
    digitalWrite(LEDR, LOW);
  }
}

As far as SDK and NRF connect Install the tool chain and click the “First Step” it will walk you through building app and code with Zephyr.
HTH
GL :slight_smile:
PJ