I am trying to build a project where I have connected 8 Sparkfun TOF Sensors to an I2C MUX which is connected to a Sparkfun RedBoard.
I need to read and display the data from each sensor on the serial monitor right now and later on an OLED Display. The issue I am facing is that it is reading data only from the first sensor and then it stops running.
I have pasted my code below. I am new to all of this so any help would be appreciated on how to fix this issue. The enableMux()
and disableMux()
functions are from the GitHub example code libraries. I want to know if the issue is that I am not enabling my ports properly or if the issue is with my data reading. Do I need to initialize each sensor individually?
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SparkFun_TMF882X_Library.h"
#define sensors 8
SparkFun_TMF882X myTMF882X;
void setup()
{
Serial.begin(9600);
while(!Serial);
Wire.begin();
for(byte x=0;x<sensors;x++)
{
enableMuxPort(x);
if(!myTMF882X.begin())
{
Serial.println("Error - The TMF882X failed to initialize - is the board connected?");
while(1);
}
else
Serial.print(x+1);
Serial.println(" TMF882X started.");
disableMuxPort(x);
}
}
void loop()
{
delay(500);
for(byte x=0;x<sensors;x++)
{
enableMuxPort(x);
struct tmf882x_msg_meas_results myResults;
myTMF882X.startMeasuring(myResults);
Serial.print(myResults.num_results);
unsigned int distance = myResults.results[0].distance_mm;
Serial.print("Sensor ");
Serial.print(x+1);
Serial.print(" distance: ");
Serial.print(distance);
Serial.print("mm");
Serial.println();
disableMuxPort(x);
}
}
Hardware Used:
Microcontroller: Sparkfun Redboard Turbo
MUX: SparkFun Qwiic Mux Breakout - 8 Channel (TCA9548A) - BOB-16784 - SparkFun Electronics
Sensor: Qwiic dToF Imager (TMF882X) Hookup Guide - SparkFun Learn
Below is the output I am getting:
1 TMF882X started.
2 TMF882X started.
3 TMF882X started.
4 TMF882X started.
5 TMF882X started.
6 TMF882X started.
7 TMF882X started.
8 TMF882X started.
9Sensor 1 distance: 3325mm
0Sensor 2 distance: 0mm
SparkFun_TMF882X
objectSparkFun_TMF882X myTMF882X[ 8 ];
instead of using a shared object. Alternatively, call the begin() method each time you switch to a different sensor. You could also test the multiplexing by hardcoding sayenableMuxPort(2);
to see if it works for just sensor 2.enableMux(
) anddisableMux()
?