NXT Color Sensor

../_images/nxtdevice-color.png
class ColorSensor(port)

LEGO® MINDSTORMS® NXT Color Sensor.

Parameters:

port (Port) – Port to which the sensor is connected.

color() Color

Scans the color of a surface.

You choose which colors are detected using the detectable_colors() method. By default, it detects Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.WHITE, or Color.NONE.

Returns:

Detected color.

ambient() int: %

Measures the ambient light intensity.

Returns:

Ambient light intensity, ranging from 0% (dark) to 100% (bright).

reflection() int: %

Measures how much a surface reflects the light emitted by the sensor.

Returns:

Measured reflection, ranging from 0% (no reflection) to 100% (high reflection).

rgb() tuple[int, int, int]

Measures the reflection of a surface using a red, green, and then a blue light.

Returns:

Tuple of reflections for red, green, and blue light, each ranging from 0.0% (no reflection) to 100.0% (high reflection).

Advanced color sensing

hsv() Color

Scans the color of a surface.

This method is similar to color(), but it gives the full range of hue, saturation and brightness values, instead of rounding it to the nearest detectable color.

Returns:

Measured color. The color is described by a hue (0–359), a saturation (0–100), and a brightness value (0–100).

detectable_colors(colors)
detectable_colors() Collection[Color]

Configures which colors the color() method should detect.

Specify only colors that you wish to detect in your application. This way, the full-color measurements are rounded to the nearest desired color, and other colors are ignored. This improves reliability.

If you give no arguments, the currently chosen colors will be returned.

When coding with blocks, this is configured in the sensor setup block.

Parameters:

colors (list or tuple) – List of Color objects: the colors that you want to detect. You can pick standard colors such as Color.MAGENTA, or provide your own colors like Color(h=348, s=96, v=40) for even better results. You measure your own colors with the hsv() method.

Built-in light

This sensor has a built-in light. You can make it red, green, blue, or turn it off.

light.on(color)

Turns on the light at the specified color.

Parameters:

color (Color) – Color of the light.

light.off()

Turns off the light.