Home MBed TCS34725 Color sensor and MBed 1768 example

TCS34725 Color sensor and MBed 1768 example

by shedboy71

In this example we connect a TCS34725 Color sensor to an MBed 1768, lets look at some details about this sensor first

The TCS34725 device provides a digital return of red, green, blue (RGB), and clear light sensing values. An IR blocking filter, integrated on-chip and localized to the color sensing photodiodes, minimizes the IR spectral component of the incoming light and allows color measurements to be made accurately.

The high sensitivity, wide dynamic range, and IR blocking filter make the TCS3472 an ideal color sensor solution for use under varying lighting conditions and through attenuating materials. This data is transferred via an I2C to the host.

  • Minimizes IR and UV spectral component effects to produce accurate color measurement
  • Enables accurate color and ambient light sensing under varying lighting conditions
  • Minimizes motion / transient errors
  • Clear-Channel provides a reference allows for isolation of color content

 

Parts List

Label Part Type Properties
MBED1 mbed Mbed 1768
Part1 Adafruit TCS34725 RGB Color Sensor

Layout

mbed and TCS34725

mbed and TCS34725

 

 

Code

This example uses the https://os.mbed.com/components/Adafruit-TCS34725-RGB-Color-Sensor/

[codesyntax lang=”cpp”]

#include "mbed.h"

I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL)
Serial pc(USBTX, USBRX);

int sensor_addr = 41 << 1;


int main() {
    pc.baud(9600);

    
    // Connect to the Color sensor and verify whether we connected to the correct sensor. 
    
    i2c.frequency(200000);
    
    char id_regval[1] = {146};
    char data[1] = {0};
    i2c.write(sensor_addr,id_regval,1, true);
    i2c.read(sensor_addr,data,1,false);
    

    
    // Initialize color sensor
    
    char timing_register[2] = {129,0};
    i2c.write(sensor_addr,timing_register,2,false);
    
    char control_register[2] = {143,0};
    i2c.write(sensor_addr,control_register,2,false);
    
    char enable_register[2] = {128,3};
    i2c.write(sensor_addr,enable_register,2,false);
    
    // Read data from color sensor (Clear/Red/Green/Blue)
    
    while (true) { 
        char clear_reg[1] = {148};
        char clear_data[2] = {0,0};
        i2c.write(sensor_addr,clear_reg,1, true);
        i2c.read(sensor_addr,clear_data,2, false);
        
        int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
        
        char red_reg[1] = {150};
        char red_data[2] = {0,0};
        i2c.write(sensor_addr,red_reg,1, true);
        i2c.read(sensor_addr,red_data,2, false);
        
        int red_value = ((int)red_data[1] << 8) | red_data[0];
        
        char green_reg[1] = {152};
        char green_data[2] = {0,0};
        i2c.write(sensor_addr,green_reg,1, true);
        i2c.read(sensor_addr,green_data,2, false);
        
        int green_value = ((int)green_data[1] << 8) | green_data[0];
        
        char blue_reg[1] = {154};
        char blue_data[2] = {0,0};
        i2c.write(sensor_addr,blue_reg,1, true);
        i2c.read(sensor_addr,blue_data,2, false);
        
        int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
        
        // print sensor readings
        
        pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\r\n", clear_value, red_value, green_value, blue_value);
        wait(0.5);
    }
    
}

[/codesyntax]

 

Testing

Using a terminal program such as TeraTerm you should see something like this

Clear (4248), Red (2551), Green (2133), Blue (1913)
Clear (3323), Red (1739), Green (1630), Blue (1458)
Clear (3323), Red (1739), Green (1630), Blue (1458)
Clear (4069), Red (3035), Green (2304), Blue (2184)
Clear (4124), Red (3078), Green (2338), Blue (2215)
Clear (4133), Red (3084), Green (2341), Blue (2218)
Clear (4235), Red (3128), Green (2390), Blue (2244)
Clear (5749), Red (2732), Green (2673), Blue (1664)
Clear (4989), Red (3010), Green (2574), Blue (2025)
Clear (4112), Red (3071), Green (2331), Blue (2210)
Clear (4112), Red (3071), Green (2331), Blue (2210)
Clear (2820), Red (2077), Green (1557), Blue (1474)
Clear (424), Red (275), Green (206), Blue (162)
Clear (409), Red (267), Green (202), Blue (160)
Clear (1167), Red (841), Green (634), Blue (578)
Clear (4342), Red (3158), Green (2387), Blue (2233)
Clear (6270), Red (3737), Green (3013), Blue (2516)
Clear (4007), Red (2979), Green (2250), Blue (2132)

Link

1PCS CJMCU-34725 TCS34725 Color Sensor RGB color sensor development board module

You may also like