Read Color Sensor

The LaunchPad BoosterPack incorporates a color sensor. This sensor measures the amount of light received on each of four channels: Red, Green, Blue, and Clear. These sensor readings are available via I2C. This sensor is commonly used for measuring the color of a display's backlight or the color temperature of ambient light. This example reads the amount of ambient light on each of the four channels and outputs it to the console. It's a good demonstration of how to use a sensor to measure something useful.

Note that the sensor measures the color of incident light. If you wish to measure the color of an object then you would need to strongly illuminate the object with white light.

Once you compile and run the example, shine a light on the color sensor and note the change in values read. For even more fun, shine a colored LED at the sensor and see how the values change.



This is one of the few examples that uses a separate helper file to interface to the sensor. At the top of the file you see: which is the same for all examples. But there's a second file included here: This second file contains the code for interfacing with the color sensor. This is placed in a separate file for optimal code reuse and to make it easier to troubleshoot.
 * 1) include "../HAL/hal.h"
 * 1) include "../HAL/hal_AMS_TCS3414_color_sensor.h"

This demonstrates the "building block" nature of these examples: while this example isn't very useful, it serves as a demonstration of how the color sensor works and makes it easy to incorporate it into a larger application. If you're building a system that has its own sensors, I recommend making a small example like this for each sensor you create a driver for. It's a good way of verifying proper operation of the driver instead of only trying to test inside application code.

Under the Hood
The color sensor uses an I2C interface. This can be implemented in hardware or software (aka bit-bang). The MSP430 LaunchPad uses a software I2C interface since the I2C pins are in use by other interfaces. The Stellaris LaunchPad uses a hardware I2C interface.

The initialization procedure for the Color Sensor is in colorSensorInit in hal_AMS_TCS3414_color_sensor.c:
 * 1) Initialize the I2C interface. This calls i2cInit which may use a hardware or software interface.
 * 2) Turn the sensor on by writing the power on bit to the control register
 * 3) Set the integration time to 100mSec by writing to the timing register
 * 4) Read the ID register of the chip. Not required, but handy to verify.
 * 5) Turn on the Analog to Digital Converter inside the IC.

Reading the color is simple, just read the red, blue, green, and clear registers via I2C.

For more information, see Read Color Sensor Logic Analyzer Trace.

Next Example: Read Supply Voltage

Previous: I2C Test

Top: Module_Examples