1

I'm trying to read some values from a TCS3200 colorsensor using Arduino Yun. I have some code, which you can see below, that I used when I tested on the Uno. With the Uno everything worked fine and I received some good values from the TCS3200 using Serial monitor. I also tested the code on the Nano. With the Nano I had to lead currency from a 9v battery directly to the TCS3200 through a voltage regulator, since the Nano didn't supply enough currency for the TCS3200. But it all worked fine.

Now I'm trying the same thing with the Yun, but doesn't get the right values? I tried to read about the serial-usb on Yun, which I understand could be used, but I might be wrong?

This is the code I'm testing:

#include <SoftwareSerial.h>
#include <TimerOne.h>

#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2
// OE is ground

int   g_count = 0;    // count the frequecy
int   g_array[3];     // store the RGB value
int   g_flag = 0;     // filter of RGB queue
float g_SF[3];        // save the RGB Scale factor

// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);

  digitalWrite(S0, HIGH);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, LOW); 
}

// Select the filter color 
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;

  if(Level02 != 0)
    Level02 = HIGH;

  digitalWrite(S2, Level01); 
  digitalWrite(S3, Level02); 
}

void TSC_Count()
{
  g_count ++ ;
}

void TSC_Callback()
{
  switch(g_flag)
  {
    case 0: 
         //Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
         //Serial.print("->Frequency R=");
         //Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
         //Serial.print("->Frequency G=");
         //Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;

    case 3:
/*       Serial.print("->Frequency B=");
         Serial.println(g_count);
         Serial.println("->WB End");*/
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)   
         break;
   default:
         g_count = 0;
         break;
  }
}

void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(100000);             // set 1s period
}

void setup()
{
  TSC_Init();
  Serial.begin(9600);
  while  (!Serial) {;} // Dont run serial monitor, unless there is a connection
  Serial.println("Calibrating.. Please wait..");  
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback);  
  attachInterrupt(0, TSC_Count, RISING);  

  delay(4000);


  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor
  Serial.println("Scaling factors:");     
  Serial.print("R: ");   
  Serial.println(g_SF[0]*1000);
  Serial.print("G: ");   
  Serial.println(g_SF[1]*1000);
  Serial.print("B: ");   
  Serial.println(g_SF[2]*1000);
  Serial.println("Calibrating done..");  
  /*
  g_SF[0] = 0.03405;     //R Scale factor
  g_SF[1] = 0.03488;    //G Scale factor
  g_SF[2] = 0.02727;    //B Scale factor */
}

void loop()
{
  int r = int(g_array[0] * g_SF[0]);
  int g = int(g_array[1] * g_SF[1]);  
  int b = int(g_array[2] * g_SF[2]);
  g_flag = 0;

  Serial.println("--- New values ---");     
  Serial.print("R: ");
  Serial.println(r);  
  Serial.print("G: ");
  Serial.println(g);    
  Serial.print("B: ");  
  Serial.println(b);  

  delay(2000); 
} 

I don't want to use wifi, at least at the moment. This is how I setup the whole thing: enter image description here

The sketch is from the Nano, but the whole setup is the same on the Yun. The VCC/GND in the upper left corner is the 9V battery.

migrated from electronics.stackexchange.com Mar 17 '15 at 17:48

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • You've likely done this, but their IDE has a menu for selecting which board to compile the code for. Also, I must say your code presented above is some of the nicest I have seen coming from the Arduino community, specifically referring to the formatting. As a final note about the code, I see you included some printing statements inside the ISR. I have myself done this to see if my interrupt routine was even running, but then removed it after verification. It is okay for quick tests like this, but be aware that multiple-byte transfers through interrupts will miss transfers and fail. – sherrellbc Mar 17 '15 at 15:07
  • Regarding the problem though, what exactly is happening? You are just reading incorrect values is all? Also, perhaps you simply made a mistake drawing the diagram, but LED1, LED2, and LED3 will never turn on in the configuration you have draw; did you mean to have Vcc where you have drawn GND? – sherrellbc Mar 17 '15 at 15:10
  • I did chose the right board and also serial. I have to say that the code is for the biggest part from this wiki page elecfreaks.com/wiki/index.php?title=Color_Sensor_Module – Tommy Otzen Mar 17 '15 at 15:11
  • I'm actualle not using the LEDs at the moment, the sketch is only showing the setup for the board and the tcs3200. The output O get from the serial is r000, g255 and b000. But their is no reaction if I change the color the tcs3200 reads. It does read the values right on both the uno and nano. – Tommy Otzen Mar 17 '15 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.