I’m having trouble building a system with 32 sensors / 4 mux / Arduino Uno. The setup is an Arduino Uno with 4 Qwiic Mux daisy chained. Each Mux has 8 sensors (Adafruit APDS9960) attached. The 4 Qwiic Mux have their pads soldered correctly and are addressed as 70, 71, 72, 73. I’m actually porting this from CircuitPython / Raspberry Pi to Arduino / Uno. All of the hardware has been tested and works very well with CircuitPython / Raspberry Pi.
When I run the Basic Control sketch I see the 4 Mux no problem, as well as the 0x39 APDS9960. I can also run a single APDS9960 no problem using the Adafruit APDS9960 Arduino library. I’m trying to modify the DualDistance example code to use multiple APDS9960, and that is where I am getting stuck. I’m not a strong Arduino programmer, and I’m stuck on concepts like pointers:
SFEVL53L1X **distanceSensor; //Create pointer to a set of pointers to the sensor class
The DualDistance sample code is (I’m sure) well commented for a seasoned Arduino coder, but for me it’s too confusing. I’d like to start by possibly seeing an example of how to read 8 generic I2C devices via a single Mux - then progress from there. I’m looking for any help with example code that might point me in the right direction. Any help would be greatly appreciated.
To get more specific- I’m posting code from my attempt to modify the DualDistance example - with a single Qwiic Mux and 8 APDS9960 sensors:
#include <Wire.h>
#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
QWIICMUX myMux;
#define NUMBER_OF_SENSORS 8
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
#include "Adafruit_APDS9960.h"
Adafruit_APDS9960 **distanceSensor; //Create pointer to a set of pointers to the sensor class
void setup()
{
Serial.begin(115200);
Serial.println("Qwiic Mux Shield Read Example");
Wire.begin();
//Create set of pointers to the class
distanceSensor = new Adafruit_APDS9960 *[NUMBER_OF_SENSORS];
//Assign pointers to instances of the class
for (int x = 0; x < NUMBER_OF_SENSORS; x++)
distanceSensor[x] = new Adafruit_APDS9960(); //ERROR here with Adafruit_APDS9960(Wire)-- was distanceSensor[x] = new SFEVL53L1X(Wire);
if (myMux.begin() == false)
{
Serial.println("Mux not detected. Freezing...");
while (1)
;
}
Serial.println("Mux detected");
byte currentPortNumber = myMux.getPort();
Serial.print("CurrentPort: ");
Serial.println(currentPortNumber);
//Initialize all the sensors
bool initSuccess = true;
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
{
myMux.setPort(x);
if (distanceSensor[x]->begin() != 0) //Begin returns 0 on a good init
{
Serial.print("Sensor ");
Serial.print(x);
Serial.println(" did not begin! Check wiring");
initSuccess = false;
}
else
{
//Configure each sensor
// distanceSensor[x]->setIntermeasurementPeriod(180);
// distanceSensor[x]->setDistanceModeLong();
// distanceSensor[x]->startRanging(); //Write configuration bytes to initiate measurement
//enable proximity mode
distanceSensor[x]->enableProximity(true);
//set the interrupt threshold to fire when proximity reading goes above 255
distanceSensor[x]->setProximityInterruptThreshold(0, 255);
Serial.print("Sensor ");
Serial.print(x);
Serial.println(" configured");
}
}
if (initSuccess == false)
{
Serial.print("Freezing...");
while (1)
;
}
Serial.println("Mux Shield online");
}
void loop()
{
int distance[NUMBER_OF_SENSORS];
float distanceFeet;
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
{
myMux.setPort(x); //Tell mux to connect to this port, and this port only
distance[x] = distanceSensor[x]->readProximity(); //Get the result of the measurement from the sensor
Serial.print("\tDistance");
Serial.print(x);
Serial.print("(mm): ");
Serial.print(distance[x]);
}
Serial.println();
delay(180); //Wait for next reading
}
I’m getting an error when I try to compile with ```
distanceSensor = new Adafruit_APDS9960(Wire);
Qwiic Mux ShiQwiic Mux Shield Read Example
Mux detected
CurrentPort: 255
Sensor 0 did not begin! Check wiring
Sensor 1 did not begin! Check wiring
Sensor 2 did not begin! Check wiring
Sensor 3 did not begin! Check wiring
Sensor 4 did not begin! Check wiring
Sensor 5 did not begin! Check wiring
Sensor 6 did not begin! Check wiring
Sensor 7 did not begin! Check wiring
Freezing…
As you can see the Mux seems to be read properly, but none of the sensors are...
While we aren’t coding experts either, after a bit of poking around I think looking through this page https://translate.google.com/translate? … ch&pto=aue might illustrate things a bit clearer…I believe what is needed is to alter your code to be more similar to how the “myMux.setPort” are used here?
This seems like it should work- but it doesn’t. I get no readings for the sensor on the first port (0)… Does anything look wrong to you or jump out? Maybe it’s something simple…
Just a guess - apds_00.begin() talks to the sensor, so the mux needs to be set up before. Try adding myMux_70.setPort(0); in setup before calling apds_00.begin();
One thing that I have learned in playing with the qwiic mux and various sensors is to run an I2C scanner program. This will let you know if devices are seen on the bus. From there you can debug their response.
I used the code listed just above as a baseline for my project using a SparkDUN MUX and when it compiles in Arcuino1.8.15 Window store (1.8.49) i receive a 2 wire warning.
I read on another Blog that if I were using a Mega instead of a plain UNO I would not receive this error. If that is true, is there any way to fool the compiler in some way. Is this warning going to be a serious problem in a future compiler?
Currently, the program still runs.
I just discovered that there is a library for it from
I normally see a link under the SparkFun product to a corresponding library/ I double checked but did not find it there. Maybe for future a reference for MUX Library would be helpful.