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
10
11import board
12
13import adafruit_vl53l1x
14
15i2c = board.I2C()  # uses board.SCL and board.SDA
16# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
17
18vl53 = adafruit_vl53l1x.VL53L1X(i2c)
19
20# OPTIONAL: can set non-default values
21vl53.distance_mode = 1
22vl53.timing_budget = 100
23
24print("VL53L1X Simple Test.")
25print("--------------------")
26model_id, module_type, mask_rev = vl53.model_info
27print(f"Model ID: 0x{model_id:0X}")
28print(f"Module Type: 0x{module_type:0X}")
29print(f"Mask Revision: 0x{mask_rev:0X}")
30print("Distance Mode: ", end="")
31if vl53.distance_mode == 1:
32    print("SHORT")
33elif vl53.distance_mode == 2:
34    print("LONG")
35else:
36    print("UNKNOWN")
37print(f"Timing Budget: {vl53.timing_budget}")
38print("--------------------")
39
40vl53.start_ranging()
41
42while True:
43    if vl53.data_ready:
44        print(f"Distance: {vl53.distance} cm")
45        vl53.clear_interrupt()
46        time.sleep(1.0)

Multiple Sensors

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
15
16import board
17import digitalio
18
19import adafruit_vl53l1x
20
21# Define the I2C pins.
22i2c = board.I2C()  # uses board.SCL and board.SDA
23# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
24
25xshut = [
26    # Update the D6 and D5 pins to match the pins to which you wired your sensor XSHUT pins.
27    digitalio.DigitalInOut(board.D6),
28    digitalio.DigitalInOut(board.D5),
29    # Add more VL53L1X sensors by defining their XSHUT pins here.
30]
31
32for shutdown_pin in xshut:
33    # Set the shutdown pins to output, and pull them low.
34    shutdown_pin.switch_to_output(value=False)
35    # These pins are active when Low, meaning:
36    #   If the output signal is LOW, then the VL53L1X sensor is off.
37    #   If the output signal is HIGH, then the VL53L1X sensor is on.
38# All VL53L1X sensors are now off.
39
40# Create a list to be used for the array of VL53L1X sensors.
41vl53l1x = []
42
43# Change the address of the additional VL53L1X sensors.
44for pin_number, shutdown_pin in enumerate(xshut):
45    # Turn on the VL53L1X sensors to allow hardware check.
46    shutdown_pin.value = True
47    # Instantiate the VL53L1X I2C object and insert it into the vl53l1x list.
48    # This also performs VL53L1X hardware check.
49    sensor_i2c = adafruit_vl53l1x.VL53L1X(i2c)
50    vl53l1x.append(sensor_i2c)
51    # This ensures no address change on one sensor board, specifically the last one in the series.
52    if pin_number < len(xshut) - 1:
53        # The default address is 0x29. Update it to an address that is not already in use.
54        sensor_i2c.set_address(pin_number + 0x30)
55
56# Print the various sensor I2C addresses to the serial console.
57if i2c.try_lock():
58    print("Sensor I2C addresses:", [hex(x) for x in i2c.scan()])
59    i2c.unlock()
60
61# Start ranging for sensor data collection.
62for sensor in vl53l1x:
63    sensor.start_ranging()
64while True:
65    # Extract the appropriate data from the current list, and print
66    # the sensor distance readings for all available sensors.
67    for sensor_number, sensor in enumerate(vl53l1x):
68        if sensor.data_ready:
69            print(f"Sensor {sensor_number + 1}: {sensor.distance}")
70            sensor.clear_interrupt()
71    time.sleep(0.5)

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/vl53l1x_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# contributions by J Fletcher, adapting code by Prof Gallaugher:
 3#           https://www.youtube.com/watch?v=cdx1A1xoEWc&t=5s
 4# tested on ESP32-S3 Reverse TFT Feather:
 5#           https://www.adafruit.com/product/5691
 6# SPDX-License-Identifier: MIT
 7
 8import time
 9
10import board
11from adafruit_display_text.bitmap_label import Label
12from displayio import Group
13from terminalio import FONT
14
15import adafruit_vl53l1x
16
17# create a main_group to hold anything we want to show on the display.
18main_group = Group()
19
20# Create sensor object, communicating over the board's default I2C bus
21# i2c = board.I2C()  # uses board.SCL and board.SDA
22i2c = board.STEMMA_I2C()
23# For using the built-in STEMMA QT connector on a microcontroller
24vl53 = adafruit_vl53l1x.VL53L1X(i2c)
25
26# Create a Label to show the readings. If you have a very small
27# display you may need to change to scale=1.
28display_output_label = Label(FONT, text="", scale=1)
29
30# place the label near the top left corner with anchored positioning
31display_output_label.anchor_point = (0, 0)
32display_output_label.anchored_position = (4, 4)
33
34# add the label to the main_group
35main_group.append(display_output_label)
36
37# set the main_group as the root_group of the built-in DISPLAY
38board.DISPLAY.root_group = main_group
39# create a display object placeholder to be updated by the loop
40screen = f"Distance: {''}cm, {''}in, {''}ft"
41# initiate repeated sensor readings
42vl53.start_ranging()
43
44
45# begin main loop
46while True:
47    # There will be no values to populate at first, just the bare 'Distance: cm, in, ft' text
48    # Assuming the first 'try' succeeds, this will be updated once the loop starts over
49    display_output_label.text = screen
50
51    # This 'try' sequence will either update the displayed items with fresh data or repeat the
52    # last available data. VL53L1X sensors output `None` when no object reflects the laser,
53    # e.g., there is nothing within 4 meters, or when objects pass too quickly in and out of
54    # view (usually perpendicular to the field of vision).
55    try:
56        if vl53.distance:
57            # simple test to see there is a value to read; no value = exception
58            distance = vl53.distance
59            # sets the variable (used by the display) to the sensor data
60            inches = distance * 0.394
61            # VL53L1X outputs distance in metric, so we convert to imperial
62            screen = f"Distance: {distance: .1f}cm, {inches: .1f}in, {inches/12: .1f}ft"
63            # if we made it this far, we have new data to display!
64    except TypeError:
65        repeat_screen = screen
66        screen = repeat_screen
67        # if things went sideways, we repeat the previous loop's data so we can try again
68
69    time.sleep(0.25)