I needed to used 2 ToF sensors (VL53L4CX) connected with I2C but could not find sufficient examples to add it to my project.
The example for the VL53L4CD and PCA9546 on Adafruit's guide for addressing two sensors with the same I2C address will not work for these sensors, because they use different libraries (respectively, vl53l4cd_class.h and vl53l4cx_class.h).
I created this code using an interpretive coding method I learned in UWF's Digital Art program called "mashup", in which I may not need to know a coding language to successfully analyze and break apart fragments of code that I can assemble back together to achieve desired results. This is the method I used for this guide and the rest of my projects.
/*Danielle Biohax (Danielle Muir) * Bachelor of Fine Arts in Digital Art (pending Spring 2023) * minors: Anthropology, Kugelman Honors Program * University of West Florida * * * * BASED ON : * "SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries * SPDX-License-Identifier: MIT * PCA9546 I2C Multi Sensor Example * Using two VL53L4CD sensors on ports 0 and 1" * https://learn.adafruit.com/adafruit-pca9546-4-channel-stemma-qt-multiplexer/arduino * * AND * * STMicroelectronics Example code * "VL53L4CX_Sat_HelloWorld.ino" * https://learn.adafruit.com/adafruit-vl53l4cx-time-of-flight-distance-sensor/arduino * (Copyright Statement Below) */ /******************************************************************************** * @file VL53L4CX_Sat_HelloWorld.ino * @author SRA * @version V1.0.0 * @date 16 March 2022 * @brief Arduino test application for the STMicrolectronics VL53L4CX * proximity sensor satellite based on FlightSense. * This application makes use of C++ classes obtained from the C * components' drivers. ****************************************************************************** * @attention * * COPYRIGHT(c) 2022 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include <Arduino.h> #include <Wire.h> #include <vl53l4cx_class.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <assert.h> #include <stdlib.h> #define PCAADDR 0x70 #define DEV_I2C Wire #define SerialPort Serial #ifndef LED_BUILTIN #define LED_BUILTIN 13 #endif #define LedPin LED_BUILTIN // Components. VL53L4CX sensor_vl53l4cx_sat1(&DEV_I2C, A1); VL53L4CX sensor_vl53l4cx_sat2(&DEV_I2C, A1); void pcaselect(uint8_t i) { if (i > 3) return; Wire.beginTransmission(PCAADDR); Wire.write(1 << i); Wire.endTransmission(); } /* Setup ---------------------------------------------------------------------*/ void setup() { Wire.begin(); // Initialize serial for output. SerialPort.begin(115200); SerialPort.println("Starting Multisensor..."); // Initialize I2C bus. DEV_I2C.begin(); // define the port on the PCA9546 for the first sensor pcaselect(0); // setup the first sensor // Configure VL53L4CX satellite component. sensor_vl53l4cx_sat1.begin(); // Switch off VL53L4CX satellite component. sensor_vl53l4cx_sat1.VL53L4CX_Off(); //Initialize VL53L4CX satellite component. sensor_vl53l4cx_sat1.InitSensor(0x12); // Start Measurements sensor_vl53l4cx_sat1.VL53L4CX_StartMeasurement(); // define the port on the PCA9546 for the 2nd sensor pcaselect(1); // setup the 2nd sensor // Configure VL53L4CX satellite component. sensor_vl53l4cx_sat2.begin(); // Switch off VL53L4CX satellite component. sensor_vl53l4cx_sat2.VL53L4CX_Off(); //Initialize VL53L4CX satellite component. sensor_vl53l4cx_sat2.InitSensor(0x12); // Start Measurements sensor_vl53l4cx_sat2.VL53L4CX_StartMeasurement(); } void loop() { VL53L4CX_MultiRangingData_t MultiRangingData; VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData; uint8_t NewDataReady = 0; int no_of_object_found = 0, j; char report[64]; int status; // define port on the PCA9546 pcaselect(0); do { status = sensor_vl53l4cx_sat1.VL53L4CX_GetMeasurementDataReady(&NewDataReady); } while (!NewDataReady); //Led on digitalWrite(LedPin, HIGH); if ((!status) && (NewDataReady != 0)) { status = sensor_vl53l4cx_sat1.VL53L4CX_GetMultiRangingData(pMultiRangingData); no_of_object_found = pMultiRangingData->NumberOfObjectsFound; snprintf(report, sizeof(report), "ToF1: Count=%d, #Objs=%1d ", pMultiRangingData->StreamCount, no_of_object_found); SerialPort.print(report); for (j = 0; j < no_of_object_found; j++) { if (j != 0) { SerialPort.print("\r\n "); } SerialPort.print("status="); SerialPort.print(pMultiRangingData->RangeData[j].RangeStatus); SerialPort.print(", D="); SerialPort.print(pMultiRangingData->RangeData[j].RangeMilliMeter); SerialPort.print("mm"); SerialPort.print(", Signal="); SerialPort.print((float)pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65536.0); SerialPort.print(" Mcps, Ambient="); SerialPort.print((float)pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65536.0); SerialPort.print(" Mcps"); } SerialPort.println(""); if (status == 0) { status = sensor_vl53l4cx_sat1.VL53L4CX_ClearInterruptAndStartMeasurement(); } } digitalWrite(LedPin, LOW); pcaselect(1); do { status = sensor_vl53l4cx_sat2.VL53L4CX_GetMeasurementDataReady(&NewDataReady); } while (!NewDataReady); //Led on digitalWrite(LedPin, HIGH); if ((!status) && (NewDataReady != 0)) { status = sensor_vl53l4cx_sat2.VL53L4CX_GetMultiRangingData(pMultiRangingData); no_of_object_found = pMultiRangingData->NumberOfObjectsFound; snprintf(report, sizeof(report), "ToF2: Count=%d, #Objs=%1d ", pMultiRangingData->StreamCount, no_of_object_found); SerialPort.print(report); for (j = 0; j < no_of_object_found; j++) { if (j != 0) { SerialPort.print("\r\n "); } SerialPort.print("status="); SerialPort.print(pMultiRangingData->RangeData[j].RangeStatus); SerialPort.print(", D="); SerialPort.print(pMultiRangingData->RangeData[j].RangeMilliMeter); SerialPort.print("mm"); SerialPort.print(", Signal="); SerialPort.print((float)pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65536.0); SerialPort.print(" Mcps, Ambient="); SerialPort.print((float)pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65536.0); SerialPort.print(" Mcps"); } SerialPort.println(""); if (status == 0) { status = sensor_vl53l4cx_sat2.VL53L4CX_ClearInterruptAndStartMeasurement(); } } digitalWrite(LedPin, LOW); }