Continue to Site

Reply to thread

Here is one code I tried using. Let me know if that works. Thank you.


[CODE]#define COM 0x55


void setup() {

  Serial.begin(115200);        // USB serial monitor

  while (!Serial);             // Wait for connection


  Serial1.begin(115200);       // UART connection to sensor (TX on sensor → RX1 = pin 0)

  Serial.println("Listening for distance from UART sensor...");

}


void loop() {

  Serial1.write(COM);          // Send 0x55 to request data

  delay(100);


  if (Serial1.available() >= 4) {

    byte header = Serial1.read();

    if (header == 0xFF) {

      byte highByte = Serial1.read();

      byte lowByte  = Serial1.read();

      byte checksum = Serial1.read();


      int distance = (highByte << 8) | lowByte;

      byte calcChecksum = (0xFF + highByte + lowByte) & 0xFF;


      if (checksum == calcChecksum) {

        Serial.print("Distance: ");

        Serial.print(distance);

        Serial.println(" mm");

      } else {

        Serial.print("Checksum mismatch! Got 0x");

        Serial.print(checksum, HEX);

        Serial.print(", expected 0x");

        Serial.println(calcChecksum, HEX);

      }

    } else {

      Serial.print("Unexpected header: 0x");

      Serial.println(header, HEX);

    }

  } else {

    Serial.println("Waiting for response...");

  }

}

[/CODE]


Part and Inventory Search

Back
Top