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

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.

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.

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.

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.

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

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

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.