|
| 1 | +# SPDX-FileCopyrightText: 2022 wrdaigle for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +""" |
| 6 | +VL53L4CD multiple sensor I2C set_address demo. |
| 7 | +
|
| 8 | +This example is written for two sensors, but it can easily be modified to include more. |
| 9 | +
|
| 10 | +NOTE: A multitude of sensors may require more current than the on-board 3V regulator can output. |
| 11 | +The typical current consumption during active range readings is about 19 mA per sensor. |
| 12 | +""" |
| 13 | + |
| 14 | +import time |
| 15 | +import board |
| 16 | +import digitalio |
| 17 | +import adafruit_vl53l4cd |
| 18 | + |
| 19 | +# Define the I2C pins. |
| 20 | +i2c = board.I2C() |
| 21 | + |
| 22 | +xshut = [ |
| 23 | + # Update the D6 and D5 pins to match the pins to which you wired your sensor XSHUT pins. |
| 24 | + digitalio.DigitalInOut(board.D6), |
| 25 | + digitalio.DigitalInOut(board.D5), |
| 26 | + # Add more VL53L4CD sensors by defining their XSHUT pins here. |
| 27 | +] |
| 28 | + |
| 29 | +for shutdown_pin in xshut: |
| 30 | + # Set the shutdown pins to output, and pull them low. |
| 31 | + shutdown_pin.switch_to_output(value=False) |
| 32 | + # These pins are active when Low, meaning: |
| 33 | + # If the output signal is LOW, then the VL53L4CD sensor is off. |
| 34 | + # If the output signal is HIGH, then the VL53L4CD sensor is on. |
| 35 | +# All VL53L4CD sensors are now off. |
| 36 | + |
| 37 | +# Create a list to be used for the array of VL53L4CD sensors. |
| 38 | +vl53l4cd_list = [] |
| 39 | + |
| 40 | +# Change the address of the additional VL53L4CD sensors. |
| 41 | +for pin_number, shutdown_pin in enumerate(xshut): |
| 42 | + # Turn on the VL53L4CD sensors to allow hardware check. |
| 43 | + shutdown_pin.value = True |
| 44 | + # Instantiate the VL53L4CD I2C object and insert it into the VL53L4CD list. |
| 45 | + # This also performs VL53L4CD hardware check. |
| 46 | + sensor_i2c = adafruit_vl53l4cd.VL53L4CD(i2c) |
| 47 | + vl53l4cd_list.append(sensor_i2c) |
| 48 | + # This ensures no address change on one sensor board, specifically the last one in the series. |
| 49 | + if pin_number < len(xshut) - 1: |
| 50 | + # The default address is 0x29. Update it to an address that is not already in use. |
| 51 | + sensor_i2c.set_address(pin_number + 0x30) |
| 52 | + |
| 53 | +# Print the various sensor I2C addresses to the serial console. |
| 54 | +if i2c.try_lock(): |
| 55 | + print("Sensor I2C addresses:", [hex(x) for x in i2c.scan()]) |
| 56 | + i2c.unlock() |
| 57 | + |
| 58 | +# Start ranging for sensor data collection. |
| 59 | +for sensor in vl53l4cd_list: |
| 60 | + sensor.start_ranging() |
| 61 | +while True: |
| 62 | + # Extract the appropriate data from the current list, and print |
| 63 | + # the sensor distance readings for all available sensors. |
| 64 | + for sensor_number, sensor in enumerate(vl53l4cd_list): |
| 65 | + if sensor.data_ready: |
| 66 | + print("Sensor {}: {}".format(sensor_number + 1, sensor.distance)) |
| 67 | + sensor.clear_interrupt() |
| 68 | + time.sleep(0.5) |
0 commit comments