These are instructions for making your own color sensor based around an RGB LED and a Photo Cell.

This color sensor is very easy to build and use. It is not as fast or as accurate as many commericially available color sensors but works reliably and consistently for many applications.

A Brief Explanation of How It Works

The sensor essentially consists of an RGB LED and and a photo cell placed right next to each other with a small divider between them so that the light of the LED does not directly hit the photo cell. An object is placed a short distance (about 1-2 inches with the components I'm using, though brighter LEDs could likely change this range of distance) from the sensor. The red, green, and blue lights on the LED are switched on one at a time. If the object's color contains any amount of red, green, or blue, that color is reflected back onto the photo cell when the corresponding light is on and read into Arduino as an analog value. The three colors are then mixed into one, the color of the object.

Parts



1) 1 Adequately bright RGB LED, or adeuqately bright individual red, green, and blue LEDs
If the LEDs are not bright enough they will not reflect enough colored light off the sensed object and you won't get good values. Whereas the RGB LEDs Sparkfun sells are bright enough to work, the RGB LEDs I got from China on eBay are not more than half the brightness of the Sparkfun ones and don't work so well. The Sparkfun ones have luminosities of 800 mcd (millicandelas, which are I believe thousandths of a footcandle) for the red, 4000 mcd for the green, and 900 for the blue, so its safe to say anything at or over 800mcds will give you a good reading.

2) 1 Photo Cell
The stronger the photocell the better because it will give you a broader range of values.

3) 3 Switches or buttons
These aren't actually necessary but I used 2 buttons and 1 toggle switch. The buttons are for setting the white balance and black balance (more on that in a bit) and the switch is just to tell Arduino to run the color sensing function.

4) A piece of black foam
For black balancing. It doesn't have to be foam, just anything that's black and not particularly shiny or reflective.

5) A piece of white paper of foam board
For white balancing

6) An Arduino

7) Some resistors and wires

8) A small black box with a hole in it
Not absolutely necessary, but you'll get better results from the sensor if you block out ambient light with a black box.

Putting It Together



1) Plug your photocell and RGB LED into your breadboard right next to each other so the tops of both are about vertically aligned.

2) Wrap the circumfrence of the photo cell in black electrical tape so that it reaches a little bit above the top of the photo cell so that it will block direct light coming from the LEDs.

3) Plug everything into the appropriate pins in Arduino. Photo cell into analog pin 2. Red, green, and blue pins of the LED into digital 4, 5, and 6. White and black balance into digital 2 and 3, and sensor on/off switch into pin 7. Remember your resisitors to ground for the photo cell and the switches!!

Code

Arduino Code

int greenPin = 2;
int bluePin = 3;
int redPin = 4;
int whiteBalancePin = 5;
int blackBalancePin = 6;
int sensingPin = 7;
boolean whiteBalanceSet = false;
boolean blackBalanceSet = false;
int greenVal = 0;
int blueVal = 0;
int redVal = 0;
int ledPinsArray[] = {

  redPin, greenPin, bluePin};

float colorValArray[] = {

  0, 0, 0};

float whiteBalanceArray[] = {

  0,0,0};

float blackBalanceArray[] = { 0,0,0};

void setup() {

  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(whiteBalancePin, INPUT);
  pinMode(blackBalancePin, INPUT);
  pinMode(sensingPin, INPUT);
  Serial.begin(9600);

}

void loop() {

  checkWhiteBalance();
  checkBlackBalance();
  checkColorVals();

}

void checkColorVals() {

  if(blackBalanceSet && whiteBalanceSet && digitalRead(sensingPin) == HIGH 
  && digitalRead(whiteBalancePin == LOW) && digitalRead(blackBalancePin) == LOW) {
  for(int i=0;i<=2;i++) {
    digitalWrite(ledPinsArray[i], HIGH);
    delay(100);
    colorValArray[i] = analogRead(2);
    colorValArray[i] = (colorValArray[i] - blackBalanceArray[i])/(whiteBalanceArray[i]-blackBalanceArray[i]) * 255;
    digitalWrite(ledPinsArray[i], LOW);
    delay(50);
  }
  printColorVals();
  }

}

void checkBlackBalance() {

  if (digitalRead(blackBalancePin) == HIGH) {      
    for(int i=0;i<=2;i++) {
      digitalWrite(ledPinsArray[i], HIGH);
      delay(100);
      blackBalanceArray[i] = analogRead(2);
      digitalWrite(ledPinsArray[i], LOW);
      delay(50);
    }
    blackBalanceSet = true;    
  }

}

void checkWhiteBalance() {

  if (digitalRead(whiteBalancePin) == HIGH) {
    for(int i=0;i<=2;i++) {
      digitalWrite(ledPinsArray[i], HIGH);
      delay(100);
      whiteBalanceArray[i] = analogRead(2);
      digitalWrite(ledPinsArray[i], LOW);
      delay(50);
    }
    whiteBalanceSet = true;
  }

}

void printColorVals() {

  Serial.print("R");
  Serial.println(colorValArray[0], DEC);  
  Serial.print("G");
  Serial.println(colorValArray[1], DEC);
  Serial.print("B");
  Serial.println(colorValArray[2], DEC);

}

Data Visualization in Processing Code

import processing.serial.*;

PFont font;
String buff = "";
int val = 0;
int NEWLINE = 10;
int xPos,yPos,zPos = 0;
int displaySize = 2;
int an1, an2, an3;
int colorChange = 0;

Serial port;

void setup(){

  font = loadFont("AlbaMatter-48.vlw");
  background(80);
  size(900,600);
  smooth();

  port = new Serial(this, "COM20", 9600); //remember to replace COM20 with the appropriate serial port on your computer

}

void draw(){

  fill(0,2);
  noStroke();
  rect(0,0,width,height);

  // wipe out a small area in front of the new data


  // check for serial, and process
  while (port.available() > 0) {
    serialEvent(port.read());
  }

}

void serialEvent(int serial) {

  if(serial != '\n') { 
    buff += char(serial);
  } 
  else {
    int curX = buff.indexOf("R");
    int curY = buff.indexOf("G");
    int curZ = buff.indexOf("B");

    if(curX >=0){
      String val = buff.substring(curX+1);
      an1 = Integer.parseInt(val.trim());

      xPos++;
      if(xPos > width) xPos = 0;
      fill(0);
      noStroke();

    }    
    if(curY >=0){
      String val = buff.substring(curY+1);
      an2 = Integer.parseInt(val.trim());

      yPos++;
      if(yPos > width) yPos = 0;

    }
    if(curZ >=0){
      String val = buff.substring(curZ+1);
      an3 = Integer.parseInt(val.trim());

      zPos++;
      if(zPos > width) zPos = 0;
      fill(0);
      rect(xPos+20,0,250,height);       
      sensorTic(xPos,an1 + 50,"red "+an1,255,0,0,255,0,0);
      sensorTic(yPos,an2 + 50,"green "+an2,0,255,0,0,255,0);      
      sensorTic(zPos,an3 + 50,"blue "+an3,0,0,255,0,0,255);     

    }

    buff = "";
  }
  fill(an1, an2, an3);
  rect(0,500,900,100);

}

void sensorTic(int x, int y, String txt, int sr, int sg, int sb, int fr, int fg, int fb){

  stroke(sr,sg,sb);
  fill(fr,fg,fb);
  ellipse(x,y,displaySize,displaySize);
  textFont(font);
  textSize(40);
  text(txt,x+25,y); 

}

White and Black Balancing and Normalizing the Values



Before we start sensing we're going to need to find out what the minimum and maximum values we get from the photo cell when each LED is on so that we can normalize all of them. I built some code into the Arduino program to do this, and this is why we need buttons for setting white and black balancing as mentioned before. In the code it takes whatever the minimum and maximum are from each and makes it so any values between them will go from 0 to 255, making it as easy as possible to visualize the data in Processing.

To do this:

1) Hold your white piece of paper or foam board about an inch above the photo cell and LED, being sure its completely covering both of them.

2) Hit the White Balance button. The red, green, and blue LEDs should quickly blink one after another. When this happens the values have been recorded. These are the maximum values the photo cell can reach for each color, because nothing can reflect more red, green, and blue light than something pure white.

3) Do the same thing with the Black Balance button and the piece of black foam. This gives us our lowest possible values.

Color Sensing

Stick the black box on top of the color sensor. Open the Processing app and set the white and black balance as described above. If it isn't already on, switch on the Sensing switch. Values should start coming into the Processing app. Put an object about an inch above the photo cell so the light from the LEDs hit it.