Color Sensor From An RGB LED And A Photocell

When you need to quantify the color of an object, you’ve got quite a few options. You can throw a Raspberry Pi camera and OpenCV at the problem and approach it through software, or you can buy an off-the-shelf RGB sensor and wire it up to an Arduino. Or you can go back to basics and build this reflective RGB sensor from an LED and a photocell.

The principle behind [TechMartian]’s approach is simplicity itself: shine different colored lights on an object and measure how much light it reflects. If you know the red, green, and blue components of the light that correspond to maximum reflectance, then you know the color of the object. Their sensor uses a four-lead RGB LED, but we suppose a Neopixel could be used as well. The photosensor is a simple cadmium sulfide cell, which measures the intensity of light bouncing back from an object as an Arduino drives the LED through all possible colors with PWM signals. The sensor needs to be white balanced before use but seems to give sensible results in the video below. One imagines that a microcontroller-free design would be possible too, with 555s sweeping the PWN signals and op-amps taking care of detection.

And what’s the natural endpoint for a good RGB sensor? A candy sorter, or course, of which we have many examples, from the sleek and polished to the slightly more hackish.

27 thoughts on “Color Sensor From An RGB LED And A Photocell

  1. Direct light isolation between led and ldr is mandatory if you expect to have usable results, otherwise the led will generate too much noise in measurements.
    Didi it a few years ago, works like a charm, but if you want to be more precise, pre-calibrated sensors exist and are quite cheap (around $0.50 on ali), and equipped with 4 smd rgb leds and a color sensor.

  2. I think the leds are just off/on, no pwm. I thought the pwm might be used to correct for sensor response curve. Reminds me to get back to that alcohol abv sensor I was making using specific wavelength light.

  3. >”If you know the red, green, and blue components of the light that correspond to maximum reflectance, then you know the color of the object. ”

    No you don’t. The color of the object is the shape of its spectrum, and the RGB light reveals only three points of it, especially with narrow spectrum lightsources such as LEDs.

    Real objects are not RGB. It is very easy to trick such a meter to report false colors, for example with a yellow pigment that is not very reflective in either red or green. Under natural light, an RGB sensor such as the eye or a camera would percieve it as yellow, but under resticted spectrum that does not contain yellow, it appears blue, thereby the sensor reports a false color.

    1. If you want a true measurement, you need to use an incandecent light, split the result into a rainbow with a prism or a diffraction grating, sweep it across your sample while taking measurements, and then normalize according to the spectra of both your lightsource and your sensor.

      Then you can find the nearest RGB value that would produce the same impression under some lighting condition.

      1. Not necessarily, you could do with a high-CRI light source and three detectors mimicking the spectral sensitivity of the cones in the human eye. This could be achieved with optical filters.

        1. The problem with “high CRI” sources is that they often include various strong spikes and discontinuities in the spectrum, so while the color may appear correct to the human eye which has limited ability to distinguish individual wavelenghts and integrates over only three rather vaguely defined colors, it will mess up a sensor which sweeps through the whole spectrum in a narrow band.

  4. I wonder if it would be possible/practical to use the RGB LED as the sensor also. LEDs will produce a small voltage when lit, but I suppose that doesn’t necessarily mean that an RGB LED will be good at detecting those three colors. Something to think about, anyway.

    1. I did a quick study a few years ago with a bunch of different LEDs. It seems like some LEDs are sensitive to the wavelength they emit, but most are sensitive to wavelengths shorter (bluer) than they emit.

      Probably could use one of those RGB 4-lead LEDs as a way of directly measuring blue and green reflectance… makes me wish I had one right now so that I could try.

    2. Some LEDs have integrated phosphors which mess up your measurement.

      The junction works as a solar panel, which means it produces a constant voltage that corresponds to the bandgap, but also depends on the individual part and the current you’re drawing to measure it, and the number of photons falling on the junction, and the junction temperature, its internal leakage etc. which all makes it rather complicated to measure.

    1. Apparently my first comment was eaten because I foolishly tried to link to StackExchange for a response spectrum of a CdS cell…

      CdS cells respond weakly to colors other than yellow. Not very suitable for blue, UV, or IR reflectance measurements.

  5. This was almost the first thing I ever did when I first got my hands on an RGB LED more than 10 years ago. The results were atrociously bad.

    A TCS3200 color sensor is a much better choice, and you can buy a nice assemble module for about $2 from our friends in Shenzhen. And before you say the point here is DIY, DIY doesn’t mean 20 times the effort for 1% the performance.

  6. @Soc Rat,

    You’ve got it all backwards. Try living in a developing country like the vast majority of the people on the planet do. With a highly inefficient and corrupt Government-run postal and customs system, procuring a specialized part like the TCS3200 is a huge problem compared with using much easier to obtain/salvage components like LEDs and an LDR. For-example: Cheap light triggered lamp sockets are ubiquitous where I live and contain a plethora of salvageable parts like the LDR, a bridge rectifier, an op-amp, a triac, multiple diodes, resistors capacitors and other components. And I didn’t even get into the problems of actually ordering and paying for imported parts! The likes of PayPal and credit card companies geo-jail many users in the developing world due to fear of fraud when it comes to online payment.

    If you live in the first-world and want to think Globally when it comes to realizing DIY projects, think outside of your “privileged” bubble when choosing components. Do more with less – isn’t that what Engineering is all about in the end?

Leave a Reply to MaxCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.