Skip to content

add set_address to change the I2C address #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions adafruit_vl53l4cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class VL53L4CD:
"""Driver for the VL53L4CD distance sensor."""

def __init__(self, i2c, address=41):
self._i2c = i2c
self.i2c_device = i2c_device.I2CDevice(i2c, address)
model_id, module_type = self.model_info
if model_id != 0xEB or module_type != 0xAA:
Expand Down Expand Up @@ -394,3 +395,14 @@ def _read_register(self, address, length=1):
i2c.write(struct.pack(">H", address))
i2c.readinto(data)
return data

def set_address(self, new_address):
"""
Set a new I2C address to the instantaited object. This is only called when using
multiple VL53L4CD sensors on the same I2C bus (SDA & SCL pins). See also the
`example <examples.html#id2>`_ for proper usage.
"""
self._write_register(
_VL53L4CD_I2C_SLAVE_DEVICE_ADDRESS, struct.pack(">B", new_address)
)
self.i2c_device = i2c_device.I2CDevice(self._i2c, new_address)
6 changes: 6 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/vl53l4cd_simpletest.py
:caption: examples/vl53l4cd_simpletest.py
:linenos:

Use set_address to update the I2C address of additional connected sensors.

.. literalinclude:: ../examples/vl53l4cd_set_address_multiple_sensors.py
:caption: examples/vl53l4cd_set_address_multiple_sensors.py
:linenos:
68 changes: 68 additions & 0 deletions examples/vl53l4cd_set_address_multiple_sensors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-FileCopyrightText: 2022 wrdaigle for Adafruit Industries
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
VL53L4CD multiple sensor I2C set_address demo.

This example is written for two sensors, but it can easily be modified to include more.

NOTE: A multitude of sensors may require more current than the on-board 3V regulator can output.
The typical current consumption during active range readings is about 19 mA per sensor.
"""

import time
import board
import digitalio
import adafruit_vl53l4cd

# Define the I2C pins.
i2c = board.I2C()

xshut = [
# Update the D6 and D5 pins to match the pins to which you wired your sensor XSHUT pins.
digitalio.DigitalInOut(board.D6),
digitalio.DigitalInOut(board.D5),
# Add more VL53L4CD sensors by defining their XSHUT pins here.
]

for shutdown_pin in xshut:
# Set the shutdown pins to output, and pull them low.
shutdown_pin.switch_to_output(value=False)
# These pins are active when Low, meaning:
# If the output signal is LOW, then the VL53L4CD sensor is off.
# If the output signal is HIGH, then the VL53L4CD sensor is on.
# All VL53L4CD sensors are now off.

# Create a list to be used for the array of VL53L4CD sensors.
vl53l4cd_list = []

# Change the address of the additional VL53L4CD sensors.
for pin_number, shutdown_pin in enumerate(xshut):
# Turn on the VL53L4CD sensors to allow hardware check.
shutdown_pin.value = True
# Instantiate the VL53L4CD I2C object and insert it into the VL53L4CD list.
# This also performs VL53L4CD hardware check.
sensor_i2c = adafruit_vl53l4cd.VL53L4CD(i2c)
vl53l4cd_list.append(sensor_i2c)
# This ensures no address change on one sensor board, specifically the last one in the series.
if pin_number < len(xshut) - 1:
# The default address is 0x29. Update it to an address that is not already in use.
sensor_i2c.set_address(pin_number + 0x30)

# Print the various sensor I2C addresses to the serial console.
if i2c.try_lock():
print("Sensor I2C addresses:", [hex(x) for x in i2c.scan()])
i2c.unlock()

# Start ranging for sensor data collection.
for sensor in vl53l4cd_list:
sensor.start_ranging()
while True:
# Extract the appropriate data from the current list, and print
# the sensor distance readings for all available sensors.
for sensor_number, sensor in enumerate(vl53l4cd_list):
if sensor.data_ready:
print("Sensor {}: {}".format(sensor_number + 1, sensor.distance))
sensor.clear_interrupt()
time.sleep(0.5)