On Mon, 28 Sep 1998, Mark A Winters wrote: > Does anyone know of a reliable (and inexpensive) method of detecting color > for input to a PIC? I need to be able to discriminate between small objects > 1" to 2" in diameter which can be any one of four or five different colors. > Ideally, I'd like to also be able to tell what color the object is > regardless of how "shiny" or "dull" it may be. The previous posters have pointed you in the right direction, but I'd like to add a few details: 1. color filters are expensive. Therefore the third color filter can be missing, giving full luminance information. The third color (usually green) is obtained by matrixing the luminance (Y) and the red and blue signals (R and B), i.e. G=Y-0.3*R-0.3*B (you can use other coefficients, f.ex. those used in NTSC TV). 2. If you can afford to control the light intensity in real time then you will likely get away with much cheaper sensors. In particular, the Y signal can be used to generate feedback. This removes most gamma and linearity related problems that someone else has pointed out. Getting rid of ambient light is important in this case. A solid state lamp looks good for this (many white leds in a box - available from led makers - e.g. from Mouser I think), as it should not shift its color while being controlled in intensity. For a large (mains powered) light you need a mechanical iris or linear shutter to achieve this (expensive and fragile). Inexpensive CdS photocells should be usable for this, followed by a simple DC amplifier. It all depends on how accurate you want to get. Try to determine the required color contrast to discern your colors by looking at a video (taped ?) picture of the samples (all together) in a vectroscope or color analyzer. If the dots are far apart, it's easy, else, you will spend money ;). A vector scope should be available at least in a better VCR repair shop or video (editing ?) studio near you. Note that it is important to get all the sensors to read the incoming light coming from the same angle. Patterned/ribbed surfaces can give a lot of trouble otherwise. 3. To detect a color with such a setup, notice that Y >= {R,B} always (due to lack of filtering on Y). This allows for a deliberate gain reduction in the Y channel, to improve S/N on R and B. Then, you scale the 3 signals after obtaining G: G = Y - 0.3*R - 0.3*B (Note: 0.3 includes the filter attenuation ;) BASE = MIN(R,G,B) SCALE = MAX(R,G,B) - SCALE R1 = ((0.3*R - BASE) * MAXCOUNT) / SCALE G1 = ((G - BASE) * MAXCOUNT) / SCALE B1 = ((0.3*B - BASE) * MAXCOUNT) / SCALE (Note: MAXCOUNT is an arbitrary positive integer chosen to give sufficient resolution for your color selection in the discrimination table. For your numbers, 3 bits per color may be enough - or not - see #5) 4. Match R1,G1,B1 against a 3d range-table (cloud table ? - how do you call such a thing - a 3d cartesian range-plot). This is large and I don't know how you can fit it into a PIC, unless you do some serious optimization for the calculus to yield a strict range. 5. If you restrict the input colors to the 3 primary colors and their 3 complements you will have a relatively easy job at 4, there being a formula that can replace the table. There are also formulas for other color combinations. hope this helps, and sorry for the longish post. Peter