Colour Sensor

Pybrick Documentation

To explore all Pybricks’ features check the Pybricks documentaion. This can also be seen in the right-hand panel of the Pybricks IDE.

The color sensor has three main modes: color, reflected light, and ambient light. It can also be used as a light source.

  • Color - In this mode, the colour sensor can differentiate eight different LEGO colors.

  • Reflected light intensity - In this mode, the color sensor emits a light and measures the amount reflected back into itself from the surface you are testing.

  • Ambient light intensity - In this mode, the colour sensor measures the amount of light in its environment, without producing its own light source.

color light sensor

To initialise the color sensor you must call the ColorSensor() class and nominate it’s port. For example, with the robot you would use:

color_sensor = ColorSensor(Port.D)

Color Sensor Functions

Pybricks provides three functions to interact with the color sensor:

  • color(surface=True)→ Color → Scans the color of a surface or an external light source.

    • Choose True to scan the color of objects and surfaces. Choose False to scan the color of screens and other external light sources.

  • reflection()→ int: % → Measures how much a surface reflects the light emitted by the sensor.

  • ambient()→ int: % → Measures the ambient light intensity.

Pybricks also has a configuring function to choose which colors can be returned by color():

  • detectable_colors(colors) → Specify only colors that you wish to detect in your application. This way, the full-color measurements are rounded to the nearest desired color, and other colors are ignored.

Finally Pybricks allows you to turn on the color sensor’s lights:

Color Sensor Example

Check how to use each of these functions by using the following code.

  1. Create a new file called color_sensor.py

  2. Type the code below into the file

  3. Predict what you think will happen.

  4. Run your code:

    • use the left button and right button to change color sensor’s mode

 1# color_sensor
 2
 3from pybricks.hubs import PrimeHub
 4from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
 5from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
 6from pybricks.robotics import DriveBase
 7from pybricks.tools import wait, StopWatch
 8
 9# --- SETUP
10hub = PrimeHub()
11color_sensor = ColorSensor(Port.D)
12modes = ["S", "L", "R", "A"]
13mode = 0
14
15# --- MAIN LOOP
16while True:
17    presses = hub.buttons.pressed()
18
19    if Button.LEFT in presses and mode > 0:
20        mode -= 1
21        wait(250)
22    elif Button.RIGHT in presses and mode < 3:
23        mode += 1
24        wait(250)
25
26    hub.display.char(modes[mode])
27    
28    if mode == 0:
29        print(color_sensor.color())
30    elif mode == 1:
31        print(color_sensor.color(False))
32    elif mode == 2:
33        print(color_sensor.reflection())
34    elif mode == 3:
35        print(color_sensor.ambient())

Investigate

  • lines 3 - 7 → imports all the Pybricks command for use with your robot

  • line 10 → initialises the hub

  • line 11 → initialises the color sensor

  • line 12 → a list providing an indicator letter for the modes

  • line 13 → a flag variable which keeps track of which mode the color sensor should use

  • line 16 → creates the main loop

  • line 17 → detects any buttons that are currently pressed and stores the in presses

  • lines 19 - 21 → changes mode downwards if left button is pressed

    • and mode > 0 ensures that the mode cannot go past 0

    • wait(250) prevents a single press registering mutiple times

  • lines 22 - 24 → changes mode upwards if right button is pressed

    • and mode < 3 ensures that the mode cannot go past 3

    • wait(250) prevents a single press registering mutiple times

  • line 26 → displays the corresponsing mode character from modes

  • lines 28 - 29 → gets the surface color reading and prints it

  • lines 30 - 31 → gets the light color reading and prints it

  • lines 32 - 33 → gets the reflected light color reading and prints it

  • lines 34 - 35 → gets the ambient light color reading and prints it

Modify

  • what happens when you comment out line 21 and line 24?

  • what happens if you remove and mode > 0 from line 19 and and mode < 3 from line 22?