Skip to content

Commit 97b755b

Browse files
committed
added set_address() and example usage
1 parent fe654d9 commit 97b755b

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

adafruit_vl53l0x.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,21 @@ def range(self):
453453
range_mm = self._read_u16(_RESULT_RANGE_STATUS + 10)
454454
self._write_u8(_SYSTEM_INTERRUPT_CLEAR, 0x01)
455455
return range_mm
456+
457+
def set_address(self, new_address):
458+
"""Set a new I2C address to the instantaited object. This is only called when using
459+
multiple VL53L0X sensors on the same I2C bus (SDA & SCL pins). See also the
460+
`example <examples.html#multiple-vl53l0x-on-same-i2c-bus>`_ for proper usage.
461+
462+
:param int new_address: The 8-bit `int` that is to be assigned to the VL53L0X sensor.
463+
The address that is assigned should NOT be already in use by another device on the
464+
I2C bus.
465+
466+
.. important:: To properly set the address to an individual VL53L0X sensor, you must
467+
first ensure that all other VL53L0X sensors on the same I2C bus are in their off state
468+
by pulling the "SHDN" pins LOW. When the "SHDN" pin is pulled HIGH again the
469+
default I2C address is 0x29. The "SHDN" pin is usually labeled "XSHUT" on
470+
non-Adafruit breakout boards.
471+
"""
472+
self._write_u8(_I2C_SLAVE_DEVICE_ADDRESS, new_address & 0x7f)
473+
self._device.device_address = new_address

docs/examples.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/vl53l0x_simpletest.py
77
:caption: examples/vl53l0x_simpletest.py
88
:linenos:
9+
10+
Multiple VL53L0X on Same I2C Bus
11+
--------------------------------
12+
13+
.. literalinclude:: ../examples/vl53l0x_multiple_sensors.py
14+
:caption: examples/vl53l0x_multiple_sensors.py
15+
:linenos:

examples/vl53l0x_multiple_sensors.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Example of how to use the adafruit_vl53l0x library to change the assigned address of
3+
multiple VL53L0X sensors on the same I2C bus. This example only focuses on 2 VL53L0X
4+
sensors, but can be modified for more. BE AWARE: a multitude of sensors may require
5+
more current than the on-board 3V regulator can output (typical current consumption during
6+
active range readings is about 19 mA per sensor).
7+
"""
8+
import time
9+
import board
10+
import busio
11+
from digitalio import DigitalInOut
12+
from adafruit_vl53l0x import VL53L0X
13+
14+
# declare the singleton variable for the default I2C bus
15+
i2c = busio.I2C(board.SCL, board.SDA)
16+
17+
# declare the digital output pins connected to the "SHDN" pin on each VL53L0X sensor (this
18+
# pin is labeled "XSHUT" on non-Adafruit breakout boards). Default behavior upon
19+
# instantiation is (direction=INPUT) + (pull=None) = LOW output signal
20+
x_shut = [
21+
DigitalInOut(board.D7),
22+
DigitalInOut(board.D9)
23+
]
24+
# idealy you might want to use an IO extender as these pins are only used to control the
25+
# VL53L0X's power state.
26+
27+
for power_pin in x_shut:
28+
# make sure these pins are an digital output, not a digital input
29+
power_pin.switch_to_output(value=False)
30+
# These pins are active when Low, meaning:
31+
# if the output signal is LOW, then the VL53L0X sensor is off.
32+
# if the output signal is HIGH, then the VL53L0X sensor is on.
33+
# (value=False) = LOW output signal; LOW output disables/shutsdown the VL53L0X
34+
# all VL53L0X sensors are now off
35+
36+
# initialize a list to be used for the array of VL53L0X sensors
37+
vl53 = []
38+
39+
# now change the addresses of the VL53L0X sensors
40+
for i, power_pin in enumerate(x_shut):
41+
# turn on the sensor to allow hardware check
42+
power_pin.value = True
43+
# instantiate the VL53L0X sensors on the I2C bus & insert it into the "vl53" list
44+
vl53.insert(i, VL53L0X(i2c)) # also performs hardware check
45+
# don't need to change the address of the last VL53L0X sensor
46+
if i < len(x_shut) - 1:
47+
# default address is 0x29. Change that to something else
48+
vl53[i].set_address(i + 0x30) # address assigned should not be already in use
49+
# there is a helpful list of pre-designated I2C addresses for various I2C devices at
50+
# https://learn.adafruit.com/i2c-addresses/the-list
51+
# According to this list 0x30-0x34 are available although the list may be outdated/incomplete.
52+
# you can scan for all I2C devices and detirmine their addresses using:
53+
# "i2cdetect 1 -y" (without quotes) on a Raspberry Pi terminal or
54+
# In the python REPR, execute the following commands:
55+
# >>> import busio
56+
# >>> i2c = busio.I2C(board.SCL, board.SDA)
57+
# >>> i2c.try_lock() # if False is returned: something else is using the i2c bus
58+
# >>> [hex(x) for x in i2c.scan()]
59+
# >>> i2c.unlock() # free up the bus for something else to use it
60+
61+
def detect_range(count=5):
62+
""" take count=5 samples """
63+
while count:
64+
for index, sensor in enumerate(vl53):
65+
print('Sensor {} Range: {}mm'.format(index + 1, sensor.range))
66+
time.sleep(1.0)
67+
count -= 1
68+
69+
print("""\
70+
multiple VL53L0X sensors' addresses are assigned properly\n\
71+
execute detect_range() to read each sensors range readings""")

0 commit comments

Comments
 (0)