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

sensor_color

The color sensor can measure light intensity and color values. There are a number of modes that the sensor can use. 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.

Tip

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.

Info

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
ValueColor
0%
1black
2blue
3green
4yellow
5red
6white
7brown

Raw Color Components Mode

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

RGB_color_cube

import ev3dev.ev3 as ev3

cs = ev3.ColorSensor()
cs.mode = 'RGB-RAW'
cs.raw
(354, 415, 543)
Note

When executing the code above, some of our sensors return the following error:

Traceback (most recent call last):
  […]
struct.error: unpack requires a buffer of 6 bytes

In this case you need to add a fourth “h” to the call, i.e. use cs.bin_data("hhhh"). The resulting tuple will be (r,g,b,x) where x is some mysterious value.

This behaviour should be consistent, i.e. should not change while using the same EV3 Brick and Color Sensor.

(We do not know why this is the case for some sensors, probably they were built with a different chip. If you find out more, please let us know.)


Touch Sensor

sensor_touch

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

Note

These sensors do not support any commands or modes.

ValueDescription
0Released
1Pressed
import ev3dev.ev3 as ev3

ts = ev3.TouchSensor()
ts.value()
0

# [press sensor]
ts.value()
1

Gyro Sensor

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.

Note

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-CAL'  # calibrate to 0 twice
gs.mode = 'GYRO-CAL'
gs.mode = 'GYRO-ANG'
gs.value()
0

# [rotate sensor]
gs.value()
33

Ultrasonic Sensor

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.

Info

Please take into account that the sensor will sometimes lock up if the mode was changed too frequently (e.g. by calling distance_* methods).
A delay of 250ms is highly recommended avoiding sensor timeouts.

Placing the sensor at the same height, at which the bottles concaved and knobbed area is located, can lead to problems. The sensor won’t be able to register bounced sound waves due to the bottle area dispersing reflections.

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  # Not recommended
35

us.value()  # Prevents setting the mode again, just fetches the value
35

References