Skip to content

Color Sensor

Cort edited this page Jun 23, 2022 · 10 revisions

This simulates a color sensor, not unlike those found on a Lego EV3 robot.

Working Principles (Simulator)

It works by simulating a virtual camera at the sensor location, capturing an image, then processing the image pixels to determine the average RGB values. This approach allows the sensor to detect colors and textures on any object in the simulator, even if there are multiple objects in the field of view simultaneously. When capturing the image for the color sensor, all lightings are temporarily disabled, so the result is not affected by shadows or angle of lighting.

Differences from a real sensor

  • Readings from a real sensor is affected by distance (ie. nearer objects gives a higher value). The simulated sensor readings is not affected by distance, as long as the object is within range. This range is set to 5cm by default.

  • The Lego EV3 is capable of making thousands of color sensor readings per second. As the process of rendering an image for the virtual camera is slow, the number of readings per second for the simulated sensor is much lower. Depending on the CPU and graphics card, you may see as little as 20 - 30 readings per second.

Reflected light intensity

The reflected light intensity is scaled to 100% and uses only the red channel.

Colors names and numbers

When reading "color", the sensor will return a number representing the color. For example, the number 5 represents red. The list of numbers and corresponding colors are as follows...

  • 1: BLACK
  • 2: BLUE
  • 3: GREEN
  • 4: YELLOW
  • 5: RED
  • 6: WHITE
  • 7: BROWN

These are the same values used by the EV3, but unlike the real EV3, the Gears color sensor will never return a '0' (No color).

When reading "color name", the sensor will return the values (case sensitive)...

  • Black
  • Blue
  • Green
  • Yellow
  • Red
  • White
  • Brown

Note that Pybricks do not support color name. If running in Pybricks mode, you must use color number instead.

When programming in blocks, you can use the "<color> as color <number/name>" block in comparisons like this...

...or you can simply type in the number / color name like this.

Red, Green, Blue, RGB(list)

These will return the value for each channel, ranging from 0 to 255 (Ev3dev mode) and 0 to 100 (Pybricks mode). The "rgb (list)" setting returns all three color values in the form of a list.

Configurations

Default values are shown below.

"type": "ColorSensor",
"options": {
  "sensorResolution": 8,
  "sensorMinRange": 0.1,
  "sensorMaxRange": 5,
  "sensorFov": 1.3
}

sensorResolution: The resolution of the image captured by the virtual camera. The default is an 8x8 image. You generally should not change this.

sensorMinRange: Anything closer than this range (...in cm) will not be seen by the virtual camera.

sensorMaxRange: Anything further than this range (...in cm) will not be seen by the virtual camera.

sensorFov: The sensor's field of view (...in radian). The default of 1.3 is around (74.5 degrees).