1

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
Rohit Gupta
6162 gold badges5 silver badges18 bronze badges
asked Jun 2, 2023 at 20:41
3
  • 1
    you have to supply the module I2C address when you instantiate the SparkFun_TMF882X object Commented Jun 3, 2023 at 2:37
  • I'd have started with the assumption that you have to create 8 objects of type SparkFun_TMF882X as SparkFun_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 say enableMuxPort(2); to see if it works for just sensor 2. Commented Jun 4, 2023 at 3:22
  • Where is the definition of enableMux() and disableMux()? Commented Jun 12, 2023 at 4:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.