Saturday, May 18, 2013

TCS34725 RGB Color Sensor & the pcDuino

I posted earlier about using the TCS34725 RGB Color Sensor board from Adafuit on my Raspberry Pi blog. The TSC34725 connects via I2C, so it is easy t connect to the pcDuino. The same sample Python code that runs on Raspberry Pi also runs on the pcDuino with one small modification.


TCS34725  pcDuino

SDA       SDA
SCL       SCL
3V3       3V3
GND       GND

Python Code

The only difference in the code is that the pcDuino uses I2C bus 2 for the SCL and SDA pins -

import smbus
import time
bus = smbus.SMBus(2) # pcDuino uses bus 2
# I2C address 0x29
# Register 0x12 has device ver. 
# Register addresses must be OR'ed with 0x80
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
    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.

1 comment:

  1. Very usefull tutorial. We forward your post to If you have any other new post about pcDuino( or any other LinkSprite's( products, please feel free to let us know. If you have a new post, could you please give us a backlink?