Color sensor

adapted from PyBricks

A sensor lets an EV3 program measure and collect data about is surroundings. The Color Sensor can detect color and reflected light.

An EV3 color sensor

Import

from pybricks.ev3devices import ColorSensor

Setup

# Initialize the sensors.
line_sensor = ColorSensor(Port.S1)

Functions

reflection()

Measures the reflection of a surface using a red light.

Returns:

  • reflection, ranging from 0 (no reflection) to 100 (high reflection).

rgb()

Measures the reflection of a surface using a red, green, and then a blue light.

Returns:

  • tuple of reflections for red, green, and blue light, each ranging from 0.0 (no reflection) to 100.0 (high reflection).
    (r, g, b)

color()

Measures the color of a surface.

Returns:

  • Color.BLACK, Color.BLUE, Color.GREEN, Color.YELLOW, Color.RED, Color.WHITE, Color.BROWN, or None if no color is detected.

ambient()

Measures the ambient light intensity.

Returns:

  • ambient light intensity, ranging from 0 (dark) to 100 (bright).

Example

# Go forward while reflected light is less than 10.
ev3.speaker.beep()
robot.drive(100,0)
while line_sensor.reflection() < 10:
    wait(10)
robot.stop()