Sensors

You can connect a lot of different sensors to the EV3 bricks. In the following you will be introduced to the most common ones, that you are probably going to need when participating in the RoboLab.

Color Sensor

Only for Spring Course

sensor color

The color sensor can measure light intensity and color values. There are several different sensing modes available. Make sure to pick one that is suitable for your task.
For best results, it is recommended to create a shield to cover the sensor and keep the distance between 8-12mm to the object.

Continuous, fast changes of the sensing mode can slow down your robot and may lead to instabilities or crashes. Therefore, it is recommended to stick with one.

Pretty much no sensor returns accurate colors, so we prepared an example-measurement for all sensors.
The graph resulted from moving the sensor over colors in the following order: White, Red, White, Blue, White, Black, White, Black, Yellow (unused), Black, White.

Please note that these are just examples and own measurements are very advisable.

Color Mode

This mode returns the estimated color. It is fairly limited as just 8 different values are distinguished.

>>> import ev3dev.ev3 as ev3
>>> cs = ev3.ColorSensor()
>>> cs.mode = 'COL-COLOR'
>>> cs.value()
1
Table 1. COL-COLOR Mode
ValueColor

0

%

1

black

2

blue

3

green

4

yellow

5

red

6

white

7

brown

Raw Color Components Mode

In this mode the sensor returns triples of the RGB color space.

RGB color cube.svg
>>> import ev3dev.ev3 as ev3
>>> cs = ev3.ColorSensor()
>>> cs.mode = 'RGB-RAW'
>>> cs.bin_data("hhh")
(354, 415, 543)

Light Sensor

Only for Autumn Course

sensor light

The light sensor from the old LEGO MINDSTORMS NXT sets does not offer color detection but reads the grey scale values quite well. To put it more general, it can be used for intensity measurements.
For best results, it is recommended to create a shield to cover the sensor.

This sensor supports two modes. While AMBIENT simply measures the intensity of light at the sensor, we recommend to use REFLECT as it switches on an auxiliary LED, thus drastically increasing the quality of measurements.

>>> import ev3dev.ev3 as ev3
>>> ls = ev3.LightSensor()
>>> ls.mode = 'AMBIENT' # LED is off
>>> ls.ambient_light_intensity()
222.2
>>> ls.mode = 'REFLECT' # LED is on
>>> ls.reflected_light_intensity()
420.0

Touch Sensor

sensor touch

A touch sensors simply returns if the button at its front is currently released or being pressed.

These sensors do not support any commands or modes.

Table 2. Touch Sensor Return Values
ValueDescription

0

Released

1

Pressed

>>> import ev3dev.ev3 as ev3
>>> ts = ev3.TouchSensor()
>>> ts.value()
0
[press sensor]
>>> ts.value()
1

Gyro Sensor

Only for Spring Course

sensor gyro

The gyro sensor measures angular velocity and has to be calibrated initially. Make sure to keep the robot or sensor still while performing the initial calibration. Otherwise you set a wrong base and so called gyro drift will occur, rendering the measurements useless.

Due to high inaccuracy of the gyro sensor, its use is only recommended if your program is designed with fault tolerance in mind.

>>> import ev3dev.ev3 as ev3
>>> gs = ev3.GyroSensor()
>>> gs.mode = 'GYRO-RATE' # calibrate to 0
>>> gs.mode = 'GYRO-ANG'
>>> gs.value()
0
[rotate sensor]
>>> gs.value()
33

Ultrasonic Sensor

Only for Spring Course

sensor ultrasonic

The ultrasonic sensor can be used to detect obstacles lying ahead. Measurements can be done in centimeters or inch. It is also possible to detect other ultrasonic sensors nearby.

Please take into account that the sensor will sometimes lock up if the mode was changed too frequently.
A delay of 250ms is highly recommended to keep the sensor working.

>>> import ev3dev.ev3 as ev3
>>> us = ev3.UltrasonicSensor()
>>> us.mode = 'US-DIST-CM' # Continuous measurement in centimeters (for inch use US-DIST-IN)
>>> us.mode = 'US-SI-CM' # Single measurement in centimeters (for inch use US-SI-IN)
>>> us.distance_centimeters
35