Running Multiple TCS34725 RGB Sensors

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
MenaHagag
 
Posts: 3
Joined: Tue Jun 23, 2015 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by MenaHagag »

Hi,

I am new to Arduino. I recently purchased 4 TCS34725 RGB Sensors from Adafruit and would like to run them through the same Arduino. However, I want to run them consequently using one code. I want to turn on sensor 1, run the code, save the output then turn it off in a specific amount of time. Then wait for 4 hours and repeat the steps for sensor 2 and so on. I tested my code on the 4 sensors separately and its working the way it should. My next step is to run all the sensors on one ARDUINO UNO R3 consequently.

If someone could help me with the code and the configuration, I'd be grateful.

Thank you for your time.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_mike »

The TCS34725 doesn't use much current, so you can power one from an Arduino pin.

Try connecting each sensor's VCC pin to its own Arduino pin. Keep three pins low and send one pin high to turn on one sensor at a time.

User avatar
MenaHagag
 
Posts: 3
Joined: Tue Jun 23, 2015 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by MenaHagag »

Hi ,

Thank you for replying. I didn't understand what did you mean by "Keep three pins low and send one pin high to turn on one sensor at a time" and how to do it( through code or circuit connection). I tired using 2 multiplexers(SN74LS151N). One for SCL and one for SDA, however, I couldn't figure out how to connect the selection lines of the multiplexers to the Arduino.

Thank you for your time.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_mike »

The 74*151 can't multiplex I2C signals.

I2C device use what are called 'open drain' outputs. Instead of changing between high and low voltage levels, the pins change between high and low resistance connections to GND. Open drain outputs can share the same wire because it doesn't matter how many connections are open or closed at the same time. Multiple high-resistance connections just look like another high-resistance connection, and multiple low-resistance connections to GND look like a slightly lower-resistance connection to GND.

Outputs like the ones in the 74*151 can't share the same wire. If one pin wants to send the line high while another wants to send the line low, they'll fight with each other. The actual voltage on the wire will be somewhere between VCC and GND, but won't be predictable. Worse yet, the two fighting pins will basically create a short circuit between VCC and GND that could destroy one or both chips.

My suggestion was to control the power to the TCS34725 breakouts.

Connect Arduino output pin #2 to Vin on one TCS34725 breakout. Connect pin #3 to Vin on the second, and connect pin #4 to Vin on the third.

To turn all the TCS34725s off, send pins 2, 3, & 4 LOW. To talk to the first breakout, send pin 2 HIGH and leave pins 3 & 4 LOW. To talk to the second breakout, send pin 3 HIGH and leave pins 2 & 4 LOW. Only the TCS34725 whose Vin pin is HIGH will have power, so only that chip will respond to messages on the I2C bus.

User avatar
MenaHagag
 
Posts: 3
Joined: Tue Jun 23, 2015 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by MenaHagag »

Hi,

I did your suggestion and it works for 2 sensors only. I need 4 of them to be working.
I used an oscilloscope to measure the voltage as I am adding sensors: 1 sensor Vrms= 4V, for 2 sensor Vrms keeps changing from 1.8V to 3.006 V , then when I connect the 3rd sensor it drops to 64.15mV and nothing works because there is no enough Voltage to power up the sensors. Also in the code it is written " Connect VDD to 3.3V DC" are we still doing this if we are connecting pins 2, 3 and 4 to power up the sensors ?

This is the code I used for 2 sensors :

Code: Select all

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

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */
   
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

uint16_t r, g, b, c, colorTemp, lux = 0;  // define values for color sensor readings, initialize to 0

int sensorA = 2;  // digital pin 2 powers sensor A
int sensorB = 3;  // digital pin 3 powers sensor B

void setup(void) {
  Serial.begin(9600);
  pinMode(sensorA, OUTPUT);  // set pin to power sensorA
  pinMode(sensorB, OUTPUT);  // set pin to power sensorB
}

void loop(void) {
  
  // Using two FOR loops to collect samples from independent color sensors.  Could probably condense into further functions, maybe in next revision.  

  // loop to sample sensorB 5 times. NOTE: the first 2 samples (i = 0, 1) will be incorrect; samples i = 2,3,4 give proper readings.
  for(int i = 0; i < 5; i++){
    sensorON(sensorB);
    delay(1000);
  }
  
  digitalWrite(sensorB, LOW);
  
  delay(3000);
  
  for(int j = 0; j < 5; j++){
    sensorON(sensorA);
    delay(1000);
  }
  
  digitalWrite(sensorA, LOW);
  
  delay(3000);
  
}

// create function to call the sensor depending on the pin desired
void sensorON(int pin){  // "pin" is determined in the main loop, and is the pin for the sensor that is getting powered on

  Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);  // initialize sensor values each time the function gets called

  digitalWrite(pin, HIGH);  // set "pin" to high, powering the sensor
    
  // collect data from color sensor
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);
  
  // display data
  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  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(c, DEC); Serial.print(" ");
  Serial.println(" ");
}

. This is the code I used for 3 sesnsors:

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

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */
   
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

uint16_t r, g, b, c, colorTemp, lux = 0;  // define values for color sensor readings, initialize to 0

int sensorA = 2;  // digital pin 7 powers sensor A
int sensorB = 3;  // digital pin 8 powers sensor B
int sensorC = 4;  // digital pin 9 powers sensor C

void setup(void) {
  Serial.begin(9600);
  pinMode(sensorA, OUTPUT);  // set pin to power sensorA
  pinMode(sensorB, OUTPUT);  // set pin to power sensorB
  pinMode(sensorC, OUTPUT);  // set pin to power sensorC

}

void loop(void) {
  
  // Using two FOR loops to collect samples from independent color sensors.  Could probably condense into further functions, maybe in next revision.  

  // loop to sample sensorB 5 times. NOTE: the first 2 samples (i = 0, 1) will be incorrect; samples i = 2,3,4 give proper readings.
  for(int i = 0; i < 5; i++){
    sensorON(sensorB);
    delay(1000);
  }
  
  digitalWrite(sensorB, LOW);
  
  delay(3000);
  
  for(int j = 0; j < 5; j++){
    sensorON(sensorA);
    delay(1000);
  }
  
  digitalWrite(sensorA, LOW);
  
  delay(3000);
  
   for(int j = 0; j < 5; j++){
    sensorON(sensorC);
    delay(1000);
  }
  
  digitalWrite(sensorC, LOW);
  
  delay(3000);
  
}

// create function to call the sensor depending on the pin desired
void sensorON(int pin){  // "pin" is determined in the main loop, and is the pin for the sensor that is getting powered on

  Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);  // initialize sensor values each time the function gets called

  digitalWrite(pin, HIGH);  // set "pin" to high, powering the sensor
    
  // collect data from color sensor
  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);
  
  // display data
  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  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(c, DEC); Serial.print(" ");
  Serial.println(" ");
}
Thank you very much .
Last edited by adafruit_support_mike on Fri Jul 10, 2015 12:26 am, edited 1 time in total.
Reason: added CODE tags to preserve formatting

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_mike »

From your description, it sounds more like a hardware problem than a software problem.

Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

User avatar
Fire
 
Posts: 1
Joined: Wed May 04, 2016 9:42 am

Re: Running Multiple TCS34725 RGB Sensors

Post by Fire »

I was faced with the same problem in my project ( 2 TCS34725 in the same Arduino).
After long time searching the resolve - I've done my own.

Here is my solution:
https://github.com/Fire7/Adafruit_TCS34725_SoftI2C

It uses SoftwareWire library to emulate SDA SCL on any digital pin!
You just need to add pins in Adafruit_TCS34725 library constructor (view description)
Of course I've tested it and it worked perfectly!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_bill »

Nice! Thanks for posting your solution.

User avatar
haidangmz108
 
Posts: 1
Joined: Tue Jun 20, 2017 12:20 am

Re: Running Multiple TCS34725 RGB Sensors

Post by haidangmz108 »

Dear adafruit_support!
I have a question about to connect Two more sensor on the same arduino.
What happen if we turn on and turn of sensor power alot (VIN high-low). It will be died.
Why don't we use opto to connect and disconnect SDA pin when we want to read each sensor ?
I have never tried it before but that is my idea.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_bill »

Turning off VIN will not work, since it will turn the bus pullup resistors into pulldown resistors.
Opto-isolators will not work because it is a multi-drop bus with open-drain outputs as described by Mike above.
The best solution is to use an i2c multiplexer like this one: https://www.adafruit.com/product/2717

User avatar
JohanHoltby
 
Posts: 4
Joined: Mon Nov 20, 2017 3:06 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by JohanHoltby »

Hi,
I intend to use 6 of them from the same Arduino. I understand the problem with changing the output pin to low as explained in post before. However what can be done is changing the output to an input pin and rendering it essentially free floating (the internal pin state will only act as a pull up or pull down). However I would keep it low to avoid the extrem low current consumption to get some strange wrietes to the device. I have not tried it out yet but in theory it should work. If that is not the case I will use a N-channel FET to ground the device with the Arduino connected to the gate. If the device don't have a ground i't can't receive or send on the I2C.

Hope that helps some one some day. :)

User avatar
marcelrobitaille
 
Posts: 5
Joined: Wed Nov 29, 2017 1:12 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by marcelrobitaille »

Hello.

I am trying to toggle between two TCS34725 sensors. I have the Vin pins connected to two digital pins of my arduino. I have both SCL and SDA pins connected to the corresponding arduino pins. If I unplug the SCL and SDA pins from the other sensor, everything works as expected. When I plug them back in, the `tcs.begin()` function hangs forever. I can see the led on the second one light up dimly.

Since you say they are open drain, shouldn't toggling the Vin pins work as expected? Am I doing anything wrong?

I can post my code if needed, but it would be complicated to remove everything else. Since it works with just one and I can verify that the Vin pins are at the proper voltage with my multimeter I don't think that's the problem.

Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_bill »

As explained above, setting the VIN pin LOW turns your pullups into pull-downs. It doesn't just turn the device off, it disrupts communication on the bus.

An i2c multiplexer is the correct solution: https://www.adafruit.com/product/2717

User avatar
marcelrobitaille
 
Posts: 5
Joined: Wed Nov 29, 2017 1:12 pm

Re: Running Multiple TCS34725 RGB Sensors

Post by marcelrobitaille »

adafruit_support_bill wrote:An i2c multiplexer is the correct solution: https://www.adafruit.com/product/2717
That's just a link to the sensor...

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running Multiple TCS34725 RGB Sensors

Post by adafruit_support_bill »

It is a link to the TCA9548A i2c multiplexer.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”