Example Code for Arduino-Basic Distance Measurement

Last revision 2025/12/16

The example shows the usage of VL53L0X in a simple way.

Hardware Preparation

  • Arduino UNO control board x1.
  • VL53L0X Distance detection Sensor module x1.

Software Preparation

Wiring Diagram

Connection diagram

Sample Code

/*!
 * @file DFRobot_VL53L0X.ino
 * @brief DFRobot's Laser rangefinder library
 * @n The example shows the usage of VL53L0X in a simple way.

 * @copyright   [DFRobot](https://www.dfrobot.com), 2016
 * @copyright   GNU Lesser General Public License

 * @author [LiXin]
 * @version  V1.0
 * @date  2017-8-21
 * @https://github.com/DFRobot/DFRobot_VL53L0X
 timer*/
#include "Arduino.h"
#include "Wire.h"
#include "DFRobot_VL53L0X.h"

/*****************Keywords instruction*****************/
//Continuous--->Continuous measurement model
//Single------->Single measurement mode
//High--------->Accuracy of 0.25 mm
//Low---------->Accuracy of 1 mm
/*****************Function instruction*****************/
//setMode(ModeState mode, PrecisionState precision)
  //*This function is used to set the VL53L0X mode
  //*mode: Set measurement mode       Continuous or Single
  //*precision: Set the precision     High or Low
//void start()
  //*This function is used to enabled VL53L0X
//float getDistance()
  //*This function is used to get the distance
//uint16_t getAmbientCount()
  //*This function is used to get the ambient count
//uint16_t getSignalCount()
  //*This function is used to get the signal count
//uint8_t getStatus();
  //*This function is used to get the status
//void stop()
  //*This function is used to stop measuring

DFRobotVL53L0X sensor;

void setup() {
  //initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  //join i2c bus (address optional for master)
  Wire.begin();
  //Set I2C sub-device address
  sensor.begin(0x50);
  //Set to Back-to-back mode and high precision mode
  sensor.setMode(Continuous,High);
  //Laser rangefinder begins to work
  sensor.start();
}

void loop()
{
  //Get the distance
  Serial.print("Distance: ");Serial.println(sensor.getDistance());
  //The delay is added to demonstrate the effect, and if you do not add the delay,
  //it will not affect the measurement accuracy
  delay(500);
}

Result

Expected Results:

Result

Measured Results:

actual distance measuring distance(erro) actual distance measuring distance(erro) actual distance measuring distance(erro) actual distance measuring distance(erro)
500 490(-10)
491.5(-8.5)
498(-2)
496.5(-3.5)
499.75(-0.25)
503.75(+3.75)
300 304.75(+4.75)
298.75(-1.25)
299.5(-0.5)
302(+2)
297.5(-2.5)
299.5(-0.5)
138 138.5(+0.5)
131.5(-6.5)
132.25(-5.75)
131.25(-6.75)
136(-2)
138.5(+0.5)
100 102(+2)
109(+9)
105.5(+5.5)
104.75(+4.75)
99.5(-0.5)
102.5(+2.5)

Was this article helpful?

TOP