- detect the colour of a surface or light source
- detect the amount of light reflected from a surface
- detect the amount of ambient light
:class: important
To explore all Pybricks' features check the **[Pybricks documentaion](https://docs.pybricks.com/en/stable/index.html)**. 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.
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)
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. ChooseFalse
to scan the color of screens and other external light sources.
- Choose
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:
lights.on(brightness)
→ Turns on the lights at the specified brightness.
Check how to use each of these functions by using the following code.
- Create a new file called
color_sensor.py
- Type the code below into the file
- Predict what you think will happen.
- Run your code:
- use the left button and right button to change color sensor's mode
:linenos:
- **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
:class: caution
- 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**?