Skip to content

Commit 186b055

Browse files
authored
Merge pull request #6 from kattni/update-multi-breakout-demo
Update multi-sensor set_address example.
2 parents c18367c + 130e006 commit 186b055

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

docs/examples.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/vl53l1x_simpletest.py
77
:caption: examples/vl53l1x_simpletest.py
88
:linenos:
9+
10+
Use set_address to update the I2C address of additional connected sensors.
11+
12+
.. literalinclude:: ../examples/vl53l1x_set_address_multiple_sensors.py
13+
:caption: examples/vl53l1x_set_address_multiple_sensors.py
14+
:linenos:

examples/vl53l1x_multiple_on_same_i2c_bus.py

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
# 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 VL53L1X 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 VL53L1X sensor is off.
34+
# If the output signal is HIGH, then the VL53L1X sensor is on.
35+
# All VL53L1X sensors are now off.
36+
37+
# Create a list to be used for the array of VL53L1X sensors.
38+
vl53l1x = []
39+
40+
# Change the address of the additional VL53L1X sensors.
41+
for pin_number, shutdown_pin in enumerate(xshut):
42+
# Turn on the VL53L1X sensors to allow hardware check.
43+
shutdown_pin.value = True
44+
# Instantiate the VL53L1X I2C object and insert it into the vl53l1x list.
45+
# This also performs VL53L1X hardware check.
46+
sensor_i2c = adafruit_vl53l1x.VL53L1X(i2c)
47+
vl53l1x.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 vl53l1x:
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(vl53l1x):
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

Comments
 (0)