Sensing color with the ADJD-S311 + Arduino
A year ago we had an article called Sensing color with the ADJD-S371. Well the ADJD-S371 is long gone, and its replacement is the ADJD-S311 (breakout board available from sparkfun). It is basically the same all around, so without shame, I will be copying a lot of that article, because… Why reinvent the wheel?
A few things you should know about the ADJD-S311 before we dive in too deep: I have never been able to get perfect color sampling from this guy without limiting the colors it would detect to just 6, and accurately reproducing color on an LED is not as simple as one would hope. The color reading from the sensor could be spot on, but the reproduced color on the LED may be way off. (you can blame your eyes and the LEDs)
Hooking it up
The ADJD-S311 is an I2C device (I2C is a 2-wire serial connection), so we just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the Mega) SDA is on analog pin 4, and SCL is on analog pin 5. On an Arduino Mega, SDA is digital 20, and SCL is digital 21. (The Arduino Leonardo will also be different). Other than these 2 lines, we just need to connect power (3.3v), ground, and the onboard LED to digital 2 (you can change that in code if you want).
The ADJD-S311 has 4 sensors built into it to detect Red, Green, Blue, and Clear. It reports back an individual reading from each sensor. The white, or clear, sensor is mainly for sensing brightness.
The ADJD-S311 is designed to sense reflected light using the onboard LED, but I have found that by turning the onboard LED off, it works even better for sensing projected light (like from your monitor or projector). If you are interested in using the sensor for sensing reflected colors, you want the sensor to be about 1-2mm from the subject so the onboard LED can bounce off the material and back to the sensor.
For more detail: Sensing color with the ADJD-S311 + Arduino