ColorSensor#

Initializing the ColorSensor Class#

The ColorSensor constructor creates a ColorSensor object in the specified Smart Port:

Parameter

Description

port

A valid Smart Port that the Color Sensor is connected to.

# Construct a Color Sensor "colorsensor" with the
# ColorSensor class.
colorsensor = ColorSensor(Ports.PORT1)

This colorsensor object will be used in all subsequent examples throughout this API documentation when referring to ColorSensor class methods.

Class Methods#

hue()#

The hue() method returns the hue value detected by the Color Sensor.

Returns: The hue as a float in the range 0 - 359.99 degrees.

# Set 'hue' to the current hue detected by the Color Sensor.
hue = colorsensor.hue()

brightness()#

The brightness(readraw) method reads the brightness value from the Color Sensor.

Parameters

Description

readraw

Optional. Whether to return the raw value detected by the Color Sensor. The default is False.

Returns: The brightness as a float in the range 0 - 100%.

# Set 'brightness' to the current brightness detected by the Color Sensor.
brightness = colorsensor.brightness()

color()#

The color() method reads the color from the Color Sensor.

Returns: The color detected by the Color Sensor as an instance of the Color class.

# Set 'c' to the color detected by the Color Sensor.
c = colorsensor.color()

is_near_object()#

The is_near_object() method returns if the proximity sensor detects an object.

Returns: True if near an object. False if it is not.

# Check if the Color Sensor is near an object.
if colorsensor.is_near_object():
    print('near object')

set_light()#

The set_light(value) method sets the Color Sensor LED on or off.

Parameters

Description

value

A valid LEDStateType.

Returns: None.

# Turn on LED.
colorsensor.set_light(LedStateType.ON)

set_light_power()#

The set_light_power(value) method sets the Color Sensor LED to the requested power.

Parameters

Description

value

The power of the LED in the range 0 - 100%.

Returns: None.

object_detected()#

The object_detected(callback, arg) method registers a function to be called when an object detected event occurs.

Parameters

Description

callback

A function that will be called when an object detected event occurs.

arg

Optional. A tuple that is used to pass arguments to the callback function.

Returns: An instance of the Event class.

# Define a function detected()
def detected():
    # The Brain will print that an object was detected by the Color Sensor 
    # to the Brain Screen.
    brain.screen.print("object detected")

# Run detected when the Color Sensor detects an object
colorsensor.object_detected(detected)

installed()#

The installed() method checks for device connection.

Returns: True if the Color Sensor is connected. False if it is not.