10. RGB-sensor TCS34725

 
Library:
Library met voorbeelden downloaden: https://github.com/adafruit/Adafruit_TCS34725
 
 
 
 
 
aansluitschema:
 
 
rode draad: 5V
zwarte draad: GND
SDA : oranje draad A4
SCL: witte draad A5
 
- Denk aan common anode of common cathode RGB Led.
- Pinnen 3,5 en 6 zijn de binnen die verbonden worden met resp. R G en B.
- Pin 13 wordt 'hoog' als de waarde van rood boven 1200 uitkomt.
 
 
code voorbeeld:
 
#include <Wire.h>
#include "Adafruit_TCS34725.h"

// Pick analog outputs, for the UNO these three work well
// use ~560  ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground

// set to false if using a common cathode LED
#define commonAnode true
int ledPin = 13;
 

// our RGB -> eye-recognized gamma color
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);

 pinMode (ledPin, OUTPUT);
   
  // thanks PhilB for this gamma table!
  // it helps convert RGB colors to what humans see
  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);  // takes 50ms to read
 
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED
 
  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);

  if (red>1200) digitalWrite (ledPin, HIGH);
  else  digitalWrite (ledPin,LOW);
  Serial.println (red);

  // 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]);
  delay(1000);
 
}     
  
In combinatie met (RGB) Leds:

 
 
 
kleurensensor TCS 230 met Arduino:


code voorbeeld:



/*
TCS230     Arduino Pins
 VCC        5V
 GND        GND
 s0         GND
 s1         5V
 s2         3
 s3         2
 OUT        4
 OE         GND
*/

#include <Adafruit_NeoPixel.h>
 
#define ps2 3
#define ps3 2
#define pout 4
#define RGB_out 8
#define NUMPIXELS 1 // I only use ONE of the neopixels on the strip

int red = 0;  
int green = 0;  
int blue = 0;  

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, RGB_out, NEO_GRB + NEO_KHZ800);

void setup()   
{  
  Serial.begin(9600);    
  pinMode(ps2, OUTPUT);  
  pinMode(ps3, OUTPUT);  
  pinMode(pout, INPUT);  
  pinMode(RGB_out, OUTPUT);  
  pixels.begin();
}  
    
void loop() 
{  
  char tmp[3];
  get_color(); 

  pixels.setPixelColor(0, pixels.Color(red,green,blue));
  pixels.show();
  
  // This next bit sends the values ion HEX format back to the PC
  sprintf(tmp, "%02X", blue);
  Serial.print(tmp);
  sprintf(tmp, "%02X", green);
  Serial.print(tmp);
  sprintf(tmp, "%02X", red);
  Serial.print(tmp);
  Serial.println(); 
  
  delay(100);   
  
 }  
    
void get_color()  
{    
  // S2 S3  Filter
  //  0  0  red
  //  0  1  blue
  //  1  1  green
  
  digitalWrite(ps2, LOW);      // set filter to red
  digitalWrite(ps3, LOW);      // set filter to red
  while(digitalRead(pout)){};  // wait for output to go LOW
  red = pulseIn(pout, LOW);    // get the value for red
  
  digitalWrite(ps3, HIGH);     // set filter to blue
  while(digitalRead(pout)){};  // wait for output to go LOW
  blue = pulseIn(pout, LOW);   // get the value for blue
  
  digitalWrite(ps2, HIGH);     // set filter to green
  while(digitalRead(pout)){};  // wait for output to go LOW
  green = pulseIn(pout, LOW);  // get the value for green

  
  red=constrain(red,192,5200);     // limit the values of r,g, and b
  green=constrain(green,202,5200);
  blue=constrain(blue,145,3800);
   
  red   = map(red,192,5200,255,0);   // remap the values so they fit into an 8 bit scale (0..255)
  green = map(green,202,5200,255,0);
  blue  = map(blue,145,3800,255,0);

    
}