Build a Repeatable Colour Sorter: Lighting, RGBC Readings and Classification Tests
A colour sensor can recognise different objects on a bench and then become unreliable when someone opens a curtain. The electronics may be working perfectly: the light reaching the sensor has changed. A dependable colour sorter therefore begins with the optical arrangement and a clear definition of the objects it must distinguish.
The APDS9960 proximity, light, RGB and gesture module is one example in PMD Way's colour sensor collection. This guide concentrates on its colour measurements rather than hand gestures, using a small educational sorting experiment as the design problem.
Decide whether you are measuring an object or a light
A coloured object reflects some of the illumination falling on it. A lamp produces its own light. These require different test arrangements. For sorting plastic counters, the illumination source, object surface and viewing geometry must all remain controlled.
Konica Minolta's explanation of object colour and source colour describes the distinction and the importance of geometry. A red counter under one lamp need not produce the same sensor ratios under another. Glossy surfaces also make reflections and viewing angle relevant.
Define the sorting task narrowly at first: perhaps four colours of counters from one material family. Do not start with an unrestricted promise to identify every red object. Transparent parts, fluorescent materials, metallic finishes and printed patterns introduce different optical behaviour.
Build a stable optical fixture
Hold the object at a repeatable distance and orientation. Use a simple guide or pocket so every counter covers the same sensing area. Keep the illumination source fixed relative to the object and avoid a direct reflection of the lamp into the detector.
A small opaque enclosure can reduce changing room light, provided its inside surfaces do not introduce unwanted reflections. Test the enclosure with and without an object. If the empty holder resembles a valid colour, the application needs a separate object-present condition or an explicit empty classification.
Use a stable visible light source with a spectrum suitable for the samples. The APDS-9960's internal infrared emitter serves proximity and gesture functions; it is not a general white illuminator for reflected-colour sorting. Start with colour sensing alone so other operating modes do not complicate the baseline.
Make the fixture easy to clean and inspect. Dust on a window, a displaced lamp or a worn guide can change the measurement without any firmware change. Photograph the original arrangement and mark mechanical reference positions.
Check the board and acquire valid RGBC data
The Broadcom APDS-9960 product information identifies red, green, blue and clear channels and specifies a 2.4–3.6 V supply for the chip. The store's broader module supply claim depends on its supporting circuitry. Confirm the actual regulator and logic-level arrangement, including I2C pull-ups, before connecting a host.
Begin with a minimal acquisition program from a known library, such as the SparkFun APDS-9960 library linked by the listing. Record the library version, gain, integration setting and enabled sensor functions. Check initialisation and read failures instead of treating every returned number as a valid sample.
The manufacturer datasheet describes 16-bit RGBC results with adjustable gain and integration time. Longer integration and higher gain can help weak signals, but can also saturate brighter samples. The maximum valid count depends on integration time; do not assume clipping occurs only at 65,535.
Choose settings using the darkest and brightest intended samples. Leave headroom for normal variation and reject readings that are too dark or saturated. After changing settings, wait for a valid conversion produced under the new configuration before comparing results.
Keep raw readings before applying a label
Log the four channels alongside sample identity and acquisition settings. Take repeated measurements after removing and replacing each object, rather than collecting hundreds of readings without disturbing it. Replacement tests include the positioning variation that a real sorter will experience.
A useful record includes the minimum, maximum and typical reading for each class. Keep an example of an empty holder and one deliberately awkward sample. These reveal whether the apparent separation survives realistic conditions.
Try simple normalised features
For a basic experiment, calculate r = R / (R + G + B), with equivalent expressions for green and blue. Check that the denominator is safely above the dark-noise region before dividing. These ratios reduce sensitivity to an overall brightness change, but they do not correct a changed illumination spectrum, saturation or surface reflections.
For example, readings of R = 600, G = 300 and B = 100 produce ratios of 0.6, 0.3 and 0.1. Doubling all three readings leaves those ratios unchanged. Adding a strong blue light does not. This arithmetic example explains both the benefit and the limitation of normalisation.
Retain brightness information as another feature rather than discarding it completely. Two objects may have similar channel ratios but very different reflected intensity. The clear channel can provide additional evidence, although it is not simply a calibrated substitute for the sum of RGB.
Create classes with an unknown result
Plot or tabulate the feature ranges for each sample group. If red counters form a distinct cluster, a simple range rule may be sufficient. If clusters overlap, changing the fixture or illumination may be more productive than adding elaborate software.
Allow an unknown result whenever a sample lies outside the trained ranges or near an ambiguous boundary. A sorter that confidently assigns every object to the closest colour can conceal poor measurement quality. Route uncertain objects to a review tray in an educational mechanism.
Keep the measurement decision separate from the actuator command. Confirm that one object has been classified before moving the gate, and avoid repeatedly actuating while the same object remains in view. Log the chosen class and validity reason so a wrong physical placement can be traced back to either sensing or mechanics.
Test with samples that were not used for calibration
Reserve some counters from each class for evaluation. Run them in mixed order, record the true and predicted classes, and include unknown objects. Report correct classifications, mistaken classifications and rejected readings separately.
Repeat after a restart, with the enclosure installed and under the expected room-light conditions. Test modest variations in placement and lamp warm-up. If performance deteriorates, compare the raw channels before adjusting thresholds; the underlying cause may be optical rather than computational.
Finally, keep a small set of reference samples for routine checks. Store the optical arrangement, settings, feature definitions and acceptance results together. This makes a low-cost colour sensor useful for a specific repeatable task without pretending that raw RGB counts are a universal colour measurement.