Let's Make Robots!

Color Sensor Algorithm


I know the video quality sucks I just wanted to give a view of  what it is doing. The black tube has the LDR in it to hide stray light. Below is a tabl eof the values I got for Red, Blue and Green when different color soda caps were put in front of the LDR. Now I need to take the raw values and put them into some sort of algorithm that looks at the difference between the values before a cap is put in place and after to determine the cap color.



LED Color                            
red LED 68 88 93 142 126 188 126 157 77 62 55 182 146 130
blue LED 67 83 106 118 101 125 89 134 111 83 74 184 150 112
green LED 68 117 122 115 98 153 90 119 94 57 64 190 150 122
Cap Color ambient light green dark green red dark red yellow orange pink blue dark blue dark teal white clear gold




I swapped out the 10k resistor on the LDR to a 1k I think it made the values way too low as blue and green always stay fairly low. However red is very high. I think I need to change the 1k resistor to a 5k and take the resistor on the red LED and swap it for a higher one to make the light less intense. More to come!

 Update 6/8/09:

Not much to update but I replaced all of the resistors with pots to try to dial in the appropriate resistor values. Its funny how wacky the behavior gets. At one point when the blue LED came on I could see the reflection on the LDR, but yet the values dropped. CRAZY! 

Update 6/8/09:

Got values from the pots transferred to resistors and I am getting fairly consistent values. Now to make an algorithm that takes ambient light then the values with the cap in place and determines the color. More to come tomorrow hopefully...


Update 6/17/09

Got some 100ohm, 500ohm and 2kohm pots to better dial in the resistance values. I also updated the chart with the values I get with each color. Next is to solder the sensor to perf board with the resistor values of:  2.83k for the LDR, 448ohm for the red LED, 8ohm for the blue LED, and 66ohm for the green LED.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Here's aq good write up i=on making your own color sensor.


This is great. I haven't had time to make my colour sensor yet but now I know to add some trimpots for calibration. That hadn't occured to me. Thanks for saving me some debauchery (desoldering and butchering).

Hi jklug,

I think you should have a look at the mahalanobis distance. It's a bit mathematical for me to be explaining it in detail here, so google or wiki it.

Basically it works like this: you start off by collecting some sample data for each type of cap and then the algorithm generates coordinate systems for each type where the R,G,B axes units correspond to the variation of that particular color component in the sample set for that type. When determining what type an unknown R,G,B triple is it will measure distance to each of the sample types using those generated coordinate systems. I know this is not the best explanation in the world, but I tell you this technique is quite usefull.

- Jimmy

I just skimmed over the wikipedia on that distance thingey. Woosh! Over my head. And I suspect over Picaxe's head as well. But I agree that this algorithm is exactly what is required to solve this colour matching game.

Mahalanobis Picaxe library writers, report on this node!

Once all input data is there, you still need to interpret it. In my LDR room recognition problem (I can hardly call it a solution), I propose to create fingerprints out of multiple readings. Then use each fingerprint as a memory location in a lookup table. That may be overkill for this purpose.

The alternative however is a huge (if/then/else/endif)^13 program. Like

if red > 30 and red < 50  then
   if green < 100 and blue > 200 then
      colour = yellowish
   if green > 100 and green < 180 then
      colour = maroonesque

but much longer. Either way, you are looking for a pattern recognition algorythm.

Electronics: start by dimming your red LED to equalize the readings between the three colours for white objects. That way, you can normalize all of the voltages (roughly) to digitized values of 240 or somewhere near that value. You may have to fiddle with the resistor value in series with the LDR. In my "telescope" experiment I used a 20 turn pot of 250 kOhm. But I found out that I ended up turning it all the way below 5 kOhm. I might as well have used a 25 kOhm pot, that I also bought.

Then do some trials on repeatability of your reading. How consistent are the measured voltages? On a white object first, then a red, a green and a blue one. Hopefully, you will see nice clear distinct values for every different colour object, but most of all: hopefully you will get very little variation between identical experiments. For example reading a white object should give you RGB values of 240,240,240 ideally. Plus/minus 10. Every time.

Then a blue object should get readings like 20,20,240 +/- 10. If you cannot make the voltages distinct and repeatable, then no algorythm in the world will be able to compensate for that. GIGO: garbage in = garbage out.

But hey, maybe you don't need all three colours to make the distinction. Judging from your red readings above, you seem to have a pretty decent detector already. It's not exactly using colour to tell the different caps from each other, just the reflectiveness (in the part of the spectrum that happens to be reddish, perhaps even infra reddish, how would you know without trying). But as your readings demonstrate, reflectiveness is discriminating between different bottle caps quite well.Six caps (out of 13) could be detected by these red-numbers alone when you insisted on the accuracy of 10 out 255.

Have you considered a Taos color sensor?
Good call robologist! Btw, I posted this on a taos sensor that can be purchased over at radio shack. I think you can differentiate between colors as well with this sesnor...I'd look at the data sheet as it's been a while.

The Radio Shack sensor may not do what you and I thought. It is carefully worded in the datasheet to be a "light sensor" where TAOS does make a frequency output "color sensor". The TSL230 seems to detect light only, where the TCS230 does respond to coolor depending on how it is configured. The configuration seems to require a number of parts detailed in the Parallx verions that costs $66. I think the sensor module could actually be built for a lot less, maybe $15 worth of parts. 

Mouser has the light version for $2.29 and the color version for $4.19 (in surface mount). 

I know this is a late reply but could be handy for anyone reading it later on. There's really no such thing as colour but what our eyes percieve as colour. To a robot all colour is, is different frequencies of light... so in escense all you need is a light sensor... the better the quality the better results... there's a variety of ways to get better results... ie the method used here with a RBG Led and a ldr or you could use 3 ldr's with 3 different coloured transparent film over them... or you could use 1 rbg led in reverse polarity to detect the colours... anyway... just my thoughts :)