How do I call the value of reading from VL53LOX and store it to a register?

I have VL53LOX connecting to arduino D1 I use the code find it in the example of IDE (shown below). Its required to read this value to to PLC via Modbus protocol

The problem is the serial stopped reading the value when plc connected to arduino D1 and began to read the distance.

#include “Adafruit_VL53L0X.h”

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {

  Serial.begin(115200);

  // wait until serial port opens for native USB devices

  while (! Serial) {

    delay(1);

  }

  Serial.println(“Adafruit VL53L0X test”);

  if (!lox.begin()) {

    Serial.println(F(“Failed to boot VL53L0X”));

    while(1);

  }

  // power

  Serial.println(F(“VL53L0X API Simple Ranging example\n\n”));

}

void loop() {

  VL53L0X_RangingMeasurementData_t measure;

      Serial.print("Reading a measurement… ");

  lox.rangingTest(&measure, false); // pass in ‘true’ to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data

    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);

  } else {

    Serial.println(" out of range ");

  }

MBHoldingRegister[6] = measure.RangeMilliMeter;

      delay(1000);

}

Knowing the part of Modbus code not included.

Could anyone help me to solve the problem

Does it work the first time? Is there a conflict between the Modbus and the Vl53lox?

1 Like