TCS3200 Color Sensor help!!

Hi I’m a complete newbie in electronics with little experience on working with sensors. For our final year, we were given the task to create something for us to qualify to graduate so my team and I were given the suggestion of working with home automation pertaining to lights. I was wondering if the said color sensor (TCS3200) could ever be modified as a motion sensor? Like PIR? My instructor already said it detects “ambient surroundings” so my teammates and I assumed it really detects if it’s bright in the room or too dark.

I’m still trying to study Arduino programming language and I’m liking it but it’s still all too confusing for me so the code I have for the color sensor is only based on this website: http://www.dfrobot.com/wiki/index.php?title=TCS3200_Color_Sensor_(SKU:SEN0101) but we still haven’t seen the demonstration it produces on the Arduino board with the color sensor on the breadboard. I’m also still trying to understand the code like why is this and why is that but it’s too hazy for me yet.

I have so much questions but all I want to know for the moment is if it’s ever possible :frowning: :frowning:

Thank you.

Hi I'm a complete newbie in electronics with little experience on working with sensors. For our final year, we were given the task to create something for us to qualify to graduate so my team and I were given the suggestion of working with home automation pertaining to lights. I was wondering if the said color sensor (TCS3200) could ever be modified as a motion sensor? Like PIR? My instructor already said it detects "ambient surroundings" so my teammates and I assumed it really detects if it's bright in the room or too dark.

I'm still trying to study Arduino programming language and I'm liking it but it's still all too confusing for me so the code I have for the color sensor is only based on this website: TCS3200_Color_Sensor__SKU_SEN0101_-DFRobot but we still haven't seen the demonstration it produces on the Arduino board with the color sensor on the breadboard. I'm also still trying to understand the code like why is this and why is that but it's too hazy for me yet.

I have so much questions but all I want to know for the moment is if it's ever possible :frowning: :frowning:

Thank you.

P.S. I posted this the first time on the wrong forum

The long, but useful, story: LESSON 15: Arduino Color Sensor and RGB LED - YouTube

This video
uses this code.
Remove what you dont want (comments in Norwegian)

#include <Servo.h>   //Vi legger til biblioteket for servoene. 


Servo myservo;
Servo myservo1;      // 3x servoer blir definert
Servo myservo2;      

int tom = 0;         // Teller om armen er tom.
int posGreen = 11;   // possisjon for grønne smarties
int posLilla = 28;   // possisjon for Lilla smarties
int posRosa = 40;    // possisjon for Rosa smarties
int posGul = 51;     // possisjon for Gule smarties
int posRed = 64;     // possisjon for Røde smarties
int posOrange = 77;  // possisjon for Oransje smarties
int posBrun = 90;    // possisjon for Brune smarties
int posBlue =107;    // possisjon for Blå smarties
int val, val2, val3, counter = 0;   // variabel for avlesning av farge

long sum;

unsigned long white,red,blue,green;
byte led=4, S0=5, S1=6, S2=7, S3=8, taosOutPin = 12;
#define farger 10  //antall farger vi skal detektere + Sort
// lagre alle data i en tabell, for så å sjekke aktuell måling mot match
int tbl[farger+1][8]=   // OBS: linje 0..farger kolonne 0..7
{
//  rød     grønn      blå     *hvit*      	Om alle fire verdiene matcher så er fargen bestemt. Fargeverdier                                  //					stammer fra sensor
  {85,100,  65,80,   70,90,   0,9999}, //grønn
  {50,70,   80,95,   100,110, 110,200}, //Lilla
  {60,75,   65,75,   110,130, 110,160}, //Rosa
  {100,140, 50,90,   65,99,   90,170}, //Gul
  {70,85,   40,60,   110,145, 160,9999}, //rød
  {90,105,  0,60,    95,135,  120,400}, //Orange
  {80,95,   45,80,   80,110,  180,400},   //Brun
  {50,70,   92,130,  80,99,   81,300},  //blå
  {0,9999,  0,9999,  0,9999,  400,9999}, //sort
};

// Navn for å lese av farge serielt under testing. 
String navn[farger+1] ={"Gronn","Lilla","Rosa",
                "Gul","roed","Orange","brown",
                "blue","sort"};
bool match;
byte test; //teller



void setup() {
  Serial.begin(9600);    
  myservo.attach(9);    
  myservo1.attach(10);    //servoene settes på hver sin pinne
  myservo2.attach(11);
  pinMode(S0,OUTPUT); 
  pinMode(S1,OUTPUT); 
  pinMode(S2,OUTPUT); 
  pinMode(S3,OUTPUT); 
  pinMode(taosOutPin, INPUT); //data fra fargesensor
  pinMode(led,OUTPUT);
  // sett mode 2 (1:5 = 20%
  digitalWrite(S1, LOW); 
  digitalWrite(S0, HIGH); // slå på sensoren
//  digitalWrite(led, HIGH); 
  /* Mulighetene er: 
   * mode 0 (avslått)
    digitalWrite(S0, LOW);
    digitalWrite(S1, LOW);
   * mode 1 (1:1)
    digitalWrite(S0, HIGH);
    digitalWrite(S1, HIGH);
   * mode 2 (1:5)
    digitalWrite(S0, HIGH);
    digitalWrite(S1, LOW);
   * mode 3 (1:50)
    digitalWrite(S0, LOW);
    digitalWrite(S1, HIGH);
  */
}

void loop() 
{
    detectColor(taosOutPin); 
/* Til debuggning:
    Serial.println(red);
    Serial.println(green);   
    Serial.println(blue);
    Serial.println(white);
    Serial.println("-----");     
*/    
    // bestem fargen
    for (test=0; test<=farger; test++) //gjenta for N farger
    {  // anta først at vi har match på 'denne' fargen
     match=true;
     if ( red  <tbl[test][0] || red  >tbl[test][1]) match=false;
     if ( green<tbl[test][2] || green>tbl[test][3]) match=false;
     if ( blue <tbl[test][4] || blue >tbl[test][5]) match=false;
     if ( white<tbl[test][6] || white>tbl[test][7]) match=false;
    // hvis 'match' fortsatt er true, så har vi fargen
     if (match==true) break; //fargen er funnet - hopp ut av løkka
    }
    
    // følgende kode setter armen i gitt posisjon avhengig
    // av hvordan nummer fra 1 til 9 som vi fikk ut av fargesensoren
     if (test<(farger-1)) 
     {
       Serial.println(navn[test]);
       Serial.println(test);
       if( test == 0){
         tom = 0;
         myservo2.write(posGreen);
         delay (300);
       }
       if( test == 1){
         tom = 0;
         myservo2.write(posLilla);
         delay (300);
       }
       if( test == 2){
         tom = 0;
         myservo2.write(posRosa);
         delay (300);
       }
       if( test == 3){
         tom = 0;
         myservo2.write(posGul);
         delay (300);
       }
       if( test == 4){
         tom = 0;
         myservo2.write(posRed);
         delay (300);
       }
       if( test == 5){
         tom = 0;
         myservo2.write(posOrange);
         delay (300);
       }
       if( test == 6){
         tom = 0;
         myservo2.write(posBrun);
         delay (300);
       }
       if( test == 7){
         tom = 0;
         myservo2.write(posBlue);
         delay (300);
       }
       if(test == 8){
         delay(300);
         tom++;
         if(tom > 5){    // Om armen er tom 5 ganger på rad
           delay(7000);  // antar vi røret er tomt. Da
         }               // bruker den 4x så lang tid på en sortering.
       }
       
       
      myservo.write(30);     //Mini: Hent smarties fra rør
      delay(300);
      myservo1.write(137);   //Arm: Stopp rett før kanten, slik at
      delay(500);            //     slik at den bremeser ned før neste.
      myservo1.write(145);   //Arm: Beveg over kanten, slipper smarties. 
      delay(730);
      myservo1.write(34);    //Arm: Hent smarties ved mini servo.
      delay(500);
      myservo.write(103);    // Mini: Lever smarties til arm.
      delay(400);
      myservo1.write(58);    //Arm: Beveger seg til fargesensor
      delay(200);    
     }
} 
//----------------------------------------------------


void detectColor(byte taosOutPin)
{
  myservo1.write(57);
  delay(350);
  digitalWrite(led,HIGH); // slå på strømmen til lysdiodene
  white = colorRead(taosOutPin,0);
  green = colorRead(taosOutPin,1);
  red = colorRead(taosOutPin,2);
  blue = colorRead(taosOutPin,3);
  digitalWrite(led ,LOW); // slå av strømmen til lysdiodene
  sum=green+red+blue;  //finn samlet luminans
  green=(green<<8)/sum;  //ganget med 256 for å få hver farge i områder 0..255
  red=(red<<8)/sum;
  blue=(blue<<8)/sum;
       Serial.print("luminans:"); //Vi får fargene serielt for
       Serial.println(white);     //for å bruke dem til de-bugging. 
       Serial.print("green:");
       Serial.println(green);
       Serial.print("red:");
       Serial.println(red);
       Serial.print("blue:");
       Serial.println(blue);
       Serial.println(" ");
}

long colorRead(int taosOutPin, int farge)
{
//  digitalWrite(S1, LOW);  // med mode 1:5 (er low allerede)
  int sensorDelay = 100;
  //sett S2 og S3 til riktig farge
  if(farge == 0){ //white
    digitalWrite(S3, LOW);
    digitalWrite(S2, HIGH);
  } else 
  if(farge == 1){//red
    digitalWrite(S3, LOW); 
    digitalWrite(S2, LOW); 
  } else 
  if(farge == 2){//blue
    digitalWrite(S3, HIGH); 
    digitalWrite(S2, LOW);  
  }else 
  if(farge == 3){//green
    digitalWrite(S3, HIGH); 
    digitalWrite(S2, HIGH); 
  }
  delay(sensorDelay); //gi sensor tid til å stille seg inn
  // mål pulsbredden minst 3 ganger for 'utjevning'
  long readPulse = pulseIn(taosOutPin, LOW, 50000);
  readPulse += pulseIn(taosOutPin, LOW, 50000);
  readPulse += pulseIn(taosOutPin, LOW, 50000);
//  delay(100);
//  slå av sensoren
//  digitalWrite(S0, LOW); //power off chip. Ikke nødv å slå av chip'en - den bruker kun 1,5 mA
//  digitalWrite(S1, LOW); (er low allerede)
  return readPulse;
}

It might just be possible to detect movement. You would have to write a sketch that read the light level and spotted when the value changes suddenly. This would not truly be detecting movement, because it could be fooled by switching a light on, for example. Also it would probably not be very sensitive, and perhaps only movement near the sensor, or a shadow passing over the sensor would be detected. If you tried to make it more sensitive, by detecting very small changes in the value, it would probably just give false alarms all the time due to noise in the sensor.

My advice would be go buy a proper pir sensor!

Paul

Any light sensor (or heat sensor, or whatever) can be used as a motion sensor, however ...

The "trick" used by PIR sensors, is to use two adjacent sensors and not only that, but a lens which generates a pattern of stripes or "chequerboard" which alternates the sensitive areas so that overall changes are balanced out and only small relative variations are sensed.

In addition, the signal processing ignores single step changes, responding only to cyclic changes corresponding to movement over at least two successive boundaries.

This color sensor is really only useful to detect color at very short distances (<5 cm) and the closer it is the more accurate the detection. I don’t think that it is very useful for presence detection. There are many other options for distance/presence sensing that cold be easier to implement, like PIR for ‘general presence’ to the ultrasonic sensors for more targeted line of sight distance.

@amansnow, please do not cross-post. Threads merged.

Oh yeah I apologize for that! I was aiming for the sensors forum but I posted it on the wrong forum and I didn't know how to delete my post. :slight_smile:

Click Report to Moderator. Ask for the thread to be moved.

Good luck with your project.

knut_ny:
The long, but useful, story: LESSON 15: Arduino Color Sensor and RGB LED - YouTube

This video
uses this code.
Remove what you dont want (comments in Norwegian)

#include <Servo.h>   //Vi legger til biblioteket for servoene. 

Servo myservo;
Servo myservo1;      // 3x servoer blir definert
Servo myservo2;

int tom = 0;         // Teller om armen er tom.
int posGreen = 11;   // possisjon for grønne smarties
int posLilla = 28;   // possisjon for Lilla smarties
int posRosa = 40;    // possisjon for Rosa smarties
int posGul = 51;     // possisjon for Gule smarties
int posRed = 64;     // possisjon for Røde smarties
int posOrange = 77;  // possisjon for Oransje smarties
int posBrun = 90;    // possisjon for Brune smarties
int posBlue =107;    // possisjon for Blå smarties
int val, val2, val3, counter = 0;   // variabel for avlesning av farge

long sum;

unsigned long white,red,blue,green;
byte led=4, S0=5, S1=6, S2=7, S3=8, taosOutPin = 12;
#define farger 10  //antall farger vi skal detektere + Sort
// lagre alle data i en tabell, for så å sjekke aktuell måling mot match
int tbl[farger+1][8]=   // OBS: linje 0…farger kolonne 0…7
{
//  rød     grønn      blå     hvit       Om alle fire verdiene matcher så er fargen bestemt. Fargeverdier                                  // stammer fra sensor
 {85,100,  65,80,   70,90,   0,9999}, //grønn
 {50,70,   80,95,   100,110, 110,200}, //Lilla
 {60,75,   65,75,   110,130, 110,160}, //Rosa
 {100,140, 50,90,   65,99,   90,170}, //Gul
 {70,85,   40,60,   110,145, 160,9999}, //rød
 {90,105,  0,60,    95,135,  120,400}, //Orange
 {80,95,   45,80,   80,110,  180,400},   //Brun
 {50,70,   92,130,  80,99,   81,300},  //blå
 {0,9999,  0,9999,  0,9999,  400,9999}, //sort
};

// Navn for å lese av farge serielt under testing.
String navn[farger+1] ={“Gronn”,“Lilla”,“Rosa”,
               “Gul”,“roed”,“Orange”,“brown”,
               “blue”,“sort”};
bool match;
byte test; //teller

void setup() {
 Serial.begin(9600);    
 myservo.attach(9);    
 myservo1.attach(10);    //servoene settes på hver sin pinne
 myservo2.attach(11);
 pinMode(S0,OUTPUT);
 pinMode(S1,OUTPUT);
 pinMode(S2,OUTPUT);
 pinMode(S3,OUTPUT);
 pinMode(taosOutPin, INPUT); //data fra fargesensor
 pinMode(led,OUTPUT);
 // sett mode 2 (1:5 = 20%
 digitalWrite(S1, LOW);
 digitalWrite(S0, HIGH); // slå på sensoren
//  digitalWrite(led, HIGH);
 /* Mulighetene er:
  * mode 0 (avslått)
   digitalWrite(S0, LOW);
   digitalWrite(S1, LOW);
  * mode 1 (1:1)
   digitalWrite(S0, HIGH);
   digitalWrite(S1, HIGH);
  * mode 2 (1:5)
   digitalWrite(S0, HIGH);
   digitalWrite(S1, LOW);
  * mode 3 (1:50)
   digitalWrite(S0, LOW);
   digitalWrite(S1, HIGH);
 */
}

void loop()
{
   detectColor(taosOutPin);
/* Til debuggning:
   Serial.println(red);
   Serial.println(green);  
   Serial.println(blue);
   Serial.println(white);
   Serial.println("-----");    
*/    
   // bestem fargen
   for (test=0; test<=farger; test++) //gjenta for N farger
   {  // anta først at vi har match på ‘denne’ fargen
    match=true;
    if ( red  <tbl[test][0] || red  >tbl[test][1]) match=false;
    if ( green<tbl[test][2] || green>tbl[test][3]) match=false;
    if ( blue <tbl[test][4] || blue >tbl[test][5]) match=false;
    if ( white<tbl[test][6] || white>tbl[test][7]) match=false;
   // hvis ‘match’ fortsatt er true, så har vi fargen
    if (match==true) break; //fargen er funnet - hopp ut av løkka
   }
   
   // følgende kode setter armen i gitt posisjon avhengig
   // av hvordan nummer fra 1 til 9 som vi fikk ut av fargesensoren
    if (test<(farger-1))
    {
      Serial.println(navn[test]);
      Serial.println(test);
      if( test == 0){
        tom = 0;
        myservo2.write(posGreen);
        delay (300);
      }
      if( test == 1){
        tom = 0;
        myservo2.write(posLilla);
        delay (300);
      }
      if( test == 2){
        tom = 0;
        myservo2.write(posRosa);
        delay (300);
      }
      if( test == 3){
        tom = 0;
        myservo2.write(posGul);
        delay (300);
      }
      if( test == 4){
        tom = 0;
        myservo2.write(posRed);
        delay (300);
      }
      if( test == 5){
        tom = 0;
        myservo2.write(posOrange);
        delay (300);
      }
      if( test == 6){
        tom = 0;
        myservo2.write(posBrun);
        delay (300);
      }
      if( test == 7){
        tom = 0;
        myservo2.write(posBlue);
        delay (300);
      }
      if(test == 8){
        delay(300);
        tom++;
        if(tom > 5){    // Om armen er tom 5 ganger på rad
          delay(7000);  // antar vi røret er tomt. Da
        }               // bruker den 4x så lang tid på en sortering.
      }
     
     
     myservo.write(30);     //Mini: Hent smarties fra rør
     delay(300);
     myservo1.write(137);   //Arm: Stopp rett før kanten, slik at
     delay(500);            //     slik at den bremeser ned før neste.
     myservo1.write(145);   //Arm: Beveg over kanten, slipper smarties.
     delay(730);
     myservo1.write(34);    //Arm: Hent smarties ved mini servo.
     delay(500);
     myservo.write(103);    // Mini: Lever smarties til arm.
     delay(400);
     myservo1.write(58);    //Arm: Beveger seg til fargesensor
     delay(200);    
    }
}
//----------------------------------------------------

void detectColor(byte taosOutPin)
{
 myservo1.write(57);
 delay(350);
 digitalWrite(led,HIGH); // slå på strømmen til lysdiodene
 white = colorRead(taosOutPin,0);
 green = colorRead(taosOutPin,1);
 red = colorRead(taosOutPin,2);
 blue = colorRead(taosOutPin,3);
 digitalWrite(led ,LOW); // slå av strømmen til lysdiodene
 sum=green+red+blue;  //finn samlet luminans
 green=(green<<8)/sum;  //ganget med 256 for å få hver farge i områder 0…255
 red=(red<<8)/sum;
 blue=(blue<<8)/sum;
      Serial.print(“luminans:”); //Vi får fargene serielt for
      Serial.println(white);     //for å bruke dem til de-bugging.
      Serial.print(“green:”);
      Serial.println(green);
      Serial.print(“red:”);
      Serial.println(red);
      Serial.print(“blue:”);
      Serial.println(blue);
      Serial.println(" ");
}

long colorRead(int taosOutPin, int farge)
{
//  digitalWrite(S1, LOW);  // med mode 1:5 (er low allerede)
 int sensorDelay = 100;
 //sett S2 og S3 til riktig farge
 if(farge == 0){ //white
   digitalWrite(S3, LOW);
   digitalWrite(S2, HIGH);
 } else
 if(farge == 1){//red
   digitalWrite(S3, LOW);
   digitalWrite(S2, LOW);
 } else
 if(farge == 2){//blue
   digitalWrite(S3, HIGH);
   digitalWrite(S2, LOW);  
 }else
 if(farge == 3){//green
   digitalWrite(S3, HIGH);
   digitalWrite(S2, HIGH);
 }
 delay(sensorDelay); //gi sensor tid til å stille seg inn
 // mål pulsbredden minst 3 ganger for ‘utjevning’
 long readPulse = pulseIn(taosOutPin, LOW, 50000);
 readPulse += pulseIn(taosOutPin, LOW, 50000);
 readPulse += pulseIn(taosOutPin, LOW, 50000);
//  delay(100);
//  slå av sensoren
//  digitalWrite(S0, LOW); //power off chip. Ikke nødv å slå av chip’en - den bruker kun 1,5 mA
//  digitalWrite(S1, LOW); (er low allerede)
 return readPulse;
}

Thanks! Will definitely get back to you on this one :slight_smile:

Oh okay thank you! :slight_smile:

Hello, I have a quick question. In the manual of the color sensor there is a table that shows the OUTPUT FREQUENCY SCALING. I’m not exactly sure it’s what I think it means it but if there’s anyone who can explain it me, it would be very much appreciated.

The table is shown in the picture attached. The Table-1.

There is an article on my blog that explains how the sensor can be calibrated. Link below.

Hello, I have another quick question. Is it possible to use the smartphone as an LOS controller (or just imagine a remote controller) and be detected by the color sensor?