Skip to content

Commit d3ef19a

Browse files
authored
Merge pull request #7 from Neradoc/add-set-address
add set_address to change the I2C address
2 parents 4253517 + f3c193a commit d3ef19a

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

adafruit_vl53l4cd.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class VL53L4CD:
7171
"""Driver for the VL53L4CD distance sensor."""
7272

7373
def __init__(self, i2c, address=41):
74+
self._i2c = i2c
7475
self.i2c_device = i2c_device.I2CDevice(i2c, address)
7576
model_id, module_type = self.model_info
7677
if model_id != 0xEB or module_type != 0xAA:
@@ -394,3 +395,14 @@ def _read_register(self, address, length=1):
394395
i2c.write(struct.pack(">H", address))
395396
i2c.readinto(data)
396397
return data
398+
399+
def set_address(self, new_address):
400+
"""
401+
Set a new I2C address to the instantaited object. This is only called when using
402+
multiple VL53L4CD sensors on the same I2C bus (SDA & SCL pins). See also the
403+
`example <examples.html#id2>`_ for proper usage.
404+
"""
405+
self._write_register(
406+
_VL53L4CD_I2C_SLAVE_DEVICE_ADDRESS, struct.pack(">B", new_address)
407+
)
408+
self.i2c_device = i2c_device.I2CDevice(self._i2c, new_address)

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/vl53l4cd_simpletest.py
77
:caption: examples/vl53l4cd_simpletest.py
88
:linenos:
9+
10+
Use set_address to update the I2C address of additional connected sensors.
11+
12+
.. literalinclude:: ../examples/vl53l4cd_set_address_multiple_sensors.py
13+
:caption: examples/vl53l4cd_set_address_multiple_sensors.py
14+
:linenos:
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+
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

Comments
 (0)