Saturday, May 18, 2013

TCS34725 RGB Color Sensor & the Raspberry Pi

The TCS34725 RGB color sensor board from Adafruit works with the Raspberry Pi using an I2C connection. You can find the datasheet here.

The Python example below reports the clear, red, green, and blue light levels for a given sample (printing them to the console).

Connections


TCS34725  RPi
SDA       P3 GPIO 0 (SDA)
SCL       P5 GPIO 1 (SCL)
3V3       P1 3V3
GND       P9



Python Code


import smbus
import time
bus = smbus.SMBus(1)
# I2C address 0x29
# Register 0x12 has device ver. 
# Register addresses must be OR'ed with 0x80
bus.write_byte(0x29,0x80|0x12)
ver = bus.read_byte(0x29)
# version # should be 0x44
if ver == 0x44:
print "Device found\n"
bus.write_byte(0x29, 0x80|0x00) # 0x00 = ENABLE register
bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
bus.write_byte(0x29, 0x80|0x14) # Reading results start register 14, LSB then MSB
while True:
data = bus.read_i2c_block_data(0x29, 0)
clear = clear = data[1] << 8 | data[0]
red = data[3] << 8 | data[2]
green = data[5] << 8 | data[4]
blue = data[7] << 8 | data[6]
crgb = "C: %s, R: %s, G: %s, B: %s\n" % (clear, red, green, blue)
print crgb
time.sleep(1)
else: 
print "Device not found\n"


To keep this example simple, I have left the timing/power consumption for the analog to digital converters on the sensor board and sensor gain at their default values. For more information about these (and other settings) see the datasheet.

16 comments:

  1. hi i have been using TCS34725 for a twin note detection process. I have a problem in the timing of the processor. Do you know how long does it take to convert the collected RGB values into the Digital values

    ReplyDelete
    Replies
    1. Hi Yeshwanth -

      Have a look at page 8 of the data sheet. Does that answer your question?

      Delete
  2. Hi Brad,
    Excellent program in python for the TCS34725. Very useful to me.
    However, I get an Input/Output error.
    Do you know what can I do to get it to work?
    I've verified my connections a dozen times. I run on Arch Linux, and have also done "modprobe i2c-dev", "modprobe i2c-bm2708" and "i2cdetect -y 1" and it doesn't detect any devices connected.
    Do you know if I need to do something else?

    ReplyDelete
    Replies
    1. Sorry. Fixed. It was my soldering.

      Now your program already works smoothly. Thank you.

      Delete
  3. Hi Brad,
    Thanks for your article which is very useful !
    I had a little question about the number of TCS34725 that you can connect to a Raspberry Pi (any model). From what I understand, it's only possible to connect 1 TCS34725 and not more, am I right ? If yes, would you see a solution to connect 3 TCS34725 and deal with their informations separatly ?
    Thanks a lot,
    Regards,
    Maxime

    ReplyDelete
    Replies
    1. Hi Maxime -

      According to the data sheet for the TCS34725, it looks like the device's I2C address can't be changed (see http://www.adafruit.com/datasheets/TCS34725.pdf on p. 3), so you could only have one on a I2C bus. I've seen some discussions about using the 2nd I2C bus on the RPi, but I haven't tried it. (See, for instance, http://raspberrypi.znix.com/hipidocs/topic_i2cbus_2.htm).

      --Brad

      Delete
    2. Hi Brad,

      Thanks a lot for your fast reply !
      It helped me a lot :) I'll try to use the second I2C bus !

      Regards,
      Maxime

      Delete
  4. Can you tell me how I could modify the program to turn the LED on and off?

    ReplyDelete
    Replies
    1. Hi -

      I don't believe that you can. The data sheet doesn't show any commands or configuration registers that have that functionality.

      --Brad

      Delete
    2. The Adafruit tutorial (https://learn.adafruit.com/downloads/pdf/adafruit-color-sensors.pdf) mentions that the breakout board version of the TCS34725 allows the LED to be turned on and off using setInterrupt() . Thanks for your response.

      Delete
    3. I managed to get it working using:

      https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_TCS34725/Adafruit_TCS34725_Example.py

      I had to manually set the device number and change the integration time to 0xD5 (I kept getting the "too dark" error).

      I also had to remove the last line for it to stay off when not in use.

      Delete
  5. bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
    do i need to do or with 0x80 here?

    ReplyDelete
  6. Hey can i use this sensor for detecting coloured fruits like red and green apples...or like red, green, yellow tomatoes

    ReplyDelete
  7. Can i use this color sensor to detect the color of the liquid in a test tube?

    ReplyDelete