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