Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2022 Carter Nelson for Adafruit Industries
3#
4# SPDX-License-Identifier: Unlicense
5# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
6# SPDX-FileCopyrightText: Copyright (c) 2021 Carter Nelson for Adafruit Industries
7#
8# SPDX-License-Identifier: Unlicense
9
10# Simple demo of the VL53L4CD distance sensor.
11# Will print the sensed range/distance every second.
12
13import board
14import adafruit_vl53l4cd
15
16i2c = board.I2C() # uses board.SCL and board.SDA
17# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
18
19vl53 = adafruit_vl53l4cd.VL53L4CD(i2c)
20
21# OPTIONAL: can set non-default values
22vl53.inter_measurement = 0
23vl53.timing_budget = 200
24
25print("VL53L4CD Simple Test.")
26print("--------------------")
27model_id, module_type = vl53.model_info
28print("Model ID: 0x{:0X}".format(model_id))
29print("Module Type: 0x{:0X}".format(module_type))
30print("Timing Budget: {}".format(vl53.timing_budget))
31print("Inter-Measurement: {}".format(vl53.inter_measurement))
32print("--------------------")
33
34vl53.start_ranging()
35
36while True:
37 while not vl53.data_ready:
38 pass
39 vl53.clear_interrupt()
40 print("Distance: {} cm".format(vl53.distance))
Use set_address to update the I2C address of additional connected sensors.
1# SPDX-FileCopyrightText: 2022 wrdaigle for Adafruit Industries
2# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
3#
4# SPDX-License-Identifier: MIT
5"""
6VL53L4CD 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_vl53l4cd
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 VL53L4CD 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 VL53L4CD sensor is off.
35 # If the output signal is HIGH, then the VL53L4CD sensor is on.
36# All VL53L4CD sensors are now off.
37
38# Create a list to be used for the array of VL53L4CD sensors.
39vl53l4cd_list = []
40
41# Change the address of the additional VL53L4CD sensors.
42for pin_number, shutdown_pin in enumerate(xshut):
43 # Turn on the VL53L4CD sensors to allow hardware check.
44 shutdown_pin.value = True
45 # Instantiate the VL53L4CD I2C object and insert it into the VL53L4CD list.
46 # This also performs VL53L4CD hardware check.
47 sensor_i2c = adafruit_vl53l4cd.VL53L4CD(i2c)
48 vl53l4cd_list.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 vl53l4cd_list:
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(vl53l4cd_list):
66 if sensor.data_ready:
67 print("Sensor {}: {}".format(sensor_number + 1, sensor.distance))
68 sensor.clear_interrupt()
69 time.sleep(0.5)