Simple test
Ensure your device works with this simple test.
examples/vl53l4cd_simpletest.py
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
14
15import adafruit_vl53l4cd
16
17i2c = board.I2C() # uses board.SCL and board.SDA
18# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
19
20vl53 = adafruit_vl53l4cd.VL53L4CD(i2c)
21
22# OPTIONAL: can set non-default values
23vl53.inter_measurement = 0
24vl53.timing_budget = 200
25
26print("VL53L4CD Simple Test.")
27print("--------------------")
28model_id, module_type = vl53.model_info
29print(f"Model ID: 0x{model_id:0X}")
30print(f"Module Type: 0x{module_type:0X}")
31print(f"Timing Budget: {vl53.timing_budget}")
32print(f"Inter-Measurement: {vl53.inter_measurement}")
33print("--------------------")
34
35vl53.start_ranging()
36
37while True:
38 while not vl53.data_ready:
39 pass
40 vl53.clear_interrupt()
41 print(f"Distance: {vl53.distance} cm")
Use set_address to update the I2C address of additional connected sensors.
examples/vl53l4cd_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"""
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
15
16import board
17import digitalio
18
19import adafruit_vl53l4cd
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 VL53L4CD 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 VL53L4CD sensor is off.
37 # If the output signal is HIGH, then the VL53L4CD sensor is on.
38# All VL53L4CD sensors are now off.
39
40# Create a list to be used for the array of VL53L4CD sensors.
41vl53l4cd_list = []
42
43# Change the address of the additional VL53L4CD sensors.
44for pin_number, shutdown_pin in enumerate(xshut):
45 # Turn on the VL53L4CD sensors to allow hardware check.
46 shutdown_pin.value = True
47 # Instantiate the VL53L4CD I2C object and insert it into the VL53L4CD list.
48 # This also performs VL53L4CD hardware check.
49 sensor_i2c = adafruit_vl53l4cd.VL53L4CD(i2c)
50 vl53l4cd_list.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 vl53l4cd_list:
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(vl53l4cd_list):
68 if sensor.data_ready:
69 print(f"Sensor {sensor_number + 1}: {sensor.distance}")
70 sensor.clear_interrupt()
71 time.sleep(0.5)