Tristimulus Colour Sensors Program Setup

I have 3 x Sparkfun OPT4048 Tristimulus Colour Sensors, all 3 are on the same I2C wire but their address has been changed to be unique for each, this has been confirmed to be working with Arduino I2C scanner and each comes back as happy on their own channel.

My issue is being able to specify which of the 3 sensors to retrieve data from in software.
I wish to be able to read sensor #1, read sensor #2, etc then display/process results.
In the past i used a chip to select between 8 inputs to read due to chips having a fixed I2C address on both, but this time i wish to avoid excess components.

Searching online has sent me in circles with no real answers that can explain how to setup the code to be able to address the sensors individually.

My simple request is has anyone else setup multiple of the same I2C device on the same channel but each having it’s own ID and then being able to read each uniquely.

The setup is
Arduino Nano – I2C – Sensor (0x44) – Sensor (0x45) – Sensor (0x46)

1 Like

Hey @Lina95487, welcome to the forums!

I may be missing some of the complexity of this task but have you tried defining your sensors in code like this?

#include <Wire.h>
#include <SparkFun_OPT4048.h>

// Create instances for each sensor with the default address
SparkFun_OPT4048 myColor1;
SparkFun_OPT4048 myColor2;
SparkFun_OPT4048 myColor3;


void setup() {
  // Initialise the Wire library
  Wire.begin();

  // Specify the I2C addresses of the sensors
  myColor1.begin(0x44); // Sensor #1 address 0x44
  myColor2.begin(0x45); // Sensor #2 address 0x45
  myColor3.begin(0x46); // Sensor #3 address 0x46
}

void loop() {
}

From my understanding of the SparkFun_OPT4048 library you should be able to define unique I2C ports using .begin()

Hope this helps! :grinning:

1 Like

Thank you! … that’s what i was looking for. :slight_smile:

:thinking: i think that works, well the output has changed :slight_smile:
Sensor 1 - providing feedback (Yes!)
Sensor 2 - NAN
Sensor 3 - NAN

I have a suspicion the inlude file is locked into using one address only but my skill set isn’t to that level yet.
Thank you for the reply it has progressed my project further.

2 Likes

Hi @Lina95487,

Glad Sam was able to help.

If you share your code we may be able to see what might be causing the issue.

1 Like

My cut down code is …

#include "SparkFun_OPT4048.h"

SparkFun_OPT4048 myColor;
SparkFun_OPT4048 myColor2;
SparkFun_OPT4048 myColor3;

String Result;
String Result2;
String Result3;

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

  myColor.begin(0x44);
  myColor2.begin(0x45);
  myColor3.begin(0x46);
  myColor.setRange(RANGE_18LUX);  //- Pipette / Vial
  myColor.setConversionTime(CONVERSION_TIME_800MS);
  myColor.setOperationMode(OPERATION_MODE_CONTINUOUS);
  Serial.println("Ready - Vials"); 
}

void loop()
{
//Serial input trigger code
        case 6:
          ReadResult();
          Serial.println(Result);
          break;
        case 7:
          ReadResult2();
          Serial.println(Result2);      
          break;
        case 8:
          ReadResult3();
          Serial.println(Result3);
          break; 
//End serial input trigger
}

void ReadResult() {
  Result = "";
  Result = "CIEx: ";
  Result += myColor.getCIEx();
  Result += " CIEy: ";
  Result += myColor.getCIEy();
  Result += " Lux: ";
  Result += myColor.getLux();
  Result += " Warmth: ";
  Result += myColor.getCCT();
  delay(500); //Delay if performing multiple reads
}
void ReadResult2() {
  Result2 = "";
  Result2 = "CIEx: ";
  Result2 += myColor2.getCIEx();
  Result2 += " CIEy: ";
  Result2 += myColor2.getCIEy();
  Result2 += " Lux: ";
  Result2 += myColor2.getLux();
  Result2 += " Warmth: ";
  Result2 += myColor2.getCCT();
  delay(500);
}
void ReadResult3() {
  Result3 = "";
  Result3 = "CIEx: ";
  Result3 += myColor3.getCIEx();
  Result3 += " CIEy: ";
  Result3 += myColor3.getCIEy();
  Result3 += " Lux: ";
  Result3 += myColor3.getLux();
  Result3 += " Warmth: ";
  Result3 += myColor3.getCCT();
  delay(500);
}

:grimacing: i think i just noticed my goof. …
Someone …Me … Split the sensors into myColor x3 … but only setup 1 :pleading_face:

@Aaron Thank you for the advice i believe the cause of grey hair has been located …

1 Like