|
| 1 | +#!/usr/bin/env python3 |
| 2 | +#----------------------------------------------------------------------------- |
| 3 | +# qwiic_tca9548a_ex2.py |
| 4 | +# |
| 5 | +# Example for the Qwiic 8 Channel Mux interfacing with two Qwiic VL53L1X Distance Sensors |
| 6 | +#------------------------------------------------------------------------ |
| 7 | +# |
| 8 | +# Written by SparkFun Electronics, November 2024 |
| 9 | +# |
| 10 | +# This python library supports the SparkFun Electroncis qwiic |
| 11 | +# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single |
| 12 | +# board computers. |
| 13 | +# |
| 14 | +# More information on qwiic is at https://www.sparkfun.com/qwiic |
| 15 | +# |
| 16 | +# Do you like this library? Help support SparkFun. Buy a board! |
| 17 | +# |
| 18 | +#================================================================================== |
| 19 | +# Copyright (c) 2024 SparkFun Electronics |
| 20 | +# |
| 21 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | +# of this software and associated documentation files (the "Software"), to deal |
| 23 | +# in the Software without restriction, including without limitation the rights |
| 24 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | +# copies of the Software, and to permit persons to whom the Software is |
| 26 | +# furnished to do so, subject to the following conditions: |
| 27 | +# |
| 28 | +# The above copyright notice and this permission notice shall be included in all |
| 29 | +# copies or substantial portions of the Software. |
| 30 | +# |
| 31 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 37 | +# SOFTWARE. |
| 38 | +#================================================================================== |
| 39 | +# Description: |
| 40 | +# This example shows how to hook up two VL53L1X laser distance sensors with the same address. |
| 41 | +# You can read the VL53L1X hookup guide and get the library from https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide |
| 42 | +#================================================================================== |
| 43 | +# Hardware Connections: |
| 44 | +# Attach the Qwiic Mux Board to your control board with a Qwiic cable to the "Main" port on the Mux Board |
| 45 | +# Plug a VL53L1X device into port 0 and another into port 1 |
| 46 | +#================================================================================== |
| 47 | + |
| 48 | +import qwiic_tca9548a |
| 49 | +import qwiic_vl53l1x # Get the library here: https://github.com/sparkfun/Qwiic_VL53L1X_Py/tree/master or with mip |
| 50 | +import qwiic_i2c |
| 51 | +import time |
| 52 | +import sys |
| 53 | + |
| 54 | +NUMBER_OF_SENSORS = 2 # Change this number if you have more than 2 VL53L1X sensors. Connect all sensors to lowest available ports on MUX |
| 55 | + |
| 56 | +def runExample(): |
| 57 | + print("\nSparkFun TCA9548A 8-Channel Mux Example 2\n") |
| 58 | + |
| 59 | + # Create an instance of our MUX object |
| 60 | + myTca = qwiic_tca9548a.QwiicTCA9548A() |
| 61 | + |
| 62 | + if myTca.connected == False: |
| 63 | + print("The Qwiic TCA9548A 8-Channel Mux device isn't connected to the system. Please check your connection", \ |
| 64 | + file=sys.stderr) |
| 65 | + return |
| 66 | + |
| 67 | + # Create a list of distance sensor objects |
| 68 | + distance_sensors = [None] * NUMBER_OF_SENSORS |
| 69 | + |
| 70 | + for i in range(NUMBER_OF_SENSORS): |
| 71 | + distance_sensors[i] = qwiic_vl53l1x.QwiicVL53L1X() |
| 72 | + |
| 73 | + # We will use this object to see what devices are connected to the master after configuring the MUX |
| 74 | + i2c = qwiic_i2c.getI2CDriver() |
| 75 | + |
| 76 | + print("Disabling all MUX Ports") |
| 77 | + myTca.disable_all() |
| 78 | + myTca.list_channels() |
| 79 | + |
| 80 | + # Now configure each sensor one at a time |
| 81 | + for i in range(NUMBER_OF_SENSORS): |
| 82 | + print("Enabling sensor on channel", i) |
| 83 | + myTca.disable_all() |
| 84 | + myTca.enable_channels([i]) |
| 85 | + |
| 86 | + # If a device is connected, initialize it |
| 87 | + if distance_sensors[i].sensor_init() == None: |
| 88 | + print("Sensor", i, " connected!") |
| 89 | + else: |
| 90 | + print("Sensor", i, " not connected! Exiting Program. Please connect sensor", i, "and restart program.") |
| 91 | + sys.exit(0) |
| 92 | + |
| 93 | + while True: |
| 94 | + # Loop through each sensor and enable its MUX channel then read the distance |
| 95 | + for i in range(NUMBER_OF_SENSORS): |
| 96 | + # Enable the MUX channel for the sensor we want to read |
| 97 | + myTca.disable_all() |
| 98 | + myTca.enable_channels([i]) |
| 99 | + |
| 100 | + # Read distance from the sensor |
| 101 | + distance_sensors[i].start_ranging() |
| 102 | + time.sleep(0.005) |
| 103 | + distance = distance_sensors[i].get_distance() |
| 104 | + time.sleep(0.005) |
| 105 | + distance_sensors[i].stop_ranging() |
| 106 | + |
| 107 | + distanceInches = distance / 25.4 |
| 108 | + distanceFeet = distanceInches / 12.0 |
| 109 | + |
| 110 | + print("Distance", i, "(in): ", distanceInches, " Distance", i, "(ft): ", distanceFeet) |
| 111 | + |
| 112 | + time.sleep(0.5) |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + try: |
| 116 | + runExample() |
| 117 | + except (KeyboardInterrupt, SystemExit) as exErr: |
| 118 | + print("\nEnding Example 2") |
| 119 | + sys.exit(0) |
0 commit comments