Simple test

Ensure your device works with this simple test.

examples/vl53l0x_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of the VL53L0X distance sensor.
 5# Will print the sensed range/distance every second.
 6import time
 7
 8import board
 9import busio
10
11import adafruit_vl53l0x
12
13# Initialize I2C bus and sensor.
14i2c = busio.I2C(board.SCL, board.SDA)
15vl53 = adafruit_vl53l0x.VL53L0X(i2c)
16
17# Optionally adjust the measurement timing budget to change speed and accuracy.
18# See the example here for more details:
19#   https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Single/Single.ino
20# For example a higher speed but less accurate timing budget of 20ms:
21# vl53.measurement_timing_budget = 20000
22# Or a slower but more accurate timing budget of 200ms:
23# vl53.measurement_timing_budget = 200000
24# The default timing budget is 33ms, a good compromise of speed and accuracy.
25
26# Main loop will read the range and print it every second.
27while True:
28    print(f"Range: {vl53.range}mm")
29    time.sleep(1.0)

Multiple VL53L0X on Same I2C Bus

Copy “../examples/vl53l0x_multiple_sensors.py” to your “CIRCUITPY” drive, then run the script with from vl53l0x_multiple_sensors import *

examples/vl53l0x_multiple_sensors.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example of how to use the adafruit_vl53l0x library to change the assigned address of
 6multiple VL53L0X sensors on the same I2C bus. This example only focuses on 2 VL53L0X
 7sensors, but can be modified for more. BE AWARE: a multitude of sensors may require
 8more current than the on-board 3V regulator can output (typical current consumption during
 9active range readings is about 19 mA per sensor).
10"""
11
12import time
13
14import board
15from digitalio import DigitalInOut
16
17from adafruit_vl53l0x import VL53L0X
18
19# declare the singleton variable for the default I2C bus
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
23# declare the digital output pins connected to the "SHDN" pin on each VL53L0X sensor
24xshut = [
25    DigitalInOut(board.D7),
26    DigitalInOut(board.D9),
27    # add more VL53L0X sensors by defining their SHDN pins here
28]
29
30for power_pin in xshut:
31    # make sure these pins are a digital output, not a digital input
32    power_pin.switch_to_output(value=False)
33    # These pins are active when Low, meaning:
34    #   if the output signal is LOW, then the VL53L0X sensor is off.
35    #   if the output signal is HIGH, then the VL53L0X sensor is on.
36# all VL53L0X sensors are now off
37
38# initialize a list to be used for the array of VL53L0X sensors
39vl53 = []
40
41# now change the addresses of the VL53L0X sensors
42for i, power_pin in enumerate(xshut):
43    # turn on the VL53L0X to allow hardware check
44    power_pin.value = True
45    # instantiate the VL53L0X sensor on the I2C bus & insert it into the "vl53" list
46    vl53.insert(i, VL53L0X(i2c))  # also performs VL53L0X hardware check
47    # no need to change the address of the last VL53L0X sensor
48    if i < len(xshut) - 1:
49        # default address is 0x29. Change that to something else
50        vl53[i].set_address(i + 0x30)  # address assigned should NOT be already in use
51# there is a helpful list of pre-designated I2C addresses for various I2C devices at
52# https://learn.adafruit.com/i2c-addresses/the-list
53# According to this list 0x30-0x34 are available, although the list may be incomplete.
54# In the python REPR, you can scan for all I2C devices that are attached and detirmine
55# their addresses using:
56#   >>> import board
57#   >>> i2c = board.I2C()
58#   >>> if i2c.try_lock():
59#   >>>     [hex(x) for x in i2c.scan()]
60#   >>>     i2c.unlock()
61
62
63def detect_range(count=5):
64    """take count=5 samples"""
65    while count:
66        for index, sensor in enumerate(vl53):
67            print(f"Sensor {index + 1} Range: {sensor.range}mm")
68        time.sleep(1.0)
69        count -= 1
70
71
72print(
73    "Multiple VL53L0X sensors' addresses are assigned properly\n"
74    "execute detect_range() to read each sensors range readings"
75)

Continuous mode

Simple demo of the VL53L0X distance sensor with continuous mode.

examples/vl53l0x_simplecontinuous.py
 1# SPDX-FileCopyrightText: 2021 Smankusors for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of the VL53L0X distance sensor with continuous mode.
 5# Will print the sensed range/distance as fast as possible.
 6import time
 7
 8import board
 9import busio
10
11import adafruit_vl53l0x
12
13# Initialize I2C bus and sensor.
14i2c = busio.I2C(board.SCL, board.SDA)
15vl53 = adafruit_vl53l0x.VL53L0X(i2c)
16
17# Optionally adjust the measurement timing budget to change speed and accuracy.
18# See the example here for more details:
19#   https://github.com/pololu/vl53l0x-arduino/blob/master/examples/Single/Single.ino
20# For example a higher speed but less accurate timing budget of 20ms:
21# vl53.measurement_timing_budget = 20000
22# Or a slower but more accurate timing budget of 200ms:
23vl53.measurement_timing_budget = 200000
24# The default timing budget is 33ms, a good compromise of speed and accuracy.
25
26# You will see the benefit of continous mode if you set the measurement timing
27# budget very high, while your program doing something else. When your program done
28# with something else, and the sensor already calculated the distance, the result
29# will return instantly, instead of waiting the sensor measuring first.
30
31# Main loop will read the range and print it every second.
32with vl53.continuous_mode():
33    while True:
34        # try to adjust the sleep time (simulating program doing something else)
35        # and see how fast the sensor returns the range
36        time.sleep(0.1)
37
38        curTime = time.time()
39        print(f"Range: {vl53.range}mm ({time.time() - curTime:.2f}ms)")

Multiple VL53L0X on Same I2C Bus and with continuous mode

Example of how to change the assigned address of multiple VL53L0X sensors on the same I2C bus and use

examples/vl53l0x_multiple_sensors_continuous.py
  1# SPDX-FileCopyrightText: 2021 Smankusors for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3
  4"""
  5Example of how to use the adafruit_vl53l0x library to change the assigned address of
  6multiple VL53L0X sensors on the same I2C bus. This example only focuses on 2 VL53L0X
  7sensors, but can be modified for more. BE AWARE: a multitude of sensors may require
  8more current than the on-board 3V regulator can output (typical current consumption during
  9active range readings is about 19 mA per sensor).
 10
 11This example like vl53l0x_multiple_sensors, but this with sensors in continuous mode.
 12So you don't need to wait the sensor to do range measurement and return the distance
 13for you.
 14
 15For example, you have 2 VL53L0X sensors, with timing budget of 200ms, on single mode.
 16When you want to get distance from sensor #1, sensor #2 will idle because waiting
 17for sensor #1 completes the range measurement. You could do multithreading so you
 18can ask both the sensor at the same time, but it's quite expensive.
 19
 20When you use continuous mode, the sensor will always do range measurement after it
 21completes. So when you want to get the distance from both of the device, you don't
 22need to wait 400ms, just 200ms for both of the sensors.
 23"""
 24
 25import time
 26
 27import board
 28from digitalio import DigitalInOut
 29
 30from adafruit_vl53l0x import VL53L0X
 31
 32# declare the singleton variable for the default I2C bus
 33i2c = board.I2C()  # uses board.SCL and board.SDA
 34# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
 35
 36# declare the digital output pins connected to the "SHDN" pin on each VL53L0X sensor
 37xshut = [
 38    DigitalInOut(board.D17),
 39    DigitalInOut(board.D18),
 40    # add more VL53L0X sensors by defining their SHDN pins here
 41]
 42
 43for power_pin in xshut:
 44    # make sure these pins are a digital output, not a digital input
 45    power_pin.switch_to_output(value=False)
 46    # These pins are active when Low, meaning:
 47    #   if the output signal is LOW, then the VL53L0X sensor is off.
 48    #   if the output signal is HIGH, then the VL53L0X sensor is on.
 49# all VL53L0X sensors are now off
 50
 51# initialize a list to be used for the array of VL53L0X sensors
 52vl53 = []
 53
 54# now change the addresses of the VL53L0X sensors
 55for i, power_pin in enumerate(xshut):
 56    # turn on the VL53L0X to allow hardware check
 57    power_pin.value = True
 58    # instantiate the VL53L0X sensor on the I2C bus & insert it into the "vl53" list
 59    vl53.insert(i, VL53L0X(i2c))  # also performs VL53L0X hardware check
 60
 61    # start continous mode
 62    vl53[i].start_continous()
 63
 64    # you will see the benefit of continous mode if you set the measurement timing
 65    # budget very high.
 66    # vl53[i].measurement_timing_budget = 2000000
 67
 68    # no need to change the address of the last VL53L0X sensor
 69    if i < len(xshut) - 1:
 70        # default address is 0x29. Change that to something else
 71        vl53[i].set_address(i + 0x30)  # address assigned should NOT be already in use
 72# there is a helpful list of pre-designated I2C addresses for various I2C devices at
 73# https://learn.adafruit.com/i2c-addresses/the-list
 74# According to this list 0x30-0x34 are available, although the list may be incomplete.
 75# In the python REPR, you can scan for all I2C devices that are attached and detirmine
 76# their addresses using:
 77#   >>> import board
 78#   >>> i2c = board.I2C()  # uses board.SCL and board.SDA
 79#   >>> if i2c.try_lock():
 80#   >>>     [hex(x) for x in i2c.scan()]
 81#   >>>     i2c.unlock()
 82
 83
 84def detect_range(count=5):
 85    """take count=5 samples"""
 86    while count:
 87        for index, sensor in enumerate(vl53):
 88            print(f"Sensor {index + 1} Range: {sensor.range}mm")
 89        time.sleep(1.0)
 90        count -= 1
 91
 92
 93def stop_continuous():
 94    """this is not required, if you use XSHUT to reset the sensor.
 95    unless if you want to save some energy
 96    """
 97    for sensor in vl53:
 98        sensor.stop_continuous()
 99
100
101if __name__ == "__main__":
102    detect_range()
103    stop_continuous()
104else:
105    print(
106        "Multiple VL53L0X sensors' addresses are assigned properly\n"
107        "execute detect_range() to read each sensors range readings.\n"
108        "When you are done with readings, execute stop_continuous()\n"
109        "to stop the continuous mode."
110    )

DisplayIO Simpletest

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

examples/vl53l0x_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6# Simple demo of the VL53L0X distance sensor using a built-in display.
 7import time
 8
 9import board
10from adafruit_display_text.bitmap_label import Label
11from displayio import Group
12from terminalio import FONT
13
14import adafruit_vl53l0x
15
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20vl53 = adafruit_vl53l0x.VL53L0X(i2c)
21
22# Create a Label to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (4, board.DISPLAY.height // 2)
29
30# add the label to the main_group
31main_group.append(display_output_label)
32
33# set the main_group as the root_group of the built-in DISPLAY
34board.DISPLAY.root_group = main_group
35
36# begin main loop
37while True:
38    # Update the label.text property to change the text on the display
39    display_output_label.text = f"Range: {vl53.range}mm"
40    # wait for a bit
41    time.sleep(1.0)