Skip to content

Commit 2c8895a

Browse files
authored
Merge pull request #12 from 2bndy5/master
added ability to use multiple VL53L0X on the same I2C bus
2 parents e4b89e6 + 1a170ad commit 2c8895a

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

adafruit_vl53l0x.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,20 @@ 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 7-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 (using the default address of ``0x29``)
468+
on the same I2C bus are in their off state by pulling the "SHDN" pins LOW. When the
469+
"SHDN" pin is pulled HIGH again the default I2C address is ``0x29``.
470+
"""
471+
self._write_u8(_I2C_SLAVE_DEVICE_ADDRESS, new_address & 0x7f)
472+
self._device.device_address = new_address

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ 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+
Copy "../examples/vl53l0x_multiple_sensors.py" to your "CIRCUITPY" drive, then run the script with ``from vl53l0x_multiple_sensors import *``
14+
15+
.. literalinclude:: ../examples/vl53l0x_multiple_sensors.py
16+
:caption: examples/vl53l0x_multiple_sensors.py
17+
:linenos:

examples/vl53l0x_multiple_sensors.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
from digitalio import DigitalInOut
11+
from adafruit_vl53l0x import VL53L0X
12+
13+
# declare the singleton variable for the default I2C bus
14+
i2c = board.I2C()
15+
16+
# declare the digital output pins connected to the "SHDN" pin on each VL53L0X sensor
17+
xshut = [
18+
DigitalInOut(board.D7),
19+
DigitalInOut(board.D9),
20+
# add more VL53L0X sensors by defining their SHDN pins here
21+
]
22+
23+
for power_pin in xshut:
24+
# make sure these pins are a digital output, not a digital input
25+
power_pin.switch_to_output(value=False)
26+
# These pins are active when Low, meaning:
27+
# if the output signal is LOW, then the VL53L0X sensor is off.
28+
# if the output signal is HIGH, then the VL53L0X sensor is on.
29+
# all VL53L0X sensors are now off
30+
31+
# initialize a list to be used for the array of VL53L0X sensors
32+
vl53 = []
33+
34+
# now change the addresses of the VL53L0X sensors
35+
for i, power_pin in enumerate(xshut):
36+
# turn on the VL53L0X to allow hardware check
37+
power_pin.value = True
38+
# instantiate the VL53L0X sensor on the I2C bus & insert it into the "vl53" list
39+
vl53.insert(i, VL53L0X(i2c)) # also performs VL53L0X hardware check
40+
# no need to change the address of the last VL53L0X sensor
41+
if i < len(xshut) - 1:
42+
# default address is 0x29. Change that to something else
43+
vl53[i].set_address(i + 0x30) # address assigned should NOT be already in use
44+
# there is a helpful list of pre-designated I2C addresses for various I2C devices at
45+
# https://learn.adafruit.com/i2c-addresses/the-list
46+
# According to this list 0x30-0x34 are available, although the list may be incomplete.
47+
# In the python REPR, you can scan for all I2C devices that are attached and detirmine
48+
# their addresses using:
49+
# >>> import board
50+
# >>> i2c = board.I2C()
51+
# >>> if i2c.try_lock():
52+
# >>> [hex(x) for x in i2c.scan()]
53+
# >>> i2c.unlock()
54+
55+
def detect_range(count=5):
56+
""" take count=5 samples """
57+
while count:
58+
for index, sensor in enumerate(vl53):
59+
print('Sensor {} Range: {}mm'.format(index + 1, sensor.range))
60+
time.sleep(1.0)
61+
count -= 1
62+
63+
print("Multiple VL53L0X sensors' addresses are assigned properly\n"
64+
"execute detect_range() to read each sensors range readings")

0 commit comments

Comments
 (0)