BM017 / TCS34725 Color Sensor I2C Python driver

Project Curacao Ready to Be Shipped in March 2014

We have made one more addition to the hardware on Project Curacao.  We have added a BM017 / TCS34725 RGBC color sensor to the device.  Here is the device attached to the back of the top of the box and then a picture of it peeking through (with the LED turned on for emphasis – it normally operates with the LED off). (Update 8/01/2014:  The Sensor has come back to life on Project Curacao.  A big wind gust?)



This device was purchased from solutions-cubed.com (excellent service and documentation).  Another version (compatible) can be had from Adafruit.

We wrote a Python driver for the BM017 / TCS34725 as we could not find one on the net that met our needs.

It has been posted on github under https://github.com/projectcuracao/PC_BM017CS for anyone who needs such a driver.

We connected the INT line to the LEDON pin (and the VDD_LED to VDD) and now we can switch the interrupt on and off and turn the white LED on and off without burning another GPIO pin.


The example code to test the library and the TCS34725 is below:

#!/usr/bin/python
 



 
# example driver for BM017 and TCS34725
 
 
import time
import smbus
 
from Adafruit_I2C import Adafruit_I2C
 
from PC_BM017CS import BM017
 

bm017 = BM017(True)
bm017.debug = True
 
bm017.readStatus()
 

bm017.isBM017There()


bm017.getColors()

bm017.readStatus()
 
bm017.disableDevice()
 
bm017.setIntegrationTimeAndGain(0x00, 0x03)

bm017.getColors()

bm017.readStatus()
 
 
bm017.readStatus()

 
# this will turn on the LED if LEDON is connected to INT and LEDVDD is connected to VDD_LED
 
bm017.setInterrupt(True)
 
time.sleep(5.0)

bm017.setInterrupt(False)

And the Results:

pi@projectCur ~/PC_BM107CS $ sudo python example.py
BM017 initialized
('BM017 Status=', 17)
BM017 / TCS34725 is present
('ColorList = ', [2, 0, 1, 0, 1, 0, 0, 0])
('clear_color= ', 2)
('red_color= ', 1)
('green_color= ', 1)
('blue_color= ', 0)
('BM017 Status=', 17)
('IT set to:', 0)
('Gain set to:', 3)
('ColorList = ', [8, 15, 157, 7, 105, 5, 151, 3])
('clear_color= ', 3848)
('red_color= ', 1949)
('green_color= ', 1385)
('blue_color= ', 919)
('BM017 Status=', 17)
('BM017 Status=', 17)
Interrupt On
Interrupt Off

We pointed the sensor at a spool of red wire.  That’s why the “red_color” is higher than the green and blue.

2 Comments

    • The trick is to normalize the numbers.

      Each of the RGB values is a 16 bit unsigned Integer. You need to ratio (2^8-1)/(2^16 -1) or in decimal, 255/65535.

      (‘red_color= ‘, 1949)
      (‘green_color= ‘, 1385)
      (‘blue_color= ‘, 919)

      in 255 RGB values, this would be:

      7, 5, 3

      Not very satisfying.

      You may want to adjust these values because of the gain involved. I would try normalizing the values by scaling 255 to the largest value (1949) of the RGB values. In other words, ScaledRed = (1949/1949)*255 = 255, ScaledGreen = (1385/1949)*255 = 181, ScaledBlue = (919/1949)*255 = 120

      So we end up with
      RGB = 255, 181, 120

      To avoid saturation, you might want to scale with 110% of the largest value. This gives

      RGB = 230, 163, 108

      Clearly the color is mostly red, as is correct.

1 Trackback / Pingback

  1. 5 Weeks of Project Curacao - RasPiConnect - SwitchDoc

Comments are closed.