Saturday, May 18, 2013

TCS34725 RGB Color Sensor & the Beaglebone Black

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

The Ångström Linux distribution that comes with the Beaglebone includes the I2C tools (such as i2cdetect). You will, however, need to install the python-smbus module. Be sure to run opkg update first, and then  run the following command to install the python-smbus package: opkg install python-smbus.


TCS34725  Beaglebone Black

SDA       P9 20
SCL       P9 19
3V3       P9 3
GND       P9 1

Python Code

The only difference in the code is that the Beaglebone Black uses I2C bus 1 for the SCL and SDA pins -

import smbus
import time
bus = smbus.SMBus(1) # Beaglebone Black uses bus 1
# 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.

Note that if you want to use the i2cdetect utility on the Beaglebone Black to check the address of the connected I2C device, you need to run the command i2cdetect -r  -y 1.

1 comment: