UWB Q1

Receive UWB Position Data with Arduino Mega 2560 (Example Code)

This guide connects a GrowSpace UWB developer tag to an Arduino Mega 2560 over serial, then receives and parses real-time position data with the lec and lep commands, with example code.

Arduino Mega 2560 integration, what you need, screen 1

About this article

This article shows how to connect a GrowSpace developer tag to an Arduino Mega 2560 board over serial, and receive and parse position data in real time with the lec and lep commands. It also provides example code that uses the Mega 2560, which supports hardware serial, to set up stable high-speed communication.


What you need

Arduino Mega 2560 integration, what you need, screen 1

Jumper wires

Serial connection cable provided

⚠️ The UNO board has just 1 hardware serial port, so errors can occur in high-speed communication. The Mega 2560 provides Serial1 to Serial3, which allows stable communication.


Arduino IDE setup and board connection

Arduino Mega 2560 integration, Arduino IDE setup and board connection, screen 5

Arduino Mega 2560 integration, Arduino IDE setup and board connection, screen 6


Hardware pin wiring

Using the right connector (5V) of the GrowSpace developer tag, connect as follows:

Developer tag pinArduino Mega pin
TXRX1 (pin 19)
RXTX1 (pin 18)
5V5V
GNDGND

🔄 TX ↔ RX must be cross-connected for communication to work.

Below is an example of the Arduino Mega pin layout.

Connect using TX1 (pin 18) and RX1 (pin 19) of the Serial1 port.

Arduino Mega 2560 integration, hardware pin wiring, screen 8


Serial relay example (basic communication check)

String inputFromSerial0 = "";
String inputFromSerial1 = "";

void setup() {
  Serial.begin(115200);    // USB Serial (Serial0)
  Serial1.begin(115200);   // Hardware Serial1 (pin 18 TX1, pin 19 RX1)
}

void loop() {
  // serialEvent() and serialEvent1() are called automatically
}

// Serial0 input → Serial1 output (appends CR)
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar != '\n') {
      inputFromSerial0 += inChar;
    }

    if (inChar == '\n') {
      Serial1.print(inputFromSerial0);
      Serial1.print('\r');
      inputFromSerial0 = "";
    }
  }
}

// Serial1 input → Serial0 output
void serialEvent1() {
  while (Serial1.available()) {
    char inChar = (char)Serial1.read();
    inputFromSerial1 += inChar;

    if (inChar == '\n') {
      Serial.print("From Serial1: ");
      Serial.print(inputFromSerial1);
      inputFromSerial1 = "";
    }
  }
}

Explanation

Serial Monitor setting tips

Enter the si command in the serial monitor. If you see a normal response, the connection works.

Arduino Mega 2560 integration, Serial Monitor setting tips, screen 9


Command-based position parsing example

String bufferSerial0 = "";
String bufferSerial1 = "";

void setup() {
  Serial.begin(115200);    // USB Serial
  Serial1.begin(115200);   // Serial1 for the developer tag connection
}

void loop() {
  // loop stays empty; serialEvent / serialEvent1 run automatically
}

// Receive commands from Serial0
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar != '\n') {
      bufferSerial0 += inChar;
    }

    if (inChar == '\n') {
      bufferSerial0.trim();  // Remove the newline

      if (bufferSerial0.equalsIgnoreCase("lec") || bufferSerial0.equalsIgnoreCase("lep")) {
        Serial.print("Command sent: ");
        Serial.println(bufferSerial0);
        Serial1.print(bufferSerial0); // Appends CR
        Serial1.print('\r');
      } else {
        Serial.println("⚠ Unsupported command.");
      }

      bufferSerial0 = "";
    }
  }
}

// Receive the response from Serial1 → parse lec/lep
void serialEvent1() {
  while (Serial1.available()) {
    char inChar = (char)Serial1.read();
    bufferSerial1 += inChar;

    if (inChar == '\n') {
      bufferSerial1.trim();

      if (bufferSerial1.startsWith("POS,")) {
        parseLEP(bufferSerial1);
      } else if (bufferSerial1.startsWith("DIST,")) {
        parseLEC(bufferSerial1);
      } else {
        Serial.println("[Response] " + bufferSerial1);
      }

      bufferSerial1 = "";
    }
  }
}

// --- Parse position data (lep) ---
void parseLEP(String line) {
  Serial.println("[LEP position result]");
  int idx = 0;
  String parts[5];
  while (line.length() > 0 && idx < 5) {
    int comma = line.indexOf(',');
    if (comma == -1) {
      parts[idx++] = line;
      break;
    } else {
      parts[idx++] = line.substring(0, comma);
      line = line.substring(comma + 1);
    }
  }

  Serial.print("X: "); Serial.println(parts[1]);
  Serial.print("Y: "); Serial.println(parts[2]);
  Serial.print("Z: "); Serial.println(parts[3]);
  Serial.print("Quality (QF): "); Serial.println(parts[4]);
  Serial.println();
}

// --- Parse the full lec response ---
void parseLEC(String line) {
  Serial.println("[LEC distance + position result]");
  int posIndex = line.indexOf("POS,");
  if (posIndex == -1) {
    Serial.println("→ Position information missing");
    return;
  }

  String distPart = line.substring(0, posIndex - 1);
  String posPart = line.substring(posIndex);

  Serial.println("▶ Anchor distance information:");
  int anchorIdx = 0;
  int anStart = 0;
  while ((anStart = distPart.indexOf("AN", anStart)) != -1) {
    int idStart = anStart + 2;
    int idEnd = distPart.indexOf(",", idStart);
    String id = distPart.substring(idStart, idEnd);

    int valStart = idEnd + 1;
    float x = distPart.substring(valStart, distPart.indexOf(",", valStart)).toFloat();
    valStart = distPart.indexOf(",", valStart) + 1;
    float y = distPart.substring(valStart, distPart.indexOf(",", valStart)).toFloat();
    valStart = distPart.indexOf(",", valStart) + 1;
    float z = distPart.substring(valStart, distPart.indexOf(",", valStart)).toFloat();
    valStart = distPart.indexOf(",", valStart) + 1;
    float d = distPart.substring(valStart, distPart.indexOf(",", valStart)).toFloat();

    Serial.print("AN"); Serial.print(anchorIdx++); Serial.print(" (ID "); Serial.print(id); Serial.print("): ");
    Serial.print("x="); Serial.print(x); Serial.print(", y="); Serial.print(y); Serial.print(", z="); Serial.print(z);
    Serial.print(" → distance: "); Serial.print(d); Serial.println("m");

    anStart = valStart;
  }

  Serial.println("▶ Tag position information:");
  parseLEP(posPart);
}

Arduino Mega 2560 integration, command-based position parsing example, screen 10

Arduino Mega 2560 integration, command-based position parsing example, screen 11


Summary

With this guide, you can set up serial communication with a GrowSpace developer tag on an Arduino Mega 2560, and practice parsing and printing real-time position data based on the lec and lep commands.