Sensors / Input Devices¶
The EV3 has four input ports for connecting sensors and other devices (like sensor multiplexers or motor controllers). If you are trying to use something that plugs into one of those ports, you are in the right place.
Supported Sensors¶
This is a list of sensors that currently have drivers available in the ev3dev kernel. The Part Number column links to the vendor website. The Name column links to the sensor’s entry in Appendix A.
[1] | Only touch sensors that shipped with the NXT 2.0 set can be automatically detected. Older touch sensors that shipped with the original NXT sets are missing an electrical connection (pin 2 is not internally connected to pin 3) and cannot be automatically detected. |
[2] | (1, 2, 3, 4, 5, 6) The automatic detection algorithm detects this sensor
as an NXT/Analog type sensor but it cannot determine
the exact sensor type. The generic analog driver (nxt-analog ) will be
loaded by default for this sensor. See the The lego-port Subsystem for
information on how to manually load the correct driver. |
[3] | (1, 2, 3) The automatic detection algorithm detects this sensor as an I2C sensor and the port is automatically put into I2C mode. However, the sensor does not follow the LEGO MINDSTORMS convention for I2C sensors, so the exact type of sensor cannot be determined. See Appendix C: I2C Devices for information on how to manually load the correct driver. |
[4] | The automatic detection algorithm detects this sensor as an I2C sensor and the port is automatically put into I2C mode. However, this sensor only partially follows the LEGO MINDSTORMS convention for I2C sensors, so the driver must be loaded manually. See the sensor’s page for more information. |
[5] | The Dexter Industries dFlex sensor cannot be automatically
detected (because pin 2 is not connected to pin 3). In order to use this
sensor, you must manually set the port to nxt-analog mode and then set
the driver to di-dflex . |
Unsupported Sensors¶
One of the goals of ev3dev is to support as many sensors as possible. In fact, even if a manufacturer’s documentation says that a device does not work with the EV3, chances are it will work with ev3dev.
If you have a sensor that is not supported yet, let us know about it by opening an issue on GitHub. For many sensors adding a driver is trivial - even if you are not a “kernel hacker” or a “c programmer”. For the non-trivial sensors, see the contributing page for information on how to write a driver or how to donate hardware to someone who will.
Using Sensors¶
Automatic Detection¶
The EV3 has “smart” sensor ports that can identify most sensors. Beware! Full automatic detection works on EV3 only. And even on EV3, some sensors cannot be automatically detected. See notes below in the table of supported sensors.
For sensors that cannot be automatically detected, you can manually control the input port mode. Read more about it on Input / Output Ports page.
On FatcatLab’s EVB, EV3/Analog and EV3/UART sensors can be automatically detected. For NXT/Analog sensors, you must manually set the mode. Most NXT/I2C sensors should be automatically detected.
On BrickPi, BrickPi+ and BrickPi3, sensors cannot be automatically detected at all. You must manually configure the input ports for all sensors.
On PiStorms, detection of EV3/UART, EV3/Analog and NXT/I2C sensors is
semi-automatic. If you put the input port into ev3-uart
, ev3-analog
,
or i2c-thru
mode, the specific type of sensor will be automatically detected
(assuming it is a LEGO compatible sensor). So you usually don’t have to write to
the set_device
attribute of the port for these sensors.
MINDSTORMS Compatible Sensors¶
Most of the supported sensors are designed to work with LEGO MINDSTORMS. These
sensors use the LEGO sensor class. You can find these
in sysfs at /sys/class/lego-sensor/
. The sensors have modes that select
what type of data is read from the sensor. Generally, you will select a mode
and then read the data value from the sensor. Follow the link above for more
details.
Other Sensors and Advanced Usage¶
Many sensors that were not specifically designed for LEGO MINDSTORMS can be used
with ev3dev too. You can read more about the “Other” sensor types below. Also,
some MINDSTORMS compatible sensor may have advanced features that are not
accessible using the lego-sensor
class.
When using sensors this way, often automatic detection will not work correctly,
therefore it is usually best to manually select the mode of input port (e.g.
other-i2c
or other-uart
).
For more information on using I2C sensors, check out Appendix C: I2C Devices.
Types of Sensors¶
When dealing with sensors in ev3dev, it is useful to know how it communicates with the EV3 brick. There are three basic kinds of communication that the input ports can use to get information from the sensor, Analog Sensors, I2C Sensors and UART Sensors, plus one special type for LEGO NXT Color Sensors.
Analog Sensors¶
These are the simplest type of sensor. The measured value is converted to a voltage (0-5VDC) that is read by the EV3.
EV3/Analog Sensors¶
These are sensors that were designed specifically for the EV3 and will not work on the NXT because the pinout is different. They contain an ID resistor so that the EV3 can tell different types of sensors apart. The actual analog value is measured on pin6.
NXT/Analog Sensors¶
These sensors are designed for the NXT, but also work on the EV3. The EV3 cannot differentiate between most of these sensors though, so you have to tell it which one your have or just use the generic driver.
RCX/Analog Sensors¶
RCX sensors also fall into this category, but do not work with the EV3 - at least not with the converter cable described in the NXT Hardware Developers kit. This is due to a difference in the input port pins between the EV3 and the NXT. If someone wants design a new converter cable, we could make them work.
WeDo/Analog Sensors¶
WeDo sensors are also analog sensors. They are actually electrically similar to
EV3/Analog sensors (require 5V power and have ID resistor). Currently, we only
support WeDo sensors attached to a WeDo hub, but if someone would like to design
a cable and modify the wedo-sensor
and lego-ports
drivers, we could
easily make them work with the input ports on the EV3.
LEGO NXT Color Sensors¶
The LEGO NXT Color Sensor is in a class of its own. It uses a hybrid of analog and (non-standard) digital communications. The NXT Color Sensor is not usable at this point in time. We can detect it with the auto-detect, but we don’t have a driver for it yet.
I2C Sensors¶
I2C sensors are sensors that communicate with the intelligent brick via the I2C protocol. In the NXT documentation, they are referred to a “digital” sensors. These sensors can be sorted into two categories.
NXT/I2C Sensors¶
These are sensors designed using LEGO’s guidelines for NXT digital sensors. NXT/I2C sensors can be automatically detected because they contain vendor and product IDs at specific I2C registers.
Other/I2C Sensors¶
These can be any off-the-shelf I2C device or 3rd party sensors that are designed for LEGO MINDSTORMS, but do not follow the guidelines of NXT/I2C sensors.
UART Sensors¶
The EV3 has a UART transceiver connected to each input port that can be used to communicate with many devices using this widely used standard.
EV3/UART Sensors¶
These is a new type of sensor that is designed specifically for the EV3 (they don’t work with the NXT). These sensors are a bit “smarter” in that in addition to sending the data of what they measure, they also send information about their capabilities. This means that any new EV3/UART sensors should “just work” without us having to write new drivers.
Other/UART Sensors¶
In addition to sensors designed to work with EV3, any UART device can be connected. But, be careful of voltage levels. The EV3 uses 3.3V I/O. It is safe to connect other devices that use 3.3V or 5V I/O. But, don’t connect anything with higher voltage!
The lego-sensor Subsytem¶
The lego-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
.
Identifying sensors¶
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.
Sysfs¶
Sensors can be found at /sys/class/lego-sensor/sensor<N>
, where <N>
is incremented each time a sensor is loaded.
Note
The number <N>
is not related to the port address.
Attribute | Access | Description |
---|---|---|
address |
read-only | Returns the name of the port that the sensor is connected to, e.g.
in1 . I2C sensors also include the I2C address (decimal), e.g.
in1:i2c8 . |
bin_data |
read-only | Reading the file will give the unscaled raw values in the value<N>
attributes. Use bin_data_format , num_values and the individual
sensor documentation to determine how to interpret the data. |
bin_data_format |
read-only | Returns the format of the values in
|
command |
write-only | Sends a command to the sensor. See the individual sensor documentation
for possible commands. Sensors that do not support commands will
return -EOPNOTSUPP when writing to this attribute. |
commands |
read-only | Returns a space separated list of the valid commands for the
sensor. Returns -EOPNOTSUPP if no commands are supported. |
direct |
read/write | Allows direct communication with the sensor for using advanced
features that are not otherwise available through the lego-sensor
class. Returns -EOPNOTSUPP if the sensor does not support this.
Currently this only works with I2C sensors. For I2C sensors, use
:c:func:`seek()` to set the register to read or write from, then read or
write the number of bytes required. |
decimals |
read-only | Returns the number of decimal places for the values in the
value<N> attributes of the current mode. |
driver_name |
read-only | Returns the name of the sensor device/driver. See the list of supported sensors for a complete list of drivers. |
fw_version |
read-only | Returns the firmware version of the sensor if available. Currently only NXT/I2C sensors support this. |
mode |
read/write | Returns the current mode. Writing one of the values returned by
modes sets the sensor to that mode. See the individual sensor
documentation for a description of the modes available for each type
of sensor. |
modes |
read-only | Returns a space separated list of the valid modes for the sensor. |
num_values |
read-only | Returns the number of value<N> attributes that will return a
valid value for the current mode. |
poll_ms |
read/write | Returns the polling period of the sensor in milliseconds. Writing
sets the polling period. Setting to 0 disables polling. Returns
-EOPNOTSUPP if changing polling is not supported. Note: Setting
poll_ms too high can cause the input port autodetection to fail.
If this happens, use the mode attribute of the port to force the
port to nxt-i2c mode. Values must not be negative. |
units |
read-only | Returns the units of the measured value for the current mode. May return empty string if units are unknown. |
value<N> |
read-only | 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. |
text_value |
read-only | Returns a space delimited string representing sensor-specific text
values. Returns -EOPNOTSUPP if a sensor does not support text
values. |
Events¶
In addition to the usual add
and remove
events, the kernel change
event is emitted when mode
or poll_ms
is changed. The value<N>
attributes change too rapidly to be handled this way and therefore do not
trigger any uevents.
Sensor Modules¶
The sensor drivers are implemented in the following modules:
EV3/Analog¶
The ev3-analog-sensor
module provides all of the drivers for EV3/Analog
sensors.
You can a list of the the devices implemented by this module by reading the
driver_names
attribute in the /sys/bus/lego/drivers/ev3-analog-sensor/
.
EV3/UART¶
The ev3-uart-sensor-ld
module is a tty line discipline that runs on
top of a tty. It listens for the information data that is sent from EV3/UART
sensors. When it receives valid data, it negotiates with the sensor, telling
the sensor to enter data sending mode.
This line discipline has been assigned the number 29. To attach this line
discipline to a tty, run ldattach 29 /dev/tty<N>
where <N>
is the name
of the tty you want to connect to.
Note
This driver works with any tty, which means the sensor does not necessarily have to be plugged into one of the input ports on the EV3.
EV3/UART sensors do not require individual driver implementations like other
types of sensors. Instead, all of the needed info to sent from the sensor in
a common format. As a result, the name returned by the driver_name
attribute may not be a real driver name. For well-known sensors (the LEGO
EV3 sensors and FatcatLab sensors) it will return a name like lego-ev3-color
.
For unknown sensors it returns ev3-uart-<N>
, where <N>
is the type id
of the sensor.
The ev3-uart-sensor
module provides all of the drivers for EV3/UART
sensors. This module is only used on devices that can’t use the line
discipline.
You can a list of the the devices implemented by this module by reading the
driver_names
attribute in the /sys/bus/lego/drivers/ev3-uart-sensor/
.
NXT/Analog¶
The nxt-analog-sensor
module provides all of the drivers for NXT/Analog
sensors. These drivers scale the analog voltage read from the sensor to a
useful value.
You can a list of the the devices implemented by this module by reading the
driver_names
attribute in the /sys/bus/lego/drivers/nxt-analog-sensor/
.
NXT/I2C¶
The nxt-i2c-sensor
module provides all of the drivers for NXT/I2C sensors.
allow_autodetect |
Setting to N disables probing of sensors. Default is Y . |
default_poll_ms |
This provides the default value for the poll_ms attribute. A value
of 0 will disable polling by default. Changes only affect sensors
plugged in after the change was made. Default is 100 msec. Values must
not be negative. |
Note
These parameters can be changed at runtime by writing to
/sys/module/nxt_i2c_sensor/parameters/<parameter>
.
You can a list of the the devices implemented by this module by reading the
driver_names
attribute in the /sys/bus/lego/drivers/nxt-i2c-sensor/
.