Sensor Workshop: Sensor Report!!!9
DIY Color Sensor Using RGB LEDs and Photo Cell
From the beginning I of the semester I decided I would do my Sensor Report on either a color sensor or some kind of homemade sensor which had not yet been well documented. I ultimately ended up doing both.
I've had plans for a project involving color sensors since Winter Break, when some friends told me I should do a big interactive art piece using their entire loft space. I've done some other projects using flashlights and Cadmium Sulfide cells to control sound, and wanting to go further with this idea I figured an interesting way to add to it would be to control sound not only using light, but using the color of the lights for added variety (I imagine handing out flashlights with various colored gels on them. Yesterday someone suggested I somehow mount the flashlights on people's heads. Ha!). I didn't manage to work this into any of my classes so I'm planning on working on it over the Summer. I'll be sure to document the whole process on my blog.
I looked online and found some small amount of info on using RGB LEDs and photo cells, but all were written by people with significant tech know-how, and there was nothing which layed it out really thoroughly for people like us who want to be able to just plug everything in and copy some Arduino/Processing code and have a functioning sensor ready to go. So here it is.
Parts:
What you'll need:
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 Photocell
Obviously 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
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!!
Programming Arduino and Processing:
Arduino code here
Processing data visualization code here
How it works:
Both the idea and the implementation are pretty simple. The idea is that when an LED of one color is switched on, an object placed in front of it will reflect back some amount of that color based on how much of that color the object contains. Thus by pointing a photo cell at this reflected light you can measure how much of that color the object has. Do this 3 times with red, green, and blue light and in theory you should be able to put the values together to get a color close to that of the object you're sensing.
But first....
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. See what values you get!
The sensor works best with objects which are NOT shiny. Foam and cloth work the best.
Right now it's far from perfect, but if you put a red, green, or blue object in front of it it will return roughly that color. Colors which are an RGB mixture are not incredibly accurate.
Uses:
1) Mobile appliances and consumer electronics
Automatic light adjustment for cell phones, displays, and such.