Simple test

Ensure your device works with this simple test.

examples/vl53l1x_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2021 Carter Nelson for Adafruit Industries
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6# Simple demo of the VL53L1X distance sensor.
 7# Will print the sensed range/distance every second.
 8
 9import time
10import board
11import adafruit_vl53l1x
12
13i2c = board.I2C()  # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
15
16vl53 = adafruit_vl53l1x.VL53L1X(i2c)
17
18# OPTIONAL: can set non-default values
19vl53.distance_mode = 1
20vl53.timing_budget = 100
21
22print("VL53L1X Simple Test.")
23print("--------------------")
24model_id, module_type, mask_rev = vl53.model_info
25print("Model ID: 0x{:0X}".format(model_id))
26print("Module Type: 0x{:0X}".format(module_type))
27print("Mask Revision: 0x{:0X}".format(mask_rev))
28print("Distance Mode: ", end="")
29if vl53.distance_mode == 1:
30    print("SHORT")
31elif vl53.distance_mode == 2:
32    print("LONG")
33else:
34    print("UNKNOWN")
35print("Timing Budget: {}".format(vl53.timing_budget))
36print("--------------------")
37
38vl53.start_ranging()
39
40while True:
41    if vl53.data_ready:
42        print("Distance: {} cm".format(vl53.distance))
43        vl53.clear_interrupt()
44        time.sleep(1.0)

Use set_address to update the I2C address of additional connected sensors.

examples/vl53l1x_set_address_multiple_sensors.py
 1# SPDX-FileCopyrightText: 2022 wrdaigle for Adafruit Industries
 2# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
 3#
 4# SPDX-License-Identifier: MIT
 5"""
 6VL53L1X multiple sensor I2C set_address demo.
 7
 8This example is written for two sensors, but it can easily be modified to include more.
 9
10NOTE: A multitude of sensors may require more current than the on-board 3V regulator can output.
11The typical current consumption during active range readings is about 19 mA per sensor.
12"""
13
14import time
15import board
16import digitalio
17import adafruit_vl53l1x
18
19# Define the I2C pins.
20i2c = board.I2C()  # uses board.SCL and board.SDA
21# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
22
23xshut = [
24    # Update the D6 and D5 pins to match the pins to which you wired your sensor XSHUT pins.
25    digitalio.DigitalInOut(board.D6),
26    digitalio.DigitalInOut(board.D5),
27    # Add more VL53L1X sensors by defining their XSHUT pins here.
28]
29
30for shutdown_pin in xshut:
31    # Set the shutdown pins to output, and pull them low.
32    shutdown_pin.switch_to_output(value=False)
33    # These pins are active when Low, meaning:
34    #   If the output signal is LOW, then the VL53L1X sensor is off.
35    #   If the output signal is HIGH, then the VL53L1X sensor is on.
36# All VL53L1X sensors are now off.
37
38# Create a list to be used for the array of VL53L1X sensors.
39vl53l1x = []
40
41# Change the address of the additional VL53L1X sensors.
42for pin_number, shutdown_pin in enumerate(xshut):
43    # Turn on the VL53L1X sensors to allow hardware check.
44    shutdown_pin.value = True
45    # Instantiate the VL53L1X I2C object and insert it into the vl53l1x list.
46    # This also performs VL53L1X hardware check.
47    sensor_i2c = adafruit_vl53l1x.VL53L1X(i2c)
48    vl53l1x.append(sensor_i2c)
49    # This ensures no address change on one sensor board, specifically the last one in the series.
50    if pin_number < len(xshut) - 1:
51        # The default address is 0x29. Update it to an address that is not already in use.
52        sensor_i2c.set_address(pin_number + 0x30)
53
54# Print the various sensor I2C addresses to the serial console.
55if i2c.try_lock():
56    print("Sensor I2C addresses:", [hex(x) for x in i2c.scan()])
57    i2c.unlock()
58
59# Start ranging for sensor data collection.
60for sensor in vl53l1x:
61    sensor.start_ranging()
62while True:
63    # Extract the appropriate data from the current list, and print
64    # the sensor distance readings for all available sensors.
65    for sensor_number, sensor in enumerate(vl53l1x):
66        if sensor.data_ready:
67            print("Sensor {}: {}".format(sensor_number + 1, sensor.distance))
68            sensor.clear_interrupt()
69    time.sleep(0.5)