3.2. Using the Light and Color Sensor

In this short section, we get to see how one can read data off of the Light and Color Sensor without having to fine-tune the sensor or to deal with hard-to-understand concepts. Before anything else, connect the Light and Color Sensor to an I2C port on whichever platform (be it a GoPiGo3, GrovePi or a BrickPi3) and then run the following script.

The source file for this example program can be found here on github.

from time import sleep
from di_sensors.easy_light_color_sensor import EasyLightColorSensor

print("Example program for reading a Dexter Industries Light Color Sensor on an I2C port.")

my_lcs = EasyLightColorSensor(led_state = True)

while True:
    # Read the R, G, B, C color values
    red, green, blue, clear = my_lcs.safe_raw_colors()

    # Print the values
    print("Red: {:5.3f} Green: {:5.3f} Blue: {:5.3f} Clear: {:5.3f}".format(red, green, blue, clear))

    sleep(0.02)

Here’s how the output of the script should look like:

Example program for reading a Dexter Industries Light Color Sensor on an I2C port.
Red: 0.004 Green: 0.004 Blue: 0.004 Clear: 0.013
Red: 0.005 Green: 0.004 Blue: 0.004 Clear: 0.013
Red: 0.005 Green: 0.005 Blue: 0.004 Clear: 0.014
Red: 0.005 Green: 0.005 Blue: 0.004 Clear: 0.015
Red: 0.005 Green: 0.005 Blue: 0.004 Clear: 0.014
Red: 0.005 Green: 0.005 Blue: 0.004 Clear: 0.014
Red: 0.006 Green: 0.005 Blue: 0.005 Clear: 0.015