ColorSensor

The LEGO Color Sensor

LEGO Color Sensor 45605

BrickLink item

The LEGO® Education SPIKE™ Color Sensor can sort between eight different colors and can measure reflected and ambient or natural light.

class buildhat.ColorSensor(port)

Color sensor

Parameters

port – Port of device

Raises

DeviceError – Occurs if there is no color sensor attached to port

callback(func)

Set callback function

Parameters

func – Callback function

property connected

Whether device is connected or not

Returns

Connection status

static desc_for_id(typeid)

Translate integer type id to something more descriptive than the device name

Parameters

typeid – Type of device

Returns

Description of device

property description

Device on port info

Returns

Device description

deselect()

Unselect data from mode

get()

Extract information from device

Returns

Data from device

Raises

DeviceError – Occurs if device not in valid mode

get_ambient_light()

Return the ambient light

Returns

Ambient light

Return type

int

get_color()

Return the color

Returns

Name of the color as a string

Return type

str

get_color_hsv()

Return the color

Returns

HSV representation

Return type

tuple

get_color_rgbi()

Return the color

Returns

RGBI representation

Return type

list

get_reflected_light()

Return the reflected light

Returns

Reflected light

Return type

int

isconnected()

Whether it is connected or not

Raises

DeviceError – Occurs if device no longer the same

mode(modev)

Set combimode or simple mode

Parameters

modev – List of tuples for a combimode, or integer for simple mode

property name

Determine name of device on port

Returns

Device name

static name_for_id(typeid)

Translate integer type id to device name (python class)

Parameters

typeid – Type of device

Returns

Name of device

off()

Turn off sensor

on()

Turn on the sensor and LED

reverse()

Reverse polarity

rgb_to_hsv(r, g, b)

Convert RGB to HSV

Based on https://www.rapidtables.com/convert/color/rgb-to-hsv.html algorithm

Parameters
  • r – Red

  • g – Green

  • b – Blue

Returns

HSV representation of color

Return type

tuple

segment_color(r, g, b)

Return the color name from RGB

Parameters
  • r – Red

  • g – Green

  • b – Blue

Returns

Name of the color as a string

Return type

str

select()

Request data from mode

Raises

DeviceError – Occurs if device not in valid mode

property typeid

Type ID of device

Returns

Type ID

property typeidcur

Type ID currently present

Returns

Type ID

wait_for_new_color()

Wait for new color or returns immediately if first call

Returns

Name of the color as a string

Return type

str

wait_until_color(color)

Wait until specific color

Parameters

color – Color to look for

Example

"""Example getting color values from color sensor"""

from buildhat import ColorSensor

color = ColorSensor('C')

print("HSV", color.get_color_hsv())
print("RGBI", color.get_color_rgbi())
print("Ambient", color.get_ambient_light())
print("Reflected", color.get_reflected_light())
print("Color", color.get_color())

print("Waiting for color black")
color.wait_until_color("black")
print("Found color black")

print("Waiting for color white")
color.wait_until_color("white")
print("Found color white")

while True:
    c = color.wait_for_new_color()
    print("Found new color", c)