RGB Color Sensor using TCS34725 Module

Overview:

Color Sensor? No problem. I have found the best color sensor in the market. The TCS34725 which has RGB(Red, Green, Blue) and clear light sensing elements. It has a Infrared blocking filter integrated on the chip, minimizes the Infrared spectral components of the light and allows color measurement to be made. IR filter also block infrared lights for use under varied light condition. In this activity, you will learn how the TCS34725 works with help of some electronics components. This activity is also pretty straight forward exercise which helps to easily understand how TCS34725 works.

 

Hardware Used:


  • TCS34725
  • 
    
  • Breadboard
  • 
    
  • RGB led
  • 3 – 220 ohms Resistor
  • 
    
  • Jumper Wires
  • 
    

    You can buy this at https://createlabz.store/

    Software Used:

    Library Used:

    Application Description:

    This project is to test how TCS34725 works with the use of some electronic components. With a brief description of TCS34725 it is a light to digital converter which contains a 3 x 4 photodiode array that is composed of red-filtered, green-filtered, blue-filtered and clear(unfiltered) photodiodes. It has also a photodiode which are coated with an IR-blocking filter to refer filters that pass infrared light while completely blocking other wavelengths it which block infrared light for use under varied light conditions and lastly, it is a device that provides a digital return of RGB(Red,Green and Blue) and clear light sensing values.

     

     

    https://schwiftyarduino.wordpress.com/2016/06/17/7-rgb-led-fade/

    So what I did here is a basic application of TCS34725 in which it can detect RGB colors with the use of RGB LED.

     

     

    RGB Colors

     

     

    Set-up the Hardware:

    Code:

     

    #include <Wire.h>
    #include "Adafruit_TCS34725.h"
    
    
    #define redpin 3
    #define greenpin 5
    #define bluepin 6
    
    #define commonAnode true
    
    
    byte gammatable[256];
    
    
    Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
    
    void setup() {
      Serial.begin(9600);
      Serial.println("Color View Test!");
    
      if (tcs.begin()) {
        Serial.println("Found sensor");
      } else {
        Serial.println("No TCS34725 found ... check your connections");
        while (1); // halt!
      }
      
      // use these three pins to drive an LED
      pinMode(redpin, OUTPUT);
      pinMode(greenpin, OUTPUT);
      pinMode(bluepin, OUTPUT);
      
      
      for (int i=0; i<256; i++) {
        float x = i;
        x /= 255;
        x = pow(x, 2.5);
        x *= 255;
          
        if (commonAnode) {
          gammatable[i] = 255 - x;
        } else {
          gammatable[i] = x;      
        }
        //Serial.println(gammatable[i]);
      }
    }
    
    
    void loop() {
      uint16_t clear, red, green, blue;
    
      tcs.setInterrupt(false);      // turn on LED
    
      delay(60);  
      
      tcs.getRawData(&red, &green, &blue, &clear);
    
      tcs.setInterrupt(true);  
      
      Serial.print("C:\t"); Serial.print(clear);
      Serial.print("\tR:\t"); Serial.print(red);
      Serial.print("\tG:\t"); Serial.print(green);
      Serial.print("\tB:\t"); Serial.print(blue);
    
      // Figure out some basic hex code for visualization
      uint32_t sum = clear;
      float r, g, b;
      r = red; r /= sum;
      g = green; g /= sum;
      b = blue; b /= sum;
      r *= 256; g *= 256; b *= 256;
      Serial.print("\t");
      Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
      Serial.println();
    
      //Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
    
      analogWrite(redpin, gammatable[(int)r]);
      analogWrite(greenpin, gammatable[(int)g]);
      analogWrite(bluepin, gammatable[(int)b]);
    }

     

    Code Breakdown:

     

    #define redpin 3
    #define greenpin 5
    #define bluepin 6

    This part is where you will assign the pins of RGB into the arduino. So i’ve selected the 3 as my red pin, 5 as my green pin and 6 as my blue pin and the last pin is for VCC because i’ve used a common ANODE RGB led. If you have used a common CATHODE then it should be grounded.

    #define commonAnode true

    Another one, if you have used a common ANODE RGB led set this to TRUE and FALSE if you have used a common CATHODE.

    pinMode(redpin, OUTPUT);
    pinMode(greenpin, OUTPUT);
    pinMode(bluepin, OUTPUT);

    In this part we assign our RGB led to be the output from the RGB sensor. We used this three pins the drive the RGB led.

    analogWrite(redpin, gammatable[(int)r]);
    analogWrite(greenpin, gammatable[(int)g]);
    analogWrite(bluepin, gammatable[(int)b]);

    Lastly, We used AnalogWrite because we expect our output to any number compared to DigitalWrite the outputs are only HIGH and LOW.

    Conclusion:

    There are so many applications of RGB Sensor especially in industries, especially in factories. I remember the time that we had our plant tour, we visit some of mango factories. According to my research most of the mango factories use this Color Sensor in order to sort mangoes that are raw and ripe. Ripe mangoes are the yellow ones and raw mangoes are the green ones. Most vegetable companies also uses Color Sensor to separate according to its color and many more application to this module. Just search and understand what you are doing and everything will be fine.

    References:

    https://cdn-shop.adafruit.com/datasheets/TCS34725.pdf

    https://www.adafruit.com/product/1334

     

     

    The post RGB Color Sensor using TCS34725 Module appeared first on CreateLabz.

    BlogColorKnowledgebaseLedModuleRgbSensorSorting color

    Leave a comment

    All comments are moderated before being published