Sensor classes

Sensor

This is the base class all the other sensor classes are derived from.

class ev3dev.core.Sensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

The sensor class provides a uniform interface for using most of the sensors available for the EV3. The various underlying device drivers will create a lego-sensor device for interacting with the sensors.

Sensors are primarily controlled by setting the mode and monitored by reading the value<N> attributes. Values can be converted to floating point if needed by value<N> / 10.0 ^ decimals.

Since the name of the sensor<N> device node does not correspond to the port that a sensor is plugged in to, you must look at the address attribute if you need to know which port a sensor is plugged in to. However, if you don’t have more than one sensor of each type, you can just look for a matching driver_name. Then it will not matter which port a sensor is plugged in to - your program will still work.

address

Returns the name of the port that the sensor is connected to, e.g. ev3:in1. I2C sensors also include the I2C address (decimal), e.g. ev3:in1:i2c8.

bin_data(fmt=None)

Returns the unscaled raw values in the value<N> attributes as raw byte array. Use bin_data_format, num_values and the individual sensor documentation to determine how to interpret the data.

Use fmt to unpack the raw bytes into a struct.

Example:

>>> from ev3dev import *
>>> ir = InfraredSensor()
>>> ir.value()
28
>>> ir.bin_data('<b')
(28,)
bin_data_format

Returns the format of the values in bin_data for the current mode. Possible values are:

  • u8: Unsigned 8-bit integer (byte)
  • s8: Signed 8-bit integer (sbyte)
  • u16: Unsigned 16-bit integer (ushort)
  • s16: Signed 16-bit integer (short)
  • s16_be: Signed 16-bit integer, big endian
  • s32: Signed 32-bit integer (int)
  • float: IEEE 754 32-bit floating point (float)
command

Sends a command to the sensor.

commands

Returns a list of the valid commands for the sensor. Returns -EOPNOTSUPP if no commands are supported.

decimals

Returns the number of decimal places for the values in the value<N> attributes of the current mode.

driver_name

Returns the name of the sensor device/driver. See the list of [supported sensors] for a complete list of drivers.

mode

Returns the current mode. Writing one of the values returned by modes sets the sensor to that mode.

modes

Returns a list of the valid modes for the sensor.

num_values

Returns the number of value<N> attributes that will return a valid value for the current mode.

units

Returns the units of the measured value for the current mode. May return empty string

value(n=0)

Returns the value or values measured by the sensor. Check num_values to see how many values there are. Values with N >= num_values will return an error. The values are fixed point numbers, so check decimals to see if you need to divide to get the actual value.

Special sensor classes

The classes derive from Sensor and provide helper functions specific to the corresponding sensor type. Each of the functions makes sure the sensor is in the required mode and then returns the specified value.

Touch Sensor

class ev3dev.core.TouchSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

Touch Sensor

MODE_TOUCH = 'TOUCH'

Button state

is_pressed

A boolean indicating whether the current touch sensor is being pressed.

Color Sensor

class ev3dev.core.ColorSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO EV3 color sensor.

COLOR_BLACK = 1

Black color.

COLOR_BLUE = 2

Blue color.

COLOR_BROWN = 7

Brown color.

COLOR_GREEN = 3

Green color.

COLOR_NOCOLOR = 0

No color.

COLOR_RED = 5

Red color.

COLOR_WHITE = 6

White color.

COLOR_YELLOW = 4

Yellow color.

MODE_COL_AMBIENT = 'COL-AMBIENT'

Ambient light. Red LEDs off.

MODE_COL_COLOR = 'COL-COLOR'

Color. All LEDs rapidly cycling, appears white.

MODE_COL_REFLECT = 'COL-REFLECT'

Reflected light. Red LED on.

MODE_REF_RAW = 'REF-RAW'

Raw reflected. Red LED on

MODE_RGB_RAW = 'RGB-RAW'

Raw Color Components. All LEDs rapidly cycling, appears white.

ambient_light_intensity

Ambient light intensity. Light on sensor is dimly lit blue.

blue

Blue component of the detected color, in the range 0-1020.

color
Color detected by the sensor, categorized by overall value.
  • 0: No color
  • 1: Black
  • 2: Blue
  • 3: Green
  • 4: Yellow
  • 5: Red
  • 6: White
  • 7: Brown
green

Green component of the detected color, in the range 0-1020.

raw

Red, green, and blue components of the detected color, in the range 0-1020.

red

Red component of the detected color, in the range 0-1020.

reflected_light_intensity

Reflected light intensity as a percentage. Light on sensor is red.

Ultrasonic Sensor

class ev3dev.core.UltrasonicSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO EV3 ultrasonic sensor.

MODE_US_DIST_CM = 'US-DIST-CM'

Continuous measurement in centimeters.

MODE_US_DIST_IN = 'US-DIST-IN'

Continuous measurement in inches.

MODE_US_LISTEN = 'US-LISTEN'

Listen.

MODE_US_SI_CM = 'US-SI-CM'

Single measurement in centimeters.

MODE_US_SI_IN = 'US-SI-IN'

Single measurement in inches.

distance_centimeters

Measurement of the distance detected by the sensor, in centimeters.

distance_inches

Measurement of the distance detected by the sensor, in inches.

other_sensor_present

Value indicating whether another ultrasonic sensor could be heard nearby.

Gyro Sensor

class ev3dev.core.GyroSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO EV3 gyro sensor.

MODE_GYRO_ANG = 'GYRO-ANG'

Angle

MODE_GYRO_CAL = 'GYRO-CAL'

Calibration ???

MODE_GYRO_FAS = 'GYRO-FAS'

Raw sensor value

MODE_GYRO_G_A = 'GYRO-G&A'

Angle and rotational speed

MODE_GYRO_RATE = 'GYRO-RATE'

Rotational speed

angle

The number of degrees that the sensor has been rotated since it was put into this mode.

rate

The rate at which the sensor is rotating, in degrees/second.

rate_and_angle

Angle (degrees) and Rotational Speed (degrees/second).

Infrared Sensor

class ev3dev.core.InfraredSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO EV3 infrared sensor.

MODE_IR_CAL = 'IR-CAL'

Calibration ???

MODE_IR_PROX = 'IR-PROX'

Proximity

MODE_IR_REMOTE = 'IR-REMOTE'

IR Remote Control

MODE_IR_REM_A = 'IR-REM-A'

IR Remote Control. State of the buttons is coded in binary

MODE_IR_SEEK = 'IR-SEEK'

IR Seeker

proximity

A measurement of the distance between the sensor and the remote, as a percentage. 100% is approximately 70cm/27in.

Sound Sensor

class ev3dev.core.SoundSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO NXT Sound Sensor

MODE_DB = 'DB'

Sound pressure level. Flat weighting

MODE_DBA = 'DBA'

Sound pressure level. A weighting

sound_pressure

A measurement of the measured sound pressure level, as a percent. Uses a flat weighting.

sound_pressure_low

A measurement of the measured sound pressure level, as a percent. Uses A-weighting, which focuses on levels up to 55 dB.

Light Sensor

class ev3dev.core.LightSensor(address=None, name_pattern='sensor*', name_exact=False, **kwargs)

Bases: ev3dev.core.Sensor

LEGO NXT Light Sensor

MODE_AMBIENT = 'AMBIENT'

Ambient light. LED off

MODE_REFLECT = 'REFLECT'

Reflected light. LED on

ambient_light_intensity

A measurement of the ambient light intensity, as a percentage.

reflected_light_intensity

A measurement of the reflected light intensity, as a percentage.