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.
26 thoughts on “Color Sensor From An RGB LED And A Photocell”
Nice idea, can be extended to all sorts of survey devices & in conjunction with
various chemicals as well for a few material testing in remote locations…
Thanks for the posting,
“…sweeping the PWN signals…”
I think PWN signal rules to all kinds od pulse modulation signals.
A 555 timer pwning the Arduino.
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.
got a link for those? i haven’t found any that cheap.
My bad, prices seem to have raised since.
I used a tcs3200 : https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20170829060634&SearchText=tcs3200
I’m guessing it would it be faster if it uses a photodiode instead of the photoresistor
The spectral response curve of a photoresistor matches that of the human eye much more closely though, which may simplify calibration, I guess.
…and you can eat a photodiode, but not a CdS cell, so it is legal to sell the photodiode in Europe.
Yup also photocells are hard to get now due to rohs. . Better to use the cd free variant that uses zinc selenite or copper oxide etc
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.
>”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.
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.
Considering how small incandescent lights can be (almost as small as an LED) this should be quite possible to do.
Tiny, yes, cheap, no.
The issue is making the tiny diffraction grating, and then having to either tilt a mirror, or use a linear ccd to be able to detect the different wavelengths.
Not saying you can’t DIY something less expensive of course, but it’s hard keeping it small.
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.
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.
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.
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.
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.
Add an IR and a UV LED, and you can use it for a simple chromaspectrometer.
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.
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.
Flourescence doesn’t just happen under UV light, any wavelength with more energy than the color can potentially trigger it.
See this blog post for example. http://www.howtospotapsychopath.com/2012/07/26/green-in-red-out/
It might work for color identification, but I wouldn’t be sure.
This still isn’t as bad as people trying to build color calibration systems that just use RGB though.
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?
Please be kind and respectful to help make the comments section excellent. (Comment Policy)