Skip to content

Commit 08c0e73

Browse files
committed
Update multi-sensor set_address example.
1 parent c18367c commit 08c0e73

File tree

2 files changed

+67
-73
lines changed

2 files changed

+67
-73
lines changed

examples/vl53l1x_multiple_on_same_i2c_bus.py

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
VL53L1X 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_vl53l1x
18+
19+
# Define the I2C pins.
20+
i2c = board.I2C()
21+
22+
xshut = [
23+
digitalio.DigitalInOut(board.D6),
24+
digitalio.DigitalInOut(board.D25),
25+
# Add more VL53L1X sensors by defining their XSHUT pins here.
26+
]
27+
28+
for shutdown_pin in xshut:
29+
# Set the shutdown pins to output, and pull them low.
30+
shutdown_pin.switch_to_output(value=False)
31+
# These pins are active when Low, meaning:
32+
# If the output signal is LOW, then the VL53L1X sensor is off.
33+
# If the output signal is HIGH, then the VL53L1X sensor is on.
34+
# All VL53L1X sensors are now off.
35+
36+
# Create a list to be used for the array of VL53L1X sensors.
37+
vl53l1x = []
38+
39+
# Change the address of the additional VL53L1X sensors.
40+
for pin_number, shutdown_pin in enumerate(xshut):
41+
# Turn on the VL53L1X sensors to allow hardware check.
42+
shutdown_pin.value = True
43+
# Instantiate the VL53L1X I2C object and insert it into the vl53l1x list.
44+
# This also performs VL53L1X hardware check.
45+
sensor_i2c = adafruit_vl53l1x.VL53L1X(i2c)
46+
vl53l1x.append(sensor_i2c)
47+
# This ensures no address change on one sensor board, specifically the last one in the series.
48+
if pin_number < len(xshut) - 1:
49+
# The default address is 0x29. Update it to an address that is not already in use.
50+
sensor_i2c.set_address(pin_number + 0x30)
51+
52+
# Print the various sensor I2C addresses to the serial console.
53+
if i2c.try_lock():
54+
print("Sensor I2C addresses:", [hex(x) for x in i2c.scan()])
55+
i2c.unlock()
56+
57+
# Start ranging for sensor data collection.
58+
for sensor in vl53l1x:
59+
sensor.start_ranging()
60+
while True:
61+
# Extract the appropriate data from the current list, and print
62+
# the sensor distance readings for all available sensors.
63+
for sensor_number, sensor in enumerate(vl53l1x):
64+
if sensor.data_ready:
65+
print("Sensor {}: {}".format(sensor_number + 1, sensor.distance))
66+
sensor.clear_interrupt()
67+
time.sleep(0.5)

0 commit comments

Comments
 (0)