Color Unit

Support the following products:

ColorUnit

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import *
 9from unit import ColorUnit
10
11
12title0 = None
13label0 = None
14label1 = None
15label2 = None
16i2c0 = None
17color_0 = None
18
19
20def setup():
21    global title0, label0, label1, label2, i2c0, color_0
22
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    title0 = Widgets.Title(
26        "ColorUnit Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
27    )
28    label0 = Widgets.Label("lux:", 2, 59, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29    label1 = Widgets.Label("color:", 2, 114, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30    label2 = Widgets.Label("saturation:", 2, 166, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31
32    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
33    color_0 = ColorUnit(i2c0)
34
35
36def loop():
37    global title0, label0, label1, label2, i2c0, color_0
38    M5.update()
39    label0.setText(str((str("Iux:") + str((color_0.get_lux())))))
40    label1.setText(str((str("color:") + str((color_0.get_color_rgb_bytes())))))
41    label2.setText(str((str("saturation:") + str((color_0.get_color_s())))))
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

UIFLOW2 Example:

example.png

color_core2_example.m5f2

class ColorUnit

Constructors

class ColorUnit(i2c, address=_TCS3472_DEFAULT_ADDR)

Initialize ColorUnit sensor with the given I2C interface and address.

Parameters:
  • i2c (I2C) – The I2C bus instance for communication.

  • address (int) – The I2C address of the sensor, default is _TCS3472_DEFAULT_ADDR(0x29).

UIFLOW2:

init.png

Methods

ColorUnit.get_lux() float

Get the lux value computed from the color channels.

Returns:

The computed lux value as a float.

UIFLOW2:

get_lux.png

ColorUnit.get_color_temperature() float

Get the color temperature in degrees Kelvin.

Returns:

The color temperature as a float in Kelvin.

UIFLOW2:

get_color_temperature.png

ColorUnit.get_color_rgb_bytes() tuple

Get the RGB color detected by the sensor.

Returns:

A tuple of red, green, and blue component values as bytes (0-255).

UIFLOW2:

get_color_rgb_bytes.png

ColorUnit.get_color_r() int

Get the red component of the RGB color.

Returns:

The red component value (0-255).

UIFLOW2:

get_color_r.png

ColorUnit.get_color_g() int

Get the green component of the RGB color.

Returns:

The green component value (0-255).

UIFLOW2:

get_color_g.png

ColorUnit.get_color_b() int

Get the blue component of the RGB color.

Returns:

The blue component value (0-255).

UIFLOW2:

get_color_b.png

ColorUnit.get_color_h() int

Get the hue (H) value of the color in degrees.

Returns:

The hue value as an integer in the range [0, 360].

UIFLOW2:

get_color_h.png

ColorUnit.get_color_s() float

Get the saturation (S) value of the color.

Returns:

The saturation value as a float in the range [0, 1].

UIFLOW2:

get_color_s.png

ColorUnit.get_color_v() float

Get the value (V) of the color (brightness).

Returns:

The value as a float in the range [0, 1].

UIFLOW2:

get_color_v.png

ColorUnit.get_color() int

Get the RGB color as an integer value.

Returns:

An integer representing the RGB color, with 8 bits per channel.

UIFLOW2:

get_color.png

ColorUnit.get_color565() int

Get the RGB color in 5-6-5 format as an integer. :return: An integer representing the RGB color in 5-6-5 format.

UIFLOW2:

get_color565.png

ColorUnit.get_active() bool

Get the active state of the sensor.

Returns:

True if the sensor is active, False if it is inactive.

UIFLOW2:

get_active.png

ColorUnit.set_active(val)

Set the active state of the sensor.

Parameters:

val (bool) – : True to activate the sensor, False to deactivate it.

UIFLOW2:

set_active.png

ColorUnit.get_integration_time() float

Get the integration time of the sensor in milliseconds.

Returns:

The integration time as a float.

UIFLOW2:

get_integration_time.png

ColorUnit.set_integration_time(val)

Set the integration time of the sensor.

Parameters:

val (float) – : The desired integration time in milliseconds.

Raises:

ValueError – If the integration time is out of the allowed range.

UIFLOW2:

set_integration_time.png

ColorUnit.get_gain() int

Get the gain of the sensor.

Returns:

The gain value, which should be one of 1, 4, 16, or 60.

UIFLOW2:

get_gain.png

ColorUnit.set_gain(val)

Set the gain of the sensor.

Parameters:

val (int) – : The desired gain value (1, 4, 16, or 60).

Raises:

ValueError – If the gain is not one of the allowed values.

UIFLOW2:

set_gain.png

ColorUnit.read_interrupt() bool

Read the interrupt status.

Returns:

True if the interrupt is set, False otherwise.

UIFLOW2:

read_interrupt.png

ColorUnit.clear_interrupt()

Clear the interrupt status of the sensor by writing to the interrupt register.

UIFLOW2:

clear_interrupt.png

ColorUnit.get_color_raw()

Read the raw RGBC color detected by the sensor.

Returns:

A tuple containing raw red, green, blue, and clear color data.

UIFLOW2:

get_color_raw.png

ColorUnit.get_cycles()

Get the persistence cycles of the sensor.

Returns:

The persistence cycles or -1 if interrupts are disabled.

UIFLOW2:

get_cycles.png

ColorUnit.set_cycles(val)

Set the persistence cycles for the sensor.

Parameters:

val (int) – : The number of persistence cycles, or -1 to disable interrupts.

Raises:

ValueError – If the value is not one of the permitted cycle values.

UIFLOW2:

set_cycles.png

ColorUnit.get_min_value()

Get the minimum threshold value (AILT register) of the sensor.

Returns:

The minimum threshold value.

UIFLOW2:

get_min_value.png

ColorUnit.set_min_value(val)

Set the minimum threshold value (AILT register) of the sensor.

Parameters:

val (int) – : The minimum threshold value to set.

UIFLOW2:

set_min_value.png

ColorUnit.get_max_value()

Get the maximum threshold value (AIHT register) of the sensor.

Returns:

The maximum threshold value.

UIFLOW2:

get_max_value.png

ColorUnit.set_max_value(val)

Set the maximum threshold value (AIHT register) of the sensor.

Parameters:

val (int) – : The maximum threshold value to set.

UIFLOW2:

set_max_value.png

ColorUnit.get_glass_attenuation()

Get the Glass Attenuation factor used to compensate for lower light levels due to glass presence.

Returns:

The glass attenuation factor (ga).

UIFLOW2:

get_glass_attenuation.png

ColorUnit.set_glass_attenuation(value)

Set the Glass Attenuation factor used to compensate for lower light levels due to glass presence.

Parameters:

value (float) – : The glass attenuation factor to set. Must be greater than or equal to 1.

Raises:

ValueError – If the value is less than 1.

UIFLOW2:

set_glass_attenuation.png