Distance Ranging Sensors
Distance Ranging Sensors
The VL53L0X is a ToF sensor which embeds the ST’s second generation FlightSense
technology.
Unlike conventional ranging sensors, the VL53L0X is able to provide accurate distance
measurement whatever the target color and reflectance, achieves better anti-
interference capability.
Features
Specifications
Applications
Source: https://www.waveshare.com/VL53L0X-distance-sensor.htm
Source: https://www.az-delivery.de/en/products/vl53l0x-time-of-flight-tof-laser-abstandssensor
VL53L0X Arduino Measure Distance |
Time-of-Flight Sensor Guide
This tutorial will help you to use the VL53L0X Time-of-Flight (ToF) sensor with
Arduino. A Time-of-flight sensor measures the distance of an object by
calculating the time it takes for light to reflect from the object. Such sensors use
a LASER or an LED as a light source, and a receiver to detect the reflected light.
Time-of-flight sensors differ from ultrasonic sensors such as the HC-SR04 which
rely on reflected ultrasonic waves. The ‘cone’ of sense in the ToF sensor is
narrower than ultrasonic sensors.
Since a Time-of-flight sensor uses a narrow light source, it can reliably measure
surfaces directly in front of it.
Table of Contents
The VL53L0X Sensor
o VL53L0X Features
o VL53L0X Pinout
Wiring VL53L0X with Arduino
VL53L0X Arduino Library
VL53L0X Arduino Code (Example 1)
o Output
VL53L0X Arduino Example 2
o Output
Conclusion
The VL53L0X consists of a tiny laser source and a receiver that can sense the
laser. It calculates distance by measuring the time it takes for a laser pulse to
travel to an object and back (Using the simple formula: Distance = Time x
Speed).
VL53L0X Features
The supply voltage can range from 2.8V to 5V, aided by an onboard
voltage regulator.
The module comes with an onboard logic level shifter which makes it
easier to interface with both 3.3V and 5V compatible microcontrollers.
VL53L0X Pinout
VL53L0X Pinout
Pin description:
Pin Description
Other modules are also available from different manufacturers that can be
interfaced in a similar way once you understand how to use the sensor. Some of
these modules come with only 4 pins – VCC, GND, SCL, and SDA.
For easily wiring the module on a breadboard, you can solder some pin headers
to the module.
The schematic below shows how we can connect the VL53L0X Time-of-flight
sensor with an Arduino board.
Schematic: VL53L0X with Arduino
Connections:
Connect the ‘5V’ pin of Arduino UNO to the ‘VCC’ pin of the VL53L0X
module.
Connect one of the ‘GND’ pins of Arduino UNO to the ‘GND’ pin of the
VL53L0X module.
Connect the ‘A4’ pin of Arduino UNO to the ‘SDA’ pin of the VL53L0X
module.
Connect the ‘A5’ pin of Arduino UNO to the ‘SCL’ pin of the VL53L0X
module.
We shall discuss two Arduino libraries to interface with the VL53L0X sensor.
One of the most popular libraries is the Pololu VL53L0X library. This library
allows us to measure distances up to 2 meters but it does not support
interrupts.
The Adafruit library for VL53L0X can measure distances up to 1.2 meters
and also supports interrupts.
To install the Pololu library search for “VL53L0X” in Library Manager and find and
install the library by Pololu.
Similarly, you can install Adafruit’s version of the VL53L0X library by searching
for “VL53L0X”. Scroll to find the library by Adafruit and install the latest version.
The Adafruit library may also ask you to install additional dependencies which
you must do for the code to work reliably.
VL53L0X Arduino Code (Example 1)
This section will show you how to read the VL53L0X ToF sensor using the library
from Pololu. You can find an example sketch in Arduino IDE by navigating
to File>Examples>Single.
Alternatively, you can copy-paste the code given below. This code has some
changes to enable ‘long range mode’ and high accuracy.
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
#define LONG_RANGE
//#define HIGH_SPEED
#define HIGH_ACCURACY
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1) {}
}
void loop()
{
if(sensor.readRangeSingleMillimeters()>2000)
{
Serial.println("Out of range");
}
else
{
Serial.print(sensor.readRangeSingleMillimeters());
Serial.println(" mm");
}
if (sensor.timeoutOccurred())
{
Serial.println(" TIMEOUT");
}
}
The code above will let you measure objects at a distance of up to 2 meters from
the VL53L0X ToF sensor.
This code begins by including the necessary libraries, Wire.h for I2C
communication and VL53L0X.h for interacting with the VL53L0X sensor. It then
creates an instance of the sensor object with VL53L0X sensor.
The code then checks for defined modes: if #define LONG_RANGE is active, it
configures the VL53L0X sensor for long-range operation by lowering the return
signal rate limit and laser pulse periods. Additionally, depending on
whether #define HIGH_SPEED or #define HIGH_ACCURACY is set, the code adjusts
the measurement timing budget to prioritize either speed
(sensor.setMeasurementTimingBudget(20000)) or accuracy
(sensor.setMeasurementTimingBudget(200000)).
In the loop() function, the code repeatedly checks the sensor’s distance reading.
If the measured distance is greater than 2000 mm
(if(sensor.readRangeSingleMillimeters()>2000)), it prints “Out of range” to the
serial monitor; otherwise, it prints the measured distance in millimeters. If a
timeout occurs during measurement, it prints “TIMEOUT” using if
(sensor.timeoutOccurred()).
Output
The output of the sketch will be available on the Serial Monitor of Arduino IDE
which can be accessed by navigating to Tools>Serial Monitor. Set the baud
rate in the serial monitor the same as your code (9600 in this example). The
screenshot below shows the output data obtained from the sensor. The readings
are in ‘mm’ by default.
VL53L0X Arduino Example 2
Next, we shall use Adafruit’s library to interface the time-of-flight sensor with
Arduino code. The example code can be found in Arduino IDE by navigating
to File>Examples>Adafruit_VL53L0X>vl53l0x.
You can copy the code below to get output from the distance sensor:
#include "Adafruit_VL53L0X.h"
void setup() {
Serial.begin(115200);
void loop() {
VL53L0X_RangingMeasurementData_t measure;
delay(100);
}
The code above can measure distances up to 1200mm (1.2 meters/3.94 feet)
from the sensor.
This code begins by including the Adafruit VL53L0X library and initiates Serial
communication with a baud rate of 115200. The sensor is initialized
with lox.begin(), and if the initialization fails, an error message “Failed to boot
VL53L0X” is printed, followed by an infinite loop to halt further execution.
After successful initialization, the program prints “VL53L0X API Simple Ranging
example” to the serial monitor.
The loop() function handles the continuous reading of distance measurements.
A VL53L0X_RangingMeasurementData_t structure named measure is declared to
store the sensor’s measurement data. The sensor’s ranging test is initiated
with lox.rangingTest(&measure, false), where the false parameter disables
debug data printout.
If the data is valid, the code prints the distance in millimeters. If the
measurement is out of range, it prints “out of range”. The loop pauses for 100
milliseconds before taking the next measurement.
Output
The output can be viewed in the Serial Monitor of Arduino IDE by setting the
baud rate to 115200.
The Adafruit library for VL53L0X also supports additional features such as
reading two VL53L0X sensors using I2C, or using interrupt routine to read the
sensor only when the measurement is within a specified range. You can explore
such examples through the ‘examples’ section in Arduino IDE or by visiting the
Github link for the Adafruit VL53L0X library.
Conclusion
This article explored how we can use the VL53L0X sensor with Arduino to
measure distances up to 2 meters. This guide can be used to make projects such
as a car parking assistant, digital ruler, drone altitude sensor, etc.
Also check out our article: Measure Distance Using Raspberry Pi Pico & HC-SR04
(MicroPython code)
Specification
In this lesson, you will learn how to use the VL53L0X Time of Flight Distance
Sensor with an Arduino Uno. We’ll cover the basics of connecting the sensor to
measure distances in millimeters and displaying the readings on the serial
monitor. This project provides hands-on experience with advanced sensors and
their real-world applications, enhancing your Arduino skills.
Wiring
Code
Note
To install the library, use the Arduino Library Manager and search for “Adafruit_VL53L0X” and
install it.
/*
This program tests the Adafruit VL53L0X Time of Flight Distance Sensor.
It initializes the sensor, reads a distance measurement, and prints the
distance in millimeters to the serial monitor.
Note: The VL53L0X can handle about 50 - 1200 mm of range distance.
#include <Adafruit_VL53L0X.h>
void setup() {
Serial.begin(115200);
void loop() {
VL53L0X_RangingMeasurementData_t measure;
delay(100);
}
Code Analysis
1. Including the necessary library and initializing the sensor object. We start by
including the library for the VL53L0X sensor and creating an instance of the
Adafruit_VL53L0X class.
Note
To install the library, use the Arduino Library Manager and search
for “Adafruit_VL53L0X” and install it.
#include <Adafruit_VL53L0X.h>
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
2. Initialization in the setup() function. Here, we set up serial communication and
initialize the distance sensor. If the sensor can’t be initialized, the program
halts.
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(1);
}
Serial.println("Adafruit VL53L0X test");
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while (1)
;
}
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
3. Capturing and displaying the measurements in the loop() function.
Continuously, the Arduino captures a distance measurement using
the rangingTest() method. If the measurement is valid, it’s printed to the serial
monitor.
void loop() {
VL53L0X_RangingMeasurementData_t measure;
Serial.print("Reading a measurement... ");
lox.rangingTest(&measure, false);
if (measure.RangeStatus != 4) {
Serial.print("Distance (mm): ");
Serial.println(measure.RangeMilliMeter);
} else {
Serial.println(" out of range ");
}
delay(100);
}