Notes About Working with Various Arduino & Netduino Microcontroller Boards

Thursday, May 16, 2013

TCS34725 RGB Color Sensor & the Netduino Plus 2

The TCS34725 RGB color sensor board from Adafruit works with the Netduino Plus 2 using an I2C connection. You can find the datasheet here. The C# example below reports the clear, red, green, and blue light levels for a given sample (printing them to the debug console in Visual Studio).

Connections


Connect SDA, SCL, 3V3, and GND on the sensor to the corresponding pins on the Netduino Plus 2.  No pull-up resistors are needed (they are included in the breakout board).


Code


using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace TCS34725
{
    public class Program
    {
        const int DEV_ADDR   = 0x29;
        const int CMD_BIT    = 0x80;
        const int ENABLE     = 0x00;
        const int ENABLE_PON = 0x01;
        const int ENABLE_AEN = 0x02;
        const int DEV_ID     = 0x12;
        const int RGB_DATA   = 0x14;
  
        public static void Main()
        {
            I2CDevice tcs = new I2CDevice(
                new I2CDevice.Configuration(DEV_ADDR, 100)
            );

            byte[] getIDCmd = { CMD_BIT | DEV_ID };
            byte[] id = { 0 };
            I2CDevice.I2CTransaction[] getDevID = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(getIDCmd),
                I2CDevice.CreateReadTransaction(id)
            };
            int bytesRead = tcs.Execute(getDevID, 100);
            if (id[0] == 0x44)
            {
                Debug.Print("\nOK - Device found.\n");
            }
            else
            {
                Debug.Print("\nERROR - Device not found.\n");
                while(true) { ; }
            }

            byte[] beginCmd = { CMD_BIT | ENABLE, (ENABLE_PON | ENABLE_AEN) & 0xFF };
            I2CDevice.I2CTransaction[] begin = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(beginCmd)
            };
            bytesRead = tcs.Execute(begin, 100);

            while (true)
            {
                byte[] readCmd = { CMD_BIT | RGB_DATA };
                byte[] colorData = { 0, 0, 0, 0, 0, 0, 0, 0 };
                I2CDevice.I2CTransaction[] read = new I2CDevice.I2CTransaction[] {
                    I2CDevice.CreateWriteTransaction(readCmd),
                    I2CDevice.CreateReadTransaction(colorData)
                };
                bytesRead = tcs.Execute(read, 100);
                int clear = ((colorData[1] << 8) | colorData[0]);
                int red = ((colorData[3] << 8) | colorData[2]);
                int green = ((colorData[5] << 8) | colorData[4]);
                int blue = ((colorData[7] << 8) | colorData[6]);
                string result = "C: " + clear.ToString() + " ";
                result += "R: " + red.ToString() + " ";
                result += "G: " + green.ToString() + " ";
                result += "B: " + blue.ToString();
                Debug.Print(result);
                Thread.Sleep(1000);
            }
        } 
    }
}

No comments:

Post a Comment