LEGO Mindstorms NXT Sensors

The nxt.sensor.generic module supports the sensors manufactured by LEGO.

class nxt.sensor.generic.Touch(brick, port)

LEGO Mindstorms NXT touch sensor, part number 53793.

is_pressed()

Get sensor pressed state.

Returns:

True if the sensor is pressed, else False.

Return type:

bool

get_sample()

Get sensor pressed state.

Returns:

True if the sensor is pressed, else False.

Return type:

bool

class nxt.sensor.generic.Light(brick, port, illuminated=True)

LEGO Mindstorms NXT light sensor, part number 55969.

This sensor is included in the first version of the Mindstorms NXT set and in the Education NXT set. Not to be confused with the color sensor included in the NXT 2.0 set.

Parameters:

illuminated (bool) – Initial illuminated state.

set_illuminated(active)

Set illuminated state.

Parameters:

active (bool) – True to activate light.

get_lightness()

Get detected light level.

Returns:

Detected light level between 0 and 1023.

Return type:

int

get_sample()

Get detected light level.

Returns:

Detected light level between 0 and 1023.

Return type:

int

class nxt.sensor.generic.Sound(brick, port, adjusted=True)

LEGO Mindstorms NXT sound sensor, part number 55963.

Parameters:

adjusted (bool) – Initial adjusted state.

set_adjusted(active)

Set adjusted state. When activated, the sensitivity of the sensor is adjusted to human perception.

Parameters:

active (bool) – True to activate adjustment.

get_loudness()

Get detected sound level.

Returns:

Detected sound level between 0 and 1023.

Return type:

int

get_sample()

Get detected sound level.

Returns:

Detected sound level between 0 and 1023.

Return type:

int

class nxt.sensor.generic.Ultrasonic(brick, port, check_compatible=True)

LEGO Mindstorms NXT ultrasonic distance sensor, part number 53792.

class Command(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Sensor command.

OFF = 0

Turn sensor OFF.

SINGLE_SHOT = 1

Enable single shot mode.

CONTINUOUS_MEASUREMENT = 2

Enable continuous measurement mode (default).

EVENT_CAPTURE = 3

Enable event capture mode, to measure whether any other sensor is in the neighbourhood.

REQUEST_WARM_RESET = 4

Reset sensor.

get_distance()

Get distance to detected object.

Returns:

Distance in cm.

Return type:

int

get_sample()

Get distance to detected object.

Returns:

Distance in cm.

Return type:

int

get_measurement_units()

Get measurement units.

Returns:

String representing the measurement units, should be “10E-2m”.

Return type:

str

get_all_measurements()

Get all the measurements.

Returns:

Eight distances in cm.

Return type:

(int, int, int, int, int, int, int, int)

get_measurement_no(number)

Get one of the measurements.

Parameters:

number (int) – Measurement number.

Returns:

Distance in cm.

Return type:

int

command(command)

Send a command to the sensor.

Parameters:

command (Command) – Command to send.

get_interval()

Get measurement interval, unknown unit.

Returns:

Content of interval measurement register.

Return type:

int

set_interval(interval)

Set measurement interval, unknown unit.

Parameters:

interval (int) – New content of interval measurement register.

class nxt.sensor.generic.Color(brick, port)

LEGO Mindstorms NXT color sensor, part number 64892.

This sensor is included in the Mindstorms NXT 2.0 set. Not to be confused with the light sensor included in the first version or in the Education set.

class DetectedColor(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Color detected by the sensor.

This is an enum.IntEnum for backward compatibility.

BLACK = 1

Black or low intensity.

BLUE = 2

Blue.

GREEN = 3

Green.

YELLOW = 4

Yellow.

RED = 5

Red.

WHITE = 6

White.

set_light_color(color)

Set the light color.

Parameters:

color (Type) – Light color, or off, must be one of the Type.COLOR_* values.

get_light_color()

Get the light color previously set.

Returns:

Light color, one of the Type.COLOR_* values.

Return type:

Type

get_reflected_light(color)

Get detected light level.

Parameters:

color (Type) – Light color, or off, must be one of the Type.COLOR_* values.

Returns:

Detected light level between 0 and 1023.

Return type:

int

get_color()

Get detected color.

Returns:

Detected color.

Return type:

DetectedColor

This also sets the sensor mode to color detection (light is cycling quickly to determine color).

get_sample()

Get detected color.

Returns:

Detected color.

Return type:

DetectedColor

This also sets the sensor mode to color detection (light is cycling quickly to determine color).

class nxt.sensor.generic.Temperature(brick, port)

LEGO Mindstorms NXT temperature sensor, part number 62840.

get_deg_c()

Get the temperature in degrees Celsius.

Returns:

Temperature in degrees Celsius.

Return type:

float

get_deg_f()

Get the temperature in degrees Fahrenheit.

Returns:

Temperature in degrees Fahrenheit.

Return type:

float

get_sample()

Get the temperature in degrees Celsius.

Returns:

Temperature in degrees Celsius.

Return type:

float