TroubleshootingArduino

Connecting Multiple SEN0430

userHead d_schech 2024-03-21 07:40:11 186 Views1 Replies

Hello, I am trying to connect multiple SEN0430 distance sensors to one arduino using a TCA9548 Multiplexer from Adafruit. This is import for the code later. I can get both sensors to initialize but it seems which ever sensor is currently selected in the multiplexer is the only one that will execute tof.startMeasurement. I have tried a few separate ways of coding this in and nothing seems to work only the first sensor to execute that function works. Does anyone know any way to work around this?

 

/* 
 Sketch generated by the Arduino IoT Cloud Thing "Untitled"
 https://create.arduino.cc/cloud/things/f17507e6-253c-4021-aea4-d7092f608a9d

 Arduino IoT Cloud Variables description

 The following variables are automatically generated and updated when changes are made to the Thing

 float distance0;
 float distance1;

 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
 which are called when their values are changed from the Dashboard.
 These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include "DFRobot_TMF8x01.h"
#include "Wire.h"

#define EN       -1                      //EN pin of of TMF8x01 module is floating, not used in this demo
#define INT      -1                      //INT pin of of TMF8x01 module is floating, not used in this demo

DFRobot_TMF8801 tof(/*enPin =*/EN,/*intPin=*/INT);
//DFRobot_TMF8701 tof(/*enPin =*/EN,/*intPin=*/INT);

uint8_t caliDataBuf[14] = {0x41,0x57,0x01,0xFD,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04};//The 14 bytes calibration data which you can get by calibration.ino demo.

#define TCAADDR 0x70

void tcaselect(uint8_t i) {
 if (i > 7) return;

 Wire.beginTransmission(TCAADDR);
 Wire.write(1 << i);
 Wire.endTransmission();  
}

 

void setup() {
 // Initialize serial and wait for port to open:
 Serial.begin(115200);
 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
 delay(1500);

 // Defined in thingProperties.h
 initProperties();

 // Connect to Arduino IoT Cloud
 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
 
 /*
    The following function allows you to obtain more information
    related to the state of network and IoT Cloud connection and errors
    the higher number the more granular information you’ll get.
    The default is 0 (only errors).
    Maximum is 4
*/
 setDebugMessageLevel(2);
 ArduinoCloud.printDebugInfo();
 
 {
   while (!Serial);
   delay(1000);

   Wire.begin();
   
   Serial.begin(115200);
   Serial.println("\nTCAScanner ready!");
   
   for (uint8_t t=0; t<8; t++) {
     tcaselect(t);
     Serial.print("TCA Port #"); Serial.println(t);

     for (uint8_t addr = 0; addr<=127; addr++) {
       if (addr == TCAADDR) continue;

       Wire.beginTransmission(addr);
       if (!Wire.endTransmission()) {
         Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
       }
     }
   }
   Serial.println("\ndone");
}
 
 while(!Serial){                                                              //Wait for serial port to connect. Needed for native USB port only
 }

 Serial.print("Initialization ranging sensor TMF8x01......");
 
     tcaselect(1);
     while(tof.begin() != 0){                                                 //Initialization sensor,sucess return 0, fail return -1
     Serial.println("failed1.");
     delay(1000);
     }
     
     Serial.println("Sensor 1 Initialized");
     
     tcaselect(2);
     while(tof.begin() != 0){                                                 //Initialization sensor,sucess return 0, fail return -1
     Serial.println("failed2.");
     delay(1000);
     }
 
 Serial.println("done.");

 Serial.print("Software Version: ");
 Serial.println(tof.getSoftwareVersion());
 Serial.print("Unique ID: ");
 Serial.println(tof.getUniqueID(),HEX);
 Serial.print("Model: ");
 Serial.println(tof.getSensorModel());

     for (uint8_t t=0; t<8; t++) {
     tcaselect(t);
 tof.setCalibrationData(caliDataBuf, sizeof(caliDataBuf));                //Set calibration data.

/**
* @brief Config measurement params to enable measurement. Need to call stopMeasurement to stop ranging action.
* @param cailbMode: Is an enumerated variable of eCalibModeConfig_t, which is to config measurement cailibration mode.
* @n     eModeNoCalib  :          Measuring without any calibration data.
* @n     eModeCalib    :          Measuring with calibration data.
* @n     eModeCalibAndAlgoState : Measuring with calibration and algorithm state.
* @param disMode : the ranging mode of TMF8701 sensor.(this mode only TMF8701 support)
* @n     ePROXIMITY: Raing in PROXIMITY mode,ranging range 0~10cm
* @n     eDISTANCE: Raing in distance mode,ranging range 10~60cm
* @n     eCOMBINE:  Raing in PROXIMITY and DISTANCE hybrid mode,ranging range 0~60cm
*/

 
     tof.startMeasurement(/*cailbMode =*/tof.eModeCalib);                 //Enable measuring with Calibration data.
 //tof.startMeasurement(/*cailbMode =*/tof.eModeCalib, /*disMode =*/tof.ePROXIMITY);      //only support TMF8701
 }

}

 

void loop() {
 ArduinoCloud.update();
 // Your code here 
 
 tcaselect(1);
 delay(10);
 if (tof.isDataReady()) {                                                 //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
     distance0 = tof.getDistance_mm();
     Serial.print("Distance_0 = ");
     Serial.print(distance0);                                  //Print measurement data to USB Serial COM, unit mm, in eCOMBINE mode.
     Serial.println(" mm");
 }
 
   tcaselect(2);
   delay(10);
 if (tof.isDataReady()) {                                                 //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
     distance1 = tof.getDistance_mm();
     Serial.print("Distance_1 = ");
     Serial.print(distance1);                                  //Print measurement data to USB Serial COM, unit mm, in eCOMBINE mode.
     Serial.println(" mm");
 }
 
}

 

 

/*
 Since Distance is READ_WRITE variable, onDistanceChange() is
 executed every time a new value is received from IoT Cloud.
*/
void onDistanceChange()  {
 // Add your code here to act upon Distance change
}

/*
 Since Distance0 is READ_WRITE variable, onDistance0Change() is
 executed every time a new value is received from IoT Cloud.
*/
void onDistance0Change()  {
 // Add your code here to act upon Distance0 change
}

/*
 Since Distance1 is READ_WRITE variable, onDistance1Change() is
 executed every time a new value is received from IoT Cloud.
*/
void onDistance1Change()  {
 // Add your code here to act upon Distance1 change

2024-03-21 23:01:17

For anyone else having this issue. I found you need to stop the measuring of one sensor using tof.stopMeasurement and then go to the next sensor. However once you stop measuring you need to start the sensor again using tof.begin. Therefore I had to move all the sensor code into the void loop. This is shown in this code:

 

      tcaselect(1);
     while(tof.begin() != 0){                                                 //Initialization sensor,sucess return 0, fail return -1
     Serial.println("failed1.");
     delay(1000);
     }
     
     tcaselect(2);
     while(tof.begin() != 0){                                                 //Initialization sensor,sucess return 0, fail return -1
     Serial.println("failed2.");
     delay(1000);
     }
 
 tcaselect(1);
 tof.setCalibrationData(caliDataBuf, sizeof(caliDataBuf));
 tof.startMeasurement(tof.eModeCalib);
 delay(1000);
 if (tof.isDataReady()) {                                                 //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
     distance0 = tof.getDistance_mm();
     Serial.print("Distance_0 = ");
     Serial.print(distance0);                                  //Print measurement data to USB Serial COM, unit mm, in eCOMBINE mode.
     Serial.println(" mm");
 }
 tof.stopMeasurement();
   
 tcaselect(2);
 tof.setCalibrationData(caliDataBuf, sizeof(caliDataBuf));
 tof.startMeasurement(tof.eModeCalib);
 delay(1000);
 if (tof.isDataReady()) {                                                 //Is check measuring data vaild, if vaild that print measurement data to USB Serial COM.
     distance1 = tof.getDistance_mm();
     Serial.print("Distance_1 = ");
     Serial.print(distance1);                                  //Print measurement data to USB Serial COM, unit mm, in eCOMBINE mode.
     Serial.println(" mm");
 }
 tof.stopMeasurement();
 
}

userHeadPic d_schech