Qwiic Distance Sensor (VL53L1X) Hookup Guide

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

View the updated tutorial: Qwiic Distance Sensor (VL53L1X, VL53L4CD) Hookup Guide

Pages
Contributors: Englandsaurus
Favorited Favorite 0

Python Examples

The example code for this product is located in the GitHub repository for the Python package; it is also hosted with the ReadtheDocs documentation:

To run the examples, simple download or copy the code into a file. Then, open/save the example file (if needed) and execute the code in your favorite Python IDE. For example, with the default Python IDLE click Run > Run Module or use the F5 key. To terminate the example use the Ctrl + C key combination.

Example 1

This example prints the distance to an object. If you are getting weird readings, be sure the vacuum tape has been removed from the sensor.

Import Dependencies

The first part of the code, imports the required dependencies to operate.

language:python
import qwiic
import time

Initialize Constructor

These lines instantiates an object for the device and initializes the sensor.

language:python
ToF = qwiic.QwiicVL53L1X()
if (ToF.sensor_init() == None):                  # Begin returns 0 on a good init
    print("Sensor online!\n")

Test Run

This section of the code, illustrates how readings are taken from the sensor and displayed, while being looped. In the first section of the code, sensors readings are initiated, recorded, and then terminated. The second part of the code converts the units of the readings and displays them.

language:python
while True:
    try:
        ToF.start_ranging()                      # Write configuration bytes to initiate measurement
        time.sleep(.005)
        distance = ToF.get_distance()    # Get the result of the measurement from the sensor
        time.sleep(.005)
        ToF.stop_ranging()

        distanceInches = distance / 25.4
        distanceFeet = distanceInches / 12.0

        print("Distance(mm): %s Distance(ft): %s" % (distance, distanceFeet))

    except Exception as e:
        print(e)

Example 2

This example configures the sensor to short distance mode and then prints the distance to an object. If you are getting weird readings, be sure the vacuum tape has been removed from the sensor.

Import Dependencies

The first part of the code, imports the required dependencies to operate.

language:python
import qwiic
import time

Initialize Constructor

These lines instantiates an object for the device and initializes the sensor.

language:python
ToF = qwiic.QwiicVL53L1X()
if (ToF.sensor_init() == None):                  # Begin returns 0 on a good init
    print("Sensor online!\n")

Test Run

This section of the code, illustrates how readings are taken from the sensor and displayed. In the first part of the code, the sensor is configured to read with the short distance mode (the sensor is configured for long distance mode on power up). The second part of the code reads and displays data; as mentioned in the previous example.

language:python
ToF.set_distance_mode(1)    # Sets Distance Mode Short (Long- Change value to 2)


while True:
    try:
        ToF.start_ranging()                      # Write configuration bytes to initiate measurement
        time.sleep(.005)
        distance = ToF.get_distance()    # Get the result of the measurement from the sensor
        time.sleep(.005)
        ToF.stop_ranging()

        distanceInches = distance / 25.4
        distanceFeet = distanceInches / 12.0

        print("Distance(mm): %s Distance(ft): %s" % (distance, distanceFeet))

    except Exception as e:
        print(e)