TMC - 8 Digital Tech
Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto mode

Color Sensor

adapted from PyBricks

Color Sensor

In almost all programming, American spellings are used. So as programmers we use colors, even though we would normally spell the word colours.

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

Three modes: Color, Reflected Light intensity and Ambient Light intensity.

ā€“ Color Mode: Recognizes 7 colors (black, brown, blue, green, yellow, red, white) and No Color

ā€“ Reflected Light: Measures the intensity of the light reflected back from a lamp that emits a red light. (0=very dark and 100=very light)

ā€“ Ambient Light: Measures the strength of the light that enters the sensor from the environment. (0=very dark and 100=very light)

Example Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.parameters import Port, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)

# Initialize the EV3 Brick.
ev3 = EV3Brick()

# Initialize the motors.
left_motor = Motor(Port.B)
right_motor = Motor(Port.C)

# Initialize the drive base.
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=104)

Here we initialize what sensors we would like by telling the EV3 what port it is plugged into and what we would like to call it when referring to it in our code.

18
19
20
21
22
23
24
25
26
# Initialize the sensors.
line_sensor = ColorSensor(Port.S1)

# 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()