Using two 3-Channel Wide FOV Time-of-Flight Distance Sensor Using OPT3101

I’m trying to connect two 3-channel wide fov sensor using OPT3101 IC, since it supports i2C I’m trying to change the address of one of the sensors so I can communicate with each one separately to achieve ~360 degrees of coverage. but when i change one of the adressess it gives me an error in the arduino terminal and im not able to communicate with one that its adress was changed is it possible to have twp OPT3101 ic’s with different adresses on the same i2c bus ? if yes i would be glad for some help on that
thank you

Hello,

Unfortunately, this board does not allow the I2C address of the OPT3101 to be changed. If using multiple boards on different I2C buses is not an option for you, you might look into trying an I2C multiplexer.

Kevin

@kevin In the arduino library provided for the opt3101 there is a function setAddress(), are you saying that this function isn’t actually capable of changing the address, and a multiplexer is the only option for using multiple of these on the same arduino board?

Thanks in advance.

Hi, MWonga.

Yes, the setAddress() function only changes the address the library uses to communicate with the sensor, and it currently exists only to provide flexibility for use with other boards that we might make in the future. (We realized the readme’s documentation for that function is kind of misleading, so we will be rewriting it a bit.)

There is no way to change the I2C address on our current 3-channel OPT3101 board, so yes, you would need a multiplexer or separate I2C buses to use multiple sensor boards with a single Arduino controller.

Kevin

Hey @kevin! I have a follow up to this question. I am using two 3-Channel Wide FOV TOF Sensors with a TCA9548A 1-to-8 I2C Multiplexer. Everything is working great. However, I am having one issue.

I am working with an ARDUINO UNO WiFi REV2, and after reading both sensors at roughly 30 second to 1 minute intervals, the wifi connection that I have setup gets disconnected. I understand this is a very specific question but I was wondering if you have any ideas as to why this might be happening. I would be happy to provide code it this is helpful.

Any tips would be greatly appreciated.

Hi, skl.

Unfortunately, I don’t have any particular insight into why you might be having that issue (and we don’t really have experience working with the Uno WiFi). I would suggest troubleshooting it by making small changes to try to narrow down the cause of the problem; for example, does anything change if you only read one sensor, or if you read the sensors less often or more often? You might also try checking for power or memory issues.

Kevin

@kevin Thanks for the reply. I am having the issue issue even if I power the sensors externally and if I print the memory, it is stable. I was hopeful that one of those would be the issue but doesnt seem to be. The interval at which connection is lost is super random (30 sec to 5 min) so I think that also speaks against memory.

If I read the sensor more often, the connection is lost way faster.

Here is actually even a simple code in which the error is happening.

#include <OPT3101.h>
#include <Wire.h>
#include <WiFiNINA.h>




const uint8_t dataReadyPin = 2;
OPT3101 sensor;

int16_t distances[3];
volatile bool dataReady = false;

//----------------Multiplexer
#define TCAADDR 0x70

#define SECRET_SSID "XXXXXXXXX"
#define SECRET_PASS "XXXXXXXXX"

WiFiClient wifiClient;
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS; 




void setDataReadyFlag()
{
  dataReady = true;
}

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

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

void setup()
{
  Wire.begin();

  // Wait for the serial port to be opened before printing
  // messages (only applies to boards with native USB).
  Serial.begin(1000000);

  tcaselect(2);
  sensor.init();
  if (sensor.getLastError())
  {
    Serial.print(F("Failed to initialize OPT3101: error "));
    Serial.println(sensor.getLastError());
    while (1) {}
  }
  sensor.setContinuousMode();
  sensor.enableDataReadyOutput(1);
  sensor.setFrameTiming(256);
  sensor.setChannel(OPT3101ChannelAutoSwitch);
  sensor.setBrightness(OPT3101Brightness::Adaptive);

  attachInterrupt(digitalPinToInterrupt(dataReadyPin), setDataReadyFlag, RISING);
  sensor.enableTimingGenerator();

  setup_wifi();
}

void loop()
{

  int StatusWiFi=WiFi.status();
  if(StatusWiFi!=3)
  {
    Serial.println(WiFi.status());
    WiFi.end();
    Serial.println("Wifi not connected!");
    setup_wifi();
  }

  if (dataReady)
  {
    sensor.readOutputRegs();

    distances[sensor.channelUsed] = sensor.distanceMillimeters;

    if (sensor.channelUsed == 2)
    {
      for (uint8_t i = 0; i < 3; i++)
      {
        Serial.print(distances[i]);
        Serial.print(", ");
      }
      Serial.println();
    }
    dataReady = false;
  }
}

void setup_wifi() {

  WiFiDrv::pinMode(25, OUTPUT);
  WiFiDrv::pinMode(26, OUTPUT);

  // attempt to connect to Wifi network:
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(50);
  }

  WiFi.lowPowerMode();

  // Turn on LED that Wifi is connected (RED)
  WiFiDrv::analogWrite(25, 0);
  WiFiDrv::analogWrite(26, 255);

}

I looked over your code but didn’t see any obvious issues. Unfortunately, as I mentioned, we haven’t worked with the Uno WiFi Rev2 and don’t have one to test with, so I don’t think we can offer much specific advice about your issue since it seems specific to the wireless functionality on that board. If you are able to narrow down the problem more or find a way to demonstrate a similar problem that doesn’t involve Wi-Fi, we might be able to help you look into it further.

Kevin

Yes, I totally understand. I have narrowed it down to one line of code in the OPT3101 library that seems to be causing the disconnection from WiFi.

uint32_t reg08 = readReg(0x08);

I am able to read amplitude using code similar to the one posted but some reason reading this register to eventually get distance is causing the WiFi drop.