-
Notifications
You must be signed in to change notification settings - Fork 13
Added set_address function to allow use of multiple devices on one bus #3
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
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a3f0551
Added a function to update the i2c address so multiple sensor can be…
wrdaigle f70ab27
Added an example of how to use multiple sensors on the same bus
wrdaigle c83ff77
Updated copywrite
wrdaigle a37ffc8
After running pre-commit
wrdaigle f7f2c58
Simplify docstring for sphinx
wrdaigle 6fc9415
Reused i2c from init
wrdaigle 7f764c5
Updated example without passing i2c
wrdaigle ef5228a
Added _VL53L1X_I2C_SLAVE_DEVICE_ADDRESS const and used struct.pack to…
wrdaigle eff8538
Date mod
wrdaigle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# SPDX-FileCopyrightText: 2021 wrdaigle for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
Example of how to use the adafruit_vl53l1x library to change the assigned address of | ||
multiple VL53L1X sensors on the same I2C bus. This example only focuses on 2 VL53L1X | ||
sensors, but can be modified for more. BE AWARE: a multitude of sensors may require | ||
more current than the on-board 3V regulator can output (typical current consumption during | ||
active range readings is about 19 mA per sensor). | ||
""" | ||
import time | ||
import board | ||
from digitalio import DigitalInOut | ||
from adafruit_vl53l1x import VL53L1X | ||
|
||
# declare the singleton variable for the default I2C bus | ||
i2c = board.I2C() | ||
|
||
# declare the digital output pins connected to the "SHDN" pin on each VL53L1X sensor | ||
xshut = [ | ||
DigitalInOut(board.D16), | ||
DigitalInOut(board.D17), | ||
# add more VL53L1X sensors by defining their SHDN pins here | ||
] | ||
|
||
for power_pin in xshut: | ||
# make sure these pins are a digital output, not a digital input | ||
power_pin.switch_to_output(value=False) | ||
# These pins are active when Low, meaning: | ||
# if the output signal is LOW, then the VL53L1X sensor is off. | ||
# if the output signal is HIGH, then the VL53L1X sensor is on. | ||
# all VL53L1X sensors are now off | ||
|
||
# initialize a list to be used for the array of VL53L1X sensors | ||
vl53 = [] | ||
|
||
# now change the addresses of the VL53L1X sensors | ||
for i, power_pin in enumerate(xshut): | ||
# turn on the VL53L1X to allow hardware check | ||
power_pin.value = True | ||
# instantiate the VL53L1X sensor on the I2C bus & insert it into the "vl53" list | ||
vl53.insert(i, VL53L1X(i2c)) # also performs VL53L1X hardware check | ||
# no need to change the address of the last VL53L1X sensor | ||
if i < len(xshut) - 1: | ||
# default address is 0x29. Change that to something else | ||
vl53[i].set_address( | ||
i2c, i + 0x30 | ||
) # address assigned should NOT be already in use | ||
# there is a helpful list of pre-designated I2C addresses for various I2C devices at | ||
# https://learn.adafruit.com/i2c-addresses/the-list | ||
# According to this list 0x30-0x34 are available, although the list may be incomplete. | ||
# In the python REPR, you can scan for all I2C devices that are attached and detirmine | ||
# their addresses using: | ||
# >>> import board | ||
# >>> i2c = board.I2C() | ||
# >>> if i2c.try_lock(): | ||
# >>> [hex(x) for x in i2c.scan()] | ||
# >>> i2c.unlock() | ||
|
||
|
||
def detect_range(count=5): | ||
""" take count=5 samples """ | ||
while count: | ||
for index, sensor in enumerate(vl53): | ||
print("Sensor {} Range: {}mm".format(index + 1, sensor.distance)) | ||
time.sleep(1.0) | ||
count -= 1 | ||
|
||
|
||
detect_range() | ||
print( | ||
"Multiple VL53L1X sensors' addresses are assigned properly\n" | ||
"execute detect_range() to read each sensors range readings" | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.