I have wired up my range finder (CJVL53L0XV2) to a breadboard for testing and have run the arduino example code for the rangefinder. The rangefinder works, but I do not know how to take the distance that the range finder shows on the serial monitor, example: distance measured 44mm, and turn that information into a variable that the rest of my program could use.

I am not sure how to "grab" data from the sensor and turn that data into a variable that I can then manipulate. I have tried to dissect the example code, but it seems to be taking data and using functions that do not exist anywhere but in some files in the arduino library that I can't identify or access.

Below is the code that I have been trying to reverse engineer.

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
int pbl = 9;
void setup() {
  pinMode(pbl, OUTPUT);
  digitalWrite(pbl, HIGH);

  // wait until serial port opens for native USB devices
  while (! Serial) {

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
  // 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 ");

  • use a lookup table ... distance to number of rounds
    – jsotola
    Commented Dec 4, 2019 at 20:32
  • please reduce the amount of text in your post ... you can delete the first and third paragraphs ... also delete the bottom half of second paragraph, starting with The way I would like the rangefinder .... that should leave a simple, to the point question.
    – jsotola
    Commented Dec 4, 2019 at 20:38
  • jsotola, Thanks. I have edited my question to get to the point more quickly.
    – pvGuy
    Commented Dec 4, 2019 at 20:51
  • 2
    Perhaps if you add the code to the question, we can suggest what to add/change.
    – user85471
    Commented Dec 4, 2019 at 23:59
  • My apologies. The code I have been trying to work with is just the sample code. I posted it with my question. Thanks!
    – pvGuy
    Commented Dec 5, 2019 at 0:59

1 Answer 1


measure.RangeMilliMeter was the key to it all. After experimenting for several hours, I finally got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.