RGB Light Sample

This sample demonstrates reading RGB light.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 RGB_Light.py

Sample Code

RGB_Light.py
o#!/usr/bin/env python3

#This sample demonstrates reading RGB light.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
from turta_iothat3 import Turta_Light

#Initialize
light = Turta_Light.LightSensor()

try:
    while True:
        #Read clear & RGB light values
        crgb = light.read_crgb_light()

        #Print the readings
        print("Clear...........: " + str(crgb[0]))
        print("Red.............: " + str(crgb[1]))
        print("Green...........: " + str(crgb[2]))
        print("Blue............: " + str(crgb[3]))

        #Wait
        print("-----")
        sleep(0.5)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints clear and RGB light measurements. The example output should be like this:

Clear...........: 0
Red.............: 0
Green...........: 0
Blue............: 0
-----

To stop the Python script, just press CTRL + C.

Last updated