Qwiic Distance Sensor (VL53L1X, VL53L4CD) Hookup Guide

Pages
Contributors: bboyho, Elias The Sparkiest, Englandsaurus
Favorited Favorite 3

Python Package Overview

Note: This Python package has been tested on a Raspberry Pi 3 and 4 using Python 3.

Update: This package has been updated to version 1.0.1 (released 1-20-2020), which is not backwards compatible with the previous packages. The package still retains the same functionality as version 0.9.4; however, most of the methods have been renamed to conform to the more "Pythonic" naming conventions (i.e. not camel case). For more details, check out the commit history in the GitHub repository.

Note: This example assumes you are using the latest version of Python 3. If this is your first time using Python or I2C hardware on a Raspberry Pi, please checkout our tutorial on Python Programming with the Raspberry Pi and the Raspberry Pi SPI and I2C Tutorial.

We've written a Python package to easily get setup and take readings from the Qwiic Distance Sensor. However, before we jump into getting data from the sensor, let's take a closer look at the available functions in the Python package. You can install the sparkfun-qwiic-vl53l1x Python package hosted by PyPi. However, if you prefer to manually download and build the libraries from the GitHub repository, you can grab them here (*Please be aware of any package dependencies. You can also check out the repository documentation page, hosted on Read the Docs.):

Installation

Note: Don't forget to double check that the hardware I2C connection is enabled on your Raspberry Pi or other single board computer.

PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-vl53l1x package. On systems that support PyPi installation via pip3 (use pip for Python 2) is simple, using the following commands:

For all users (note: the user must have sudo privileges):

language:bash
sudo pip3 install sparkfun-qwiic-vl53l1x

For the current user:

language:bash
pip3 install sparkfun-qwiic-vl53l1x

Local Installation

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line (use python for Python 2):

language:bash
python3 setup.py install

To build a package for use with pip3:

language:bash
python3 setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip3.

language:bash
cd dist
pip3 install sparkfun_qwiic_vl53l1x-<version>.tar.gz

Python Package Operation

Below is a description of the basic functionality of the Python package. This includes the package organization, built-in methods, and their inputs and/or outputs. For more details on how the Python package works, check out the source code, sensor datasheet, and API user manual.

Dependencies

This Python package has a very few dependencies in the code, listed below:

language:python
import time                         # Time access and conversion package
import math                         # Basic math package
import qwiic_i2c                    # I2C bus driver package

Default Variables

The default variables, in the code, for this Python package are listed below:

language:python
# From vL53l1x_class.h Header File
###############################################################################
###############################################################################
SOFT_RESET =                                                            0x0000
VL53L1_I2C_SLAVE__DEVICE_ADDRESS =                                      0x0001
VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND =                          0x0008
ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS =                        0x0016
ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS =                    0x0018
ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS =                    0x001A
ALGO__PART_TO_PART_RANGE_OFFSET_MM =                                    0x001E
MM_CONFIG__INNER_OFFSET_MM =                                            0x0020
MM_CONFIG__OUTER_OFFSET_MM =                                            0x0022
GPIO_HV_MUX__CTRL =                                                     0x0030
GPIO__TIO_HV_STATUS =                                                   0x0031
SYSTEM__INTERRUPT_CONFIG_GPIO =                                         0x0046
PHASECAL_CONFIG__TIMEOUT_MACROP =                                       0x004B
RANGE_CONFIG__TIMEOUT_MACROP_A_HI =                                     0x005E
RANGE_CONFIG__VCSEL_PERIOD_A =                                          0x0060
RANGE_CONFIG__VCSEL_PERIOD_B =                                          0x0063
RANGE_CONFIG__TIMEOUT_MACROP_B_HI =                                     0x0061
RANGE_CONFIG__TIMEOUT_MACROP_B_LO =                                     0x0062
RANGE_CONFIG__SIGMA_THRESH =                                            0x0064
RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS =                           0x0066
RANGE_CONFIG__VALID_PHASE_HIGH =                                        0x0069
VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD =                                0x006C
SYSTEM__THRESH_HIGH =                                                   0x0072
SYSTEM__THRESH_LOW =                                                    0x0074
SD_CONFIG__WOI_SD0 =                                                    0x0078
SD_CONFIG__INITIAL_PHASE_SD0 =                                          0x007A
ROI_CONFIG__USER_ROI_CENTRE_SPAD =                                      0x007F
ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE =                         0x0080
SYSTEM__SEQUENCE_CONFIG =                                               0x0081
VL53L1_SYSTEM__GROUPED_PARAMETER_HOLD =                                 0x0082
SYSTEM__INTERRUPT_CLEAR =                                               0x0086
SYSTEM__MODE_START =                                                    0x0087
VL53L1_RESULT__RANGE_STATUS =                                           0x0089
VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0 =                         0x008C
RESULT__AMBIENT_COUNT_RATE_MCPS_SD =                                    0x0090
VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 =                 0x0096
VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0 =    0x0098
VL53L1_RESULT__OSC_CALIBRATE_VAL =                                      0x00DE
VL53L1_FIRMWARE__SYSTEM_STATUS =                                        0x00E5
VL53L1_IDENTIFICATION__MODEL_ID =                                       0x010F
VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD =                               0x013E

_VL53L1X_DEFAULT_DEVICE_ADDRESS =                                       0x52
###############################################################################
###############################################################################

_DEFAULT_NAME = "Qwiic 4m Distance Sensor (ToF)"

###############################################################################
###############################################################################
_FULL_ADDRESS_LIST = list(range(0x08,0x77+1))                   # Full I2C Address List (excluding resrved addresses)
_FULL_ADDRESS_LIST.remove(_VL53L1X_DEFAULT_DEVICE_ADDRESS >> 1) # Remove Default Address of VL53L1X from list
_AVAILABLE_I2C_ADDRESS = [_VL53L1X_DEFAULT_DEVICE_ADDRESS >> 1] # Initialize with Default Address of VL53L1X
_AVAILABLE_I2C_ADDRESS.extend(_FULL_ADDRESS_LIST)               # Add Full Range of I2C Addresses


# From vL53l1x_class.cpp C++ File
###############################################################################
###############################################################################
ALGO__PART_TO_PART_RANGE_OFFSET_MM =                                    0x001E
MM_CONFIG__INNER_OFFSET_MM =                                            0x0020
MM_CONFIG__OUTER_OFFSET_MM =                                            0x0022

# DEBUG_MODE

VL51L1X_DEFAULT_CONFIGURATION = [
0x00,   # 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), else don't touch
0x01,   # 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
0x01,   # 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 (pull up at AVDD)
0x01,   # 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low (bits 3:0 must be 0x1), use SetInterruptPolarity()
0x02,   # 0x31 : bit 1 = interrupt depending on the polarity, use CheckForDataReady()
0x00,   # 0x32 : not user-modifiable
0x02,   # 0x33 : not user-modifiable
0x08,   # 0x34 : not user-modifiable
0x00,   # 0x35 : not user-modifiable
0x08,   # 0x36 : not user-modifiable
0x10,   # 0x37 : not user-modifiable
0x01,   # 0x38 : not user-modifiable
0x01,   # 0x39 : not user-modifiable
0x00,   # 0x3a : not user-modifiable
0x00,   # 0x3b : not user-modifiable
0x00,   # 0x3c : not user-modifiable
0x00,   # 0x3d : not user-modifiable
0xff,   # 0x3e : not user-modifiable
0x00,   # 0x3f : not user-modifiable
0x0F,   # 0x40 : not user-modifiable
0x00,   # 0x41 : not user-modifiable
0x00,   # 0x42 : not user-modifiable
0x00,   # 0x43 : not user-modifiable
0x00,   # 0x44 : not user-modifiable
0x00,   # 0x45 : not user-modifiable
0x20,   # 0x46 : interrupt configuration 0->level low detection, 1-> level high, 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC
0x0b,   # 0x47 : not user-modifiable
0x00,   # 0x48 : not user-modifiable
0x00,   # 0x49 : not user-modifiable
0x02,   # 0x4a : not user-modifiable
0x0a,   # 0x4b : not user-modifiable
0x21,   # 0x4c : not user-modifiable
0x00,   # 0x4d : not user-modifiable
0x00,   # 0x4e : not user-modifiable
0x05,   # 0x4f : not user-modifiable
0x00,   # 0x50 : not user-modifiable
0x00,   # 0x51 : not user-modifiable
0x00,   # 0x52 : not user-modifiable
0x00,   # 0x53 : not user-modifiable
0xc8,   # 0x54 : not user-modifiable
0x00,   # 0x55 : not user-modifiable
0x00,   # 0x56 : not user-modifiable
0x38,   # 0x57 : not user-modifiable
0xff,   # 0x58 : not user-modifiable
0x01,   # 0x59 : not user-modifiable
0x00,   # 0x5a : not user-modifiable
0x08,   # 0x5b : not user-modifiable
0x00,   # 0x5c : not user-modifiable
0x00,   # 0x5d : not user-modifiable
0x01,   # 0x5e : not user-modifiable
0xdb,   # 0x5f : not user-modifiable
0x0f,   # 0x60 : not user-modifiable
0x01,   # 0x61 : not user-modifiable
0xf1,   # 0x62 : not user-modifiable
0x0d,   # 0x63 : not user-modifiable
0x01,   # 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), use SetSigmaThreshold(), default value 90 mm 
0x68,   # 0x65 : Sigma threshold LSB
0x00,   # 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), use SetSignalThreshold()
0x80,   # 0x67 : Min count Rate LSB
0x08,   # 0x68 : not user-modifiable
0xb8,   # 0x69 : not user-modifiable
0x00,   # 0x6a : not user-modifiable
0x00,   # 0x6b : not user-modifiable
0x00,   # 0x6c : Intermeasurement period MSB, 32 bits register, use SetIntermeasurementInMs()
0x00,   # 0x6d : Intermeasurement period
0x0f,   # 0x6e : Intermeasurement period
0x89,   # 0x6f : Intermeasurement period LSB
0x00,   # 0x70 : not user-modifiable
0x00,   # 0x71 : not user-modifiable
0x00,   # 0x72 : distance threshold high MSB (in mm, MSB+LSB), use SetD:tanceThreshold()
0x00,   # 0x73 : distance threshold high LSB
0x00,   # 0x74 : distance threshold low MSB ( in mm, MSB+LSB), use SetD:tanceThreshold()
0x00,   # 0x75 : distance threshold low LSB
0x00,   # 0x76 : not user-modifiable
0x01,   # 0x77 : not user-modifiable
0x0f,   # 0x78 : not user-modifiable
0x0d,   # 0x79 : not user-modifiable
0x0e,   # 0x7a : not user-modifiable
0x0e,   # 0x7b : not user-modifiable
0x00,   # 0x7c : not user-modifiable
0x00,   # 0x7d : not user-modifiable
0x02,   # 0x7e : not user-modifiable
0xc7,   # 0x7f : ROI center, use SetROI()
0xff,   # 0x80 : XY ROI (X=Width, Y=Height), use SetROI()
0x9B,   # 0x81 : not user-modifiable
0x00,   # 0x82 : not user-modifiable
0x00,   # 0x83 : not user-modifiable
0x00,   # 0x84 : not user-modifiable
0x01,   # 0x85 : not user-modifiable
0x00,   # 0x86 : clear interrupt, use ClearInterrupt()
0x00    # 0x87 : start ranging, use StartRanging() or StopRanging(), If you want an automatic start after self.init() call, put 0x40 in location 0x87
]
###############################################################################
###############################################################################


# From vL53l1_error_codes.h Header File
###############################################################################
###############################################################################
# @brief Error Code definitions for VL53L1 API.
#=======================================================================
# PRIVATE define do not edit
#=======================================================================

# @defgroup VL53L1_define_Error_group Error and Warning code returned by API
# The following DEFINE are used to identify the PAL ERROR

VL53L1_ERROR_NONE =                                                           0
VL53L1_ERROR_CALIBRATION_WARNING =                                           -1
# """Warning invalid calibration data may be in used
#   \a VL53L1_InitData()
#   \a VL53L1_GetOffsetCalibrationData
#   \a VL53L1_SetOffsetCalibrationData"""
VL53L1_ERROR_MIN_CLIPPED =                                                   -2
# """Warning parameter passed was clipped to min before to be applied"""

VL53L1_ERROR_UNDEFINED =                                                     -3
# """Unqualified error"""
VL53L1_ERROR_INVALID_PARAMS =                                                -4
# """Parameter passed is invalid or out of range"""
VL53L1_ERROR_NOT_SUPPORTED =                                                 -5
# """Function is not supported in current mode or configuration"""
VL53L1_ERROR_RANGE_ERROR =                                                   -6
# """Device report a ranging error interrupt status"""
VL53L1_ERROR_TIME_OUT =                                                      -7
# """Aborted due to time out"""
VL53L1_ERROR_MODE_NOT_SUPPORTED =                                            -8
# """Asked mode is not supported by the device"""
VL53L1_ERROR_BUFFER_TOO_SMALL =                                              -9
# """..."""
VL53L1_ERROR_COMMS_BUFFER_TOO_SMALL =                                       -10
# """Supplied buffer is larger than I2C supports"""
VL53L1_ERROR_GPIO_NOT_EXISTING =                                            -11
# """User tried to setup a non-existing GPIO pin"""
VL53L1_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED =                             -12
# """unsupported GPIO functionality"""
VL53L1_ERROR_CONTROL_INTERFACE =                                            -13
# """error reported from IO functions"""
VL53L1_ERROR_INVALID_COMMAND =                                              -14
# """The command is not allowed in the current device state (power down)"""
VL53L1_ERROR_DIVISION_BY_ZERO =                                             -15
# """In the function a division by zero occurs"""
VL53L1_ERROR_REF_SPAD_INIT =                                                -16
# """Error during reference SPAD initialization"""
VL53L1_ERROR_GPH_SYNC_CHECK_FAIL =                                          -17
# """GPH sync interrupt check fail - API out of sync with device"""
VL53L1_ERROR_STREAM_COUNT_CHECK_FAIL =                                      -18
# """Stream count check fail - API out of sync with device"""
VL53L1_ERROR_GPH_ID_CHECK_FAIL =                                            -19
# """GPH ID check fail - API out of sync with device"""
VL53L1_ERROR_ZONE_STREAM_COUNT_CHECK_FAIL =                                 -20
# """Zone dynamic config stream count check failed - API out of sync"""
VL53L1_ERROR_ZONE_GPH_ID_CHECK_FAIL =                                       -21
# """Zone dynamic config GPH ID check failed - API out of sync"""

VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAI =                               -22
# """Thrown when run_xtalk_extraction fn has 0 succesful samples when using
# the full array to sample the xtalk. In this case there is not enough
# information to generate new Xtalk parm info. The function will exit and
# leave the current xtalk parameters unaltered"""
VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL =                            -23
# """Thrown when run_xtalk_extraction fn has found that the avg sigma
# estimate of the full array xtalk sample is > than the maximal limit
# allowed. In this case the xtalk sample is too noisy for measurement.
# The function will exit and leave the current xtalk parameters unaltered."""


VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL =                                    -24
# """Thrown if there one of stages has no valid offset calibration
# samples. A fatal error calibration not valid"""
VL53L1_ERROR_OFFSET_CAL_NO_SPADS_ENABLED_FAIL =                             -25
# """Thrown if there one of stages has zero effective SPADS Traps the case
# when MM1 SPADs is zero. A fatal error calibration not valid"""
VL53L1_ERROR_ZONE_CAL_NO_SAMPLE_FAIL =                                      -26
# """Thrown if then some of the zones have no valid samples. A fatal error
# calibration not valid"""
VL53L1_ERROR_TUNING_PARM_KEY_MISMATCH =                                     -27
# """Thrown if the tuning file key table version does not match with
# expected value. The driver expects the key table version to match the 
# compiled default version number in the define
# #VL53L1_TUNINGPARM_KEY_TABLE_VERSION_DEFAULT*"""
VL53L1_WARNING_REF_SPAD_CHAR_NOT_ENOUGH_SPADS =                             -28
# """Thrown if there are less than 5 good SPADs are available."""
VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_HIGH =                                -29
# """Thrown if the final reference rate is greater than the upper reference
# rate limit - default is 40 Mcps. Implies a minimum Q3 (x10) SPAD (5)
# selected"""
VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_LOW =                                 -30
# """Thrown if the final reference rate is less than the lower reference
# rate limit - default is 10 Mcps. Implies maximum Q1 (x1) SPADs selected"""


VL53L1_WARNING_OFFSET_CAL_MISSING_SAMPLES =                                 -31
# """Thrown if there is less than the requested number of valid samples."""
VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH =                                  -32
# """Thrown if the offset calibration range sigma estimate is greater than
# 8.0 mm. This is the recommended min value to yield a stable offset
# measurement"""
VL53L1_WARNING_OFFSET_CAL_RATE_TOO_HIGH =                                   -33
# """Thrown when VL53L1_run_offset_calibration() peak rate is greater than
# that 50.0Mcps. This is the recommended max rate to avoid pile-up
# influencing the offset measurement"""
VL53L1_WARNING_OFFSET_CAL_SPAD_COUNT_TOO_LOW =                              -34
# """Thrown when VL53L1_run_offset_calibration() when one of stages range
# has less that 5.0 effective SPADS. This is the recommended min value to
# yield a stable offset"""


VL53L1_WARNING_ZONE_CAL_MISSING_SAMPLES =                                   -35
# """Thrown if one of more of the zones have less than the requested number
# of valid samples"""
VL53L1_WARNING_ZONE_CAL_SIGMA_TOO_HIGH =                                    -36
# """Thrown if one or more zones have sigma estimate value greater than
# 8.0 mm. This is the recommended min value to yield a stable offset
# measurement"""
VL53L1_WARNING_ZONE_CAL_RATE_TOO_HIGH =                                     -37
# """Thrown if one of more zones have peak rate higher than that 50.0Mcps.
# This is the recommended max rate to avoid pile-up influencing the offset
# measurement"""


VL53L1_WARNING_XTALK_MISSING_SAMPLES =                                      -38
# """Thrown to notify that some of the xtalk samples did not yield valid
# ranging pulse data while attempting to measure the xtalk signal in
# vl53l1_run_xtalk_extract(). This can signify any of the zones are missing
# samples, for further debug information the xtalk_results struct should be
# referred to. This warning is for notification only, the xtalk pulse and
# shape have still been generated"""
VL53L1_WARNING_XTALK_NO_SAMPLES_FOR_GRADIENT =                              -39
# """Thrown to notify that some of teh xtalk samples used for gradient
# generation did not yield valid ranging pulse data while attempting to
# measure the xtalk signal in vl53l1_run_xtalk_extract(). This can signify
# that any one of the zones 0-3 yielded no successful samples. The
# xtalk_results struct should be referred to for further debug info. This
# warning is for notification only, the xtalk pulse and shape have still
# been generated."""
VL53L1_WARNING_XTALK_SIGMA_LIMIT_FOR_GRADIENT =                             -40
# """Thrown to notify that some of the xtalk samples used for gradient
# generation did not pass the sigma limit check while attempting to
# measure the xtalk signal in vl53l1_run_xtalk_extract(). This can signify
# that any one of the zones 0-3 yielded an avg sigma_mm value > the limit.
# The xtalk_results struct should be referred to for further debug info.
# This warning is for notification only, the xtalk pulse and shape have
# still been generated."""
VL53L1_ERROR_NOT_IMPLEMENTED =                                              -41
# """Tells requested functionality has not been implemented yet or not
# compatible with the device"""
VL53L1_ERROR_PLATFORM_SPECIFIC_START =                                      -60
# """Tells the starting code for platform
#    @} VL53L1_define_Error_group"""

Class

QwiicVL53L1X() or QwiicVL53L1X(i2caddr)
This Python package operates as a class object, allowing new instances of that type to be made. An __init__() constructor is used that creates a connection to an I2C device over the I2C bus using the default or specified I2C address.

The Constructor

A constructor is a special kind of method used to initialize (assign values to) the data members needed by the object when it is created.

__init__(address=None, i2c_driver=None)

Input: value
The value of the device address. If not defined, the Python package will use the default I2C address (0x77) stored under _AVAILABLE_I2C_ADDRESS variable.
Input: i2c_driver
Loads the specified I2C driver; by default the Qwiic I2C driver is used: qwiic_i2c.getI2CDriver(). Users should use the default I2C driver and leave this field blank.
Output: Boolean

True: Connected to I2C device on the default (or specified) address.
False: No device found or connected.

Functions

A function that is an attribute of the class, which defines a method for instances of that class. In simple terms, they are objects for the operations (or methods) of the class.

.init_sensor(address)
Initialize the sensor with default values

Input: value
The value of the device address. If not defined, the Python package will use the default I2C address (0x77) stored under _AVAILABLE_I2C_ADDRESS variable.
Output: Boolean

True: The initialization was successful.

.sensor_init()
Loads the 135 bytes of default configuration values to initialize the sensor.

Output: Boolean

True: Configuration successful.
False: Configuration failed.

.get_distance()
This function returns the distance measured by the sensor in mm.

Output: Integer

Returns distance measured by the sensor in mm.

.get_sw_version()
This function returns the SW driver version.

Input: List

[major, minor, build, revision]

.set_i2c_address(new_address)
This function sets the sensor I2C address used in case multiple devices application, default address 0x29 (0x52 >> 1).

Input: Value

I2C address to change device to.

.clear_interrupt()
This function clears the interrupt, to be called after a ranging data reading to arm the interrupt for the next data ready event.

.set_interrupt_polarity(NewPolarity)
This function programs the interrupt polarity

Input: Value

0: Active Low
1: Active High (Default)

.get_interrupt_polarity()
This function returns the current interrupt polarity.

Output: Value

0: Active Low
1: Active High (Default)

.start_ranging()
This function starts the ranging distance operation The ranging operation is continuous. The clear interrupt has to be done after each get data to allow the interrupt to raise when the next data is ready:

    0: Active Low
    1: Active High (Default)

use set_interrupt_polarity() to change the interrupt polarity if required.

.stop_ranging()
This function stops the ranging.

.check_for_data_ready()
This function checks if the new ranging data is available by polling the dedicated register.

Output: Value

0: Not Ready
1: Ready

.set_timing_budget_in_ms(TimingBudgetInMs)
This function programs the timing budget in ms.

Input: Value (Predefined)

15
20
33
50
100 (Default)
200
500

.get_timing_budget_in_ms()
This function returns the current timing budget in ms.

Output: Value

Timing budget in ms.

.set_distance_mode(DM)
This function programs the distance mode (1=short, 2=long).

Input: Value

1: Short mode max distance is limited to 1.3 m but better ambient immunity.
2: Long mode can range up to 4 m in the dark with 200 ms timing budget (Default)

.get_distance_mode()
This function returns the current distance mode (1=short, 2=long).

Output: Value

1: Short mode max distance is limited to 1.3 m but better ambient immunity.
2: Long mode can range up to 4 m in the dark with 200 ms timing budget (Default)

.set_inter_measurement_in_ms(InterMeasMs)
This function programs the Intermeasurement period in ms.

Input: Value

Intermeasurement period must be >/= timing budget. This condition is not checked by the API, the customer has the duty to check the condition. Default = 100 ms

.get_inter_measurement_in_ms()
This function returns the Intermeasurement period in ms.

Input: Integer

Intermeasurement period in ms.

.boot_state()
This function returns the boot state of the device (1:booted, 0:not booted)

Output: Integer

0: Booted
2: Not Booted

.get_sensor_id()
This function returns the sensor id, sensor Id must be 0xEEAC

Output: Integer

Sensor ID

.get_signal_per_spad()
This function returns the returned signal per SPAD (Single Photon Avalanche Diode) in kcps/SPAD (kcps stands for Kilo Count Per Second).

Output: Integer

Signal per SPAD (Kilo Count Per Second per Single Photon Avalanche Diode).

.get_ambient_per_spad()
This function returns the ambient per (Single Photon Avalanche Diode) in kcps/SPAD (kcps stands for Kilo Count Per Second).

Output: Integer

Ambient per SPAD.

.get_signal_rate()
This function returns the returned signal in kcps (Kilo Count Per Second).

Output: Value

Signal in kcps.

.get_spad_nb()
This function returns the current number of enabled SPADs (Single Photon Avalanche Diodes).

Output: Value

Number of enabled SPADs.

.get_ambient_rate()
This function returns the ambient rate in kcps (Kilo Count Per Second).

Output: Value

Ambient rate in kcps.

.get_range_status()
This function returns the ranging status error.

Output: Value (Ranging status error)

0: No Error
1: Sigma Failed
2: Signal Failed
7: Wrap-around

.set_offset(OffsetValue)
This function programs the offset correction in mm.

Input: Value

The offset correction value to program in mm.

.get_offset()
This function returns the programmed offset correction value in mm.

Output: Integer

Offset correction value in mm.

.set_xtalk(XtalkValue)
This function programs the xtalk correction value in cps (Count Per Second). This is the number of photons reflected back from the cover glass in cps.

Input: Integer

Xtalk correction value in count per second to avoid float type.

.get_xtalk()
This function returns the current programmed xtalk correction value in cps (Count Per Second).

Output: Value

Xtalk correction value in cps.

.set_distance_threshold(ThreshLow, ThreshHigh, Window, IntOnNoTarget)
This function programs the threshold detection mode.

Input: Value

The threshold under which one the device raises an interrupt if Window = 0.

Input: Value

The threshold above which one the device raises an interrupt if Window = 1.

Input: Value
Window detection mode:

0: Below
1: Above
2: Out
3: In

Input: 1

No longer used - just set to 1

Example:
  • self.set_distance_threshold(100,300,0,1): Below 100
  • self.set_distance_threshold(100,300,1,1): Above 300
  • self.set_distance_threshold(100,300,2,1): Out of window
  • self.set_distance_threshold(100,300,3,1): In window

.get_distance_threshold_window()
This function returns the window detection mode (0=below 1=above 2=out 3=in).

Output: Integer
Window detection mode:

0: Below
1: Above
2: Out
3: In

.get_distance_threshold_low()
This function returns the low threshold in mm.

Output: Integer

Low threshold in mm.

.get_distance_threshold_high()
This function returns the high threshold in mm.

Output: Integer

High threshold in mm.

.set_roi(X, Y, OpticalCenter = 199)
This function programs the ROI (Region of Interest). The height and width of the ROI (X, Y) are set in SPADs (Single Photon Avalanche Diodes); the smallest acceptable ROI size = 4 (4 x 4). The optical center is set based on table below. To set the center, use the pad that is to the right and above (i.e. upper right of) the exact center of the region you'd like to measure as your optical center.

Table of Optical Centers:

128,136,144,152,160,168,176,184,  192,200,208,216,224,232,240,248
129,137,145,153,161,169,177,185,  193,201,209,217,225,233,241,249
130,138,146,154,162,170,178,186,  194,202,210,218,226,234,242,250
131,139,147,155,163,171,179,187,  195,203,211,219,227,235,243,251
132,140,148,156,164,172,180,188,  196,204,212,220,228,236,244,252
133,141,149,157,165,173,181,189,  197,205,213,221,229,237,245,253
134,142,150,158,166,174,182,190,  198,206,214,222,230,238,246,254
135,143,151,159,167,175,183,191,  199,207,215,223,231,239,247,255

127,119,111,103,095,087,079,071,  063,055,047,039,031,023,015,007
126,118,110,102,094,086,078,070,  062,054,046,038,030,022,014,006
125,117,109,101,093,085,077,069,  061,053,045,037,029,021,013,005
124,116,108,100,092,084,076,068,  060,052,044,036,028,020,012,004
123,115,107,099,091,083,075,067,  059,051,043,035,027,019,011,003
122,114,106,098,090,082,074,066,  058,050,042,034,026,018,010,002
121,113,105,097,089,081,073,065,  057,049,041,033,025,017,009,001
120,112,104,096,088,080,072,064,  056,048,040,032,024,016,008,0 Pin 1

(Each SPAD has a number which is not obvious.)

Input: Value

ROI Width

Input: Value

ROI Height

Input: Value

The pad that is to the upper right of the exact center of the ROI (see table above). (Default = 199)

.get_roi_xy()
This function returns width X and height Y.

Output: List [ROI_X, ROI_Y]

Region of Interest Width (X) and Height (Y).

.set_signal_threshold(Signal)
This function programs a new signal threshold in kcps (Kilo Count Per Second).

Input: Value

Signal threshold in kcps (Default=1024 kcps)

.get_signal_threshold()
This function returns the current signal threshold in kcps (Kilo Count Per Second).

Output: Value

Signal threshold in kcps.

.set_sigma_threshold(Sigma)
This function programs a new sigma threshold in mm (default=15 mm).

Input: Value

Sigma threshold in mm (**default=15 mm**)

.get_sigma_threshold()
This function returns the current sigma threshold in mm

Output: Integer

Sigma threshold in mm.

.start_temperature_update()
This function performs the temperature calibration. It is recommended to call this function any time the temperature might have changed by more than 8 deg C without sensor ranging activity for an extended period.

.calibrate_offset(TargetDistInMm)
This function performs the offset calibration. The function returns the offset value found and programs the offset compensation into the device.

Input: Value

Target distance in mm, ST recommended 100 mm. (Target reflectance = grey 17%)

Output: Boolean

0: Success
!0: Failed

.calibrate_xtalk(TargetDistInMm)
This function performs the xtalk calibration. The function returns the xtalk value found and programs the xtalk compensation to the device

Input: Value

Target distance in mm (the distance where the sensor starts to "under range" due to the influence of the photons reflected back from the cover glass becoming strong; also called the inflection point). (Target reflectance = grey 17%)

Output: Boolean

0: Success
!0: Failed

Upgrading the Package

In the future, changes to the Python package might be made. Updating the installed packages has to be done individually for each package (i.e. sub-modules and dependencies won't update automatically and must be updated manually). For the sparkfun-qwiic-vl53l1x Python package, use the following command (use pip for Python 2):

For all users (note: the user must have sudo privileges):

language:bash
sudo pip3 install --upgrade sparkfun-qwiic-vl53l1x

For the current user:

language:bash
pip3 install --upgrade sparkfun-qwiic-vl53l1x