Pleas help. struggling using VL53L0x time of filght sensor.

Hello, I’m still pretty new to Arduino and I have programed a VL530X time of flight sensor to tell when and object moves and display it on a IC2 1602 LCD display. The problem is I want to display “0” until the object moves and then just display the distance that is moved. I also want to then push a button to reset the display back to “0” until it moves again. I cant seem to figure out how to compare a previous value to a current value. I’ve been at it for days, watching youtube videos and searching google. If anyone can offer assistance I would greatly appreciate it. When I upload the code it just stays at “0cm” . I will attach the code. I think my issue is with how im running the sensor continuously and the current and previous values are never actually different.

Thank You!

#include <VL53L0X.h>

#include <Wire.h>

VL53L0X sensor;

#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int type = 2;// 1=mm , 2= cm
String unit;// variable for unit, mm or cm

void setup()




  // initialize the LCD, 
  // Turn on the blacklight and print a message.

void loop()
  int distance =sensor.readRangeContinuousMillimeters();
  //int distance =sensor.startContinuous(100);

  int distanceCM = (int) (distance/10.0);
  int fault_lite = 2; // distande changed
  int Reset_but = 3; // button to reset display distance to zero
  int But_state = 0;
  int Display_dis; // variable to print to lcd
  int current_dis; //current distance reading of VL53L0x
  int previous_dis;  //privious reading of VL53L0x
  int differance_CM; //differance between current and previous readings 

 //distance = distance;
  Serial.print("Distance: ");

  //Set up LCD Screen
  lcd.print("Void Detector");
  lcd.setCursor (0,1); // go to start of 2nd line
  lcd.print("Dist.: ");
  lcd.setCursor (7,1); // go to start of 2nd line
  unit ="cm";
 //Start Comparison
 sensor.startContinuous(500); // start sensor reading
 current_dis = distanceCM;  // set current distance
 previous_dis = current_dis;  //set previous distance

 sensor.startContinuous(500); // re-start sensor reading
 current_dis = distanceCM;    // re-set current distance
differance_CM = current_dis - previous_dis; //find differance

  if(current_dis != previous_dis){
     lcd.print (differance_CM);  
     digitalWrite (fault_lite, HIGH);}
  else{ lcd.print("0"); }

   if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

void loop()
  int previous_dis;  //privious reading of VL53L0x

You do realize that you are initializing the previous distance measurement with every loop iteration. You are not actually saving the previous reading. You can either move the variable declaration to a larger scope or declare the variable as staic.

Thank you for your reply. I did not think about that. I changed it to a static int and also tried to move the declaration to a larger scope. It still only displays 0cm though. If you happened to notice any other mistakes or have any ideas that would help I apricate the input. Thanks! p.s I was able to verify that my current distance variable and previous distance variable are always equal in value so like you said I'm still not not saving the previous reading.

In my experience with this sensor (and most other sensors) no two subsequent readings are the same, ever. That's due to the normal measurement error. So in fact you would have to see movement ALL THE TIME rather than when the object really moves.

So to detect movement you have to look at a change in distance larger than a certain threshold, which also means you need sufficient time between readings. You can also smooth out yojur readings by taking a moving average of the last 10 or 20 readings.

The problem is in the way you take readings: this takes time, and you don't give it time.

Start with the examples that come with the library on how to properly take readings of this sensor. Twice you give the sensor.startContinuous(500); command which makes the sensor return a reading every 500 ms. This should be in setup() once; the sensor takes readings every 500 ms. You have to wait in your code for the next reading to be completed, then read it.

The easiest way to do this is through the interrupt line of the sensor: just poll the interrupt line to see if the next reading is ready.