adafruit / Adafruit_AS7341

Other
29 stars 18 forks source link

Two Sensors on Different I2C Buses #21

Closed libbierose86 closed 1 year ago

libbierose86 commented 1 year ago

Hi I was wondering it possible to us this library to read from two different sensors on to different i2c buses, I'm using a pico and am able to scan both the buses and see that the sensor are there, I just can not figure out if I'm able to define which I2C is is used at the begin().

#include <Adafruit_AS7341.h>
#include <Wire.h>

#define I2C0_SDA_PIN 4
#define I2C0_SCL_PIN 5

#define I2C1_SDA_PIN 26
#define I2C1_SCL_PIN 3

Adafruit_AS7341 sensor_one;
Adafruit_AS7341 sensor_two;

void setup() {
  Serial.begin(115200);

  //Set pins for I2C0
  Wire.setSCL(I2C0_SCL_PIN);
  Wire.setSDA(I2C0_SDA_PIN);

  //Set pins for I2C1
  Wire1.setSCL(I2C1_SDA_PIN);
  Wire1.setSDA(I2C1_SCL_PIN);

  // Wait for communication with the host computer serial monitor
  while (!Serial) {
    delay(1);
  }

  if (!sensor_one.begin()){
    Serial.println("Could not find AS7341");
    while (1) { delay(10); }
  }

  if (!sensor_two.begin()){
    Serial.println("Could not find AS7341");
    while (1) { delay(10); }
  }
caternuson commented 1 year ago

Pass the non-default bus in as a parameter to begin(). Default is Wire. https://adafruit.github.io/Adafruit_AS7341/html/class_adafruit___a_s7341.html#ae363bc9fd58634da9ac1701cc69b9dff \ Ex, assuming sensor_one is on Wire and sensor_two is on Wire1:

if (!sensor_one.begin()){
Serial.println("Could not find AS7341");
while (1) { delay(10); }
}

if (!sensor_two.begin(AS7341_I2CADDR_DEFAULT, &Wire1)){
Serial.println("Could not find AS7341");
while (1) { delay(10); }
}

Using an I2C muxer is suggested as a better solution to this: https://learn.adafruit.com/working-with-multiple-i2c-devices

libbierose86 commented 1 year ago

Hi,

Thanks I did try that, think I must have an issue somewhere else and I just went down the wrong rabbit whole for what the issue is. Even if I do just one sensor using the library the serial stops working with windows saying that the Device Descriptor Requested Failed.

caternuson commented 1 year ago

Sounds like something more basic is happening. Please post in the forums with photos of your setup: https://forums.adafruit.com/

Just tested the basic_counts example a Pi Pico with AS7341 on pins 4 (SDA) and 5 (SCL) and it works as expected:

F2 445nm : 0.01
F3 480nm : 0.01
F4 515nm : 0.05
F5 555nm : 0.10
F6 590nm : 0.03
F7 630nm : 0.21
F8 680nm : 0.03
Clear    : 0.07
NIR      : 0.03
libbierose86 commented 1 year ago

Hi, I will do later.

If I run the example with no changes its fine, for one sensor it is fine. When I try to set SDA and SCL to different pins it stops working. I might just do it without the library, I know I can see the sensor and confirmed they are talking without it. Was just hoping to save a bit of time.

Thanks

Libbie