Search Code

Using a VL53L0X Laser Distance Meter with Arduino

Using a VL53L0X Laser Distance Meter with Arduino

In this tutorial, we will explore how to use the VL53L0X laser distance meter with an Arduino. This device allows you to measure distances accurately using a laser, making it ideal for various applications such as robotics and automation. By the end of this tutorial, you will have a functional setup that can measure distances and display them in the serial monitor.

VL53L0X 200cm range sensor-blue

For our project, we will be using the Adafruit VL53L0X library, which simplifies the process of interfacing with the sensor. This library provides the necessary functions to initialize the sensor, read measurements, and handle any errors that may arise. You can refer to the video for a visual guide on the setup (in video at 02:15).

Hardware Explained

The main components of this project include the VL53L0X laser distance meter and the Arduino board. The VL53L0X is a time-of-flight sensor that uses a laser to measure distances up to 2 meters with high accuracy. It operates via the I2C protocol, allowing easy communication with the Arduino.

The Arduino board serves as the microcontroller that processes the data from the VL53L0X. It sends commands to the sensor and receives the distance measurements, which can then be displayed or utilized in other applications. Proper wiring is crucial for ensuring accurate readings from the sensor.

Datasheet Details

ManufacturerSTMicroelectronics
Part numberVL53L0X
Operating voltage2.6 V to 3.5 V
Range30 mm to 2000 mm
Accuracy±3% typical
InterfaceI2C
Temperature range-40 °C to +85 °C
Current consumption<1 mA (standby), 20 mA (active)
PackageVFLGA-8

  • Ensure the sensor is powered correctly (2.6 V to 3.5 V).
  • Maintain correct I2C address settings to avoid conflicts.
  • Keep the sensor clean for accurate distance measurements.
  • Use appropriate pull-up resistors on the I2C lines if needed.
  • Avoid direct sunlight on the sensor for reliable readings.

Wiring Instructions

Arduino Wiring for VL53L0X
Arduino Wiring for VL53L0X

To wire the VL53L0X sensor to the Arduino, connect the VCC pin of the sensor to the 5V pin on the Arduino. The GND pin should be connected to the ground (GND) of the Arduino. For the I2C communication, connect the SDA pin of the VL53L0X to the A4 pin on the Arduino, and connect the SCL pin to the A5 pin. If you're using a different Arduino model, refer to the specific SDA and SCL pin assignments for that board.

For example, on the Arduino Mega, you would connect SDA to pin 20 and SCL to pin 21. Ensure that all connections are secure to avoid communication issues. If the sensor does not respond, double-check the wiring and ensure that the Arduino is properly powered.

Code Examples & Walkthrough

Below is a snippet from the setup function that initializes the VL53L0X sensor:

void setup() {
  Serial.begin(9600);
  while (! Serial) {
    delay(1);
  }
  Serial.println("Robojax Test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
}

This code initializes the serial communication and attempts to start the VL53L0X sensor. If the sensor fails to boot, it will print an error message and halt the program.

Next, here is a snippet from the loop function that reads the distance measurement:

void loop() {
  VL53L0X_RangingMeasurementData_t measure;
  lox.rangingTest(&measure, false);
  if (measure.RangeStatus != 4) {
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
  delay(100);
}

This block continuously reads the distance measurement from the sensor and prints it to the serial monitor. If the measurement is out of range, it indicates this accordingly.

Demonstration / What to Expect

When you run the program, you should see distance measurements displayed in the serial monitor. The readings should update every 100 milliseconds. If the sensor is pointing at an object, it will display the distance in millimeters. If the object is out of range, it will indicate that as well. Make sure to test the sensor within its specified range for optimal results (in video at 10:00).

Video Timestamps

  • 00:00 - Introduction
  • 02:15 - Wiring setup
  • 05:30 - Code explanation
  • 10:00 - Demonstration

Images

VL53L0X 200cm range sensor-blue
VL53L0X 200cm range sensor-blue
Arduino Wiring for VL53L0X
Arduino Wiring for VL53L0X
15-Using a VL53L0X laser distance meter in Arduino
Language: C++
/* This example shows how to use continuous mode to take
range measurements with the VL53L0X.
// Original source from Adafruit https://github.com/adafruit/Adafruit_VL53L0X
// Modified by Ahmad Shamshiri for RoboJax.com
// Date modified: Sep 26, 2017
// Nejrabi

* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place. 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

   Copyright (c) 2015, Majenko Technologies
   All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
   are permitted provided that the following conditions are met:

 * * Redistributions of source code must retain the above copyright notice, this
     list of conditions and the following disclaimer.

 * * Redistributions in binary form must reproduce the above copyright notice, this
     list of conditions and the following disclaimer in the documentation and/or
     other materials provided with the distribution.

 * * Neither the name of Majenko Technologies nor the names of its
     contributors may be used to endorse or promote products derived from
     this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
  Serial.begin(9600);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Robojax 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 ");
  }
    
  delay(100);
}

Resources & references

Files📁

No files available.