Sherwin Chiu
Published © GPL3+

Multiplexing 6 I2C TCS34725 Color Sensors

Have you ever had to use sensors that use the I2C protocol, but realize that they all have the same, non-changeable address?

BeginnerProtip3 hours5,943
Multiplexing 6 I2C TCS34725 Color Sensors

Things used in this project

Hardware components

Gravity: TCS34725 RGB Color Sensor For Arduino
DFRobot Gravity: TCS34725 RGB Color Sensor For Arduino
×1
TCA9548A
×1
Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Breadboard, 830 Tie Points
Breadboard, 830 Tie Points

Story

Read more

Schematics

Schematic

Code

tcs34725.ino

C/C++
// Setting up 6 TCS34725 sensors together and reading their output values
// TCA9548A

#include <Wire.h>
#include "Adafruit_TCS34725.h"

#define  inPin  7
#define  rPin   8
#define  gPin   9
#define  bPin   10

byte multiAddress = 0x70;
byte count = 0;
int currentTime = 0;

char data[6][3] = {{'r', 'g', 'b'},
                 {'r', 'g', 'b'},
                 {'r', 'g', 'b'},
                 {'r', 'g', 'b'},
                 {'r', 'g', 'b'},
                 {'r', 'g', 'b'}};
Adafruit_TCS34725 tcs[] = {Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X),
                           Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X),
                           Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X),
                           Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X),
                           Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X),
                           Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X)};
byte gammatable[256];

void setup(){
    Serial.begin(9600);
    Wire.begin();
    pinMode(inPin, INPUT_PULLUP);
    pinMode(rPin, OUTPUT);
    pinMode(gPin, OUTPUT);
    pinMode(bPin, OUTPUT);
    // gammatable for more color accuracy when outputting on LED
    for(int i =0; i < 256; i++){
        float x = i;
        x /= 255;
        x = pow(x, 2.5);
        x *= 255;
        gammatable[i] = x;
    }
    initColorSensors();
    attachInterrupt(digitalPinToInterrupt(inPin), changeLED, CHANGE);
}
void loop(){
    //for(int i = 0; i < sizeof(data); i++){ // get all colors... not necessary right now 
    //    readColors(i);
    //}
    for(int i = 0; i < 5; i++){
        readColors(count);
    }
    
}
void changeLED(){
    if(millis() - currentTime > 200){
        count++;
        currentTime = millis();
    }
    if(count > 5){
        count = 0;
    }
   
}
void initColorSensors(){
    for(int i = 0; i < 6; i++){
        Serial.println(i);
        chooseBus(i);
        if (tcs[i].begin()){
            Serial.print("Found sensor "); Serial.println(i+1);
        } else{
            Serial.println("No Sensor Found");
            while (true);
        }
    }
}
void readColors(byte sensorNum){
    chooseBus(sensorNum);
    uint16_t r, g, b, c;
    tcs[sensorNum].getRawData(&r, &g, &b, &c); // reading the rgb values 16bits at a time from the i2c channel
    processColors(r, g, b, c); // processing by dividng by clear value and then multiplying by 256
    //data[sensorNum][0] = r;
    //data[sensorNum][1] = g;
    //data[sensorNum][2] = b;
    Serial.print("R: "); Serial.print(r, DEC); Serial.print(" "); 
    Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
    Serial.print("B: "); Serial.print(b, DEC); Serial.print(" "); Serial.print(c); Serial.print(" ");
    displayLED(r, g, b);
    Serial.println(sensorNum);
}
void processColors(uint16_t r, uint16_t g, uint16_t b, uint32_t c){
        // getting rid of IR component of light
       r /= c;
       g /= c;
       b /= c; 
       // adding it back in 
       r *= 256;
       g *= 256;
       b *= 256;
}
    
void chooseBus(uint8_t bus){
    Wire.beginTransmission(0x70);
    Wire.write(1 << (bus+2)); // will be using 2-7 instead of 0-5 because of convience (placed better on the breadboard)
    Wire.endTransmission();
}
void displayLED(uint16_t r, uint16_t g, uint16_t b){
    if (r > 255 || g > 255 || b > 255){
        analogWrite(rPin, 0);
        analogWrite(gPin, 0);
        analogWrite(bPin, 0);
    } else{
        analogWrite(rPin, gammatable[(int)r]);
        analogWrite(gPin, gammatable[(int)g]);
        analogWrite(bPin, gammatable[(int)b]); 
    }
    
}

Credits

Sherwin Chiu

Sherwin Chiu

7 projects • 7 followers
Just a guy who occasionally blows up capacitors. I love doing this type of stuff!

Comments