MicroPython Tutorial XIV

Lets move forward to the most interesting of all the sensors you get with the LEGO MINDSTORMS sets, the colour sensor. It is if you have extra budget the one to buy more of since it is the most versatile by far.

Some background. The colour sensor, you need to spell it the American way in your code has four modes in MicroPython. Color, which returns one of 9 colours excluding “noneType” which means it can’t tell. Ambient, which returns a grey scale going form zero [dark] to 100 [bright]. Reflection which also returns a scale going from 0 [no reflection] to 100 [highly reflective]. And finally rgb, the most flexible, and difficult to configure a tupple with red, green and blue light intensities figures ranging from 0.0 to 100.0

How do you use it, firstly its much like all the others, you need to name it in the devices and then declare in your code where it is plugged in.

from pybricks.ev3devices import ColorSensor
colourScanner = ColorSensor(Port.S1)

Notice I how I spelt color, the American spelling. Beyond that you need simply call one of the methods in the class mentioned. Here a quick script showing it reading a colored set of lines.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Color, Port
from pybricks.tools import wait
from pybricks.ev3devices import ColorSensor, Motor
colorScanned = ColorSensor(Port.S1)
colorSeen = colorScanned.color()
leftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)
colourNames = ["na","black","blue","green","yellow","red","white","brown","orange","purple"]while colorSeen != Color.RED:
colorSeen = colorScanned.color()
if type(colorSeen) is int:
print("colorSeen ",colourNames[colorSeen],colorSeen)
leftMotor.dc(10)
rightMotor.dc(10)

You should be able figure out what this script does… it defines a color sensor and motors; the color names in a list; and has a loop that advances slowly looking for the color red, printing the colors as it passes over them.

Note “na” isn’t a color, it is simply a place holder cause the color indexes start at 1 and list indexes start at 0.

We can write an almost identical script that uses reflections. Here is a script based on the one just shown that produces a cross referenced list mapping colours & their reflective properties.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Color, Port
from pybricks.tools import wait
from pybricks.ev3devices import ColorSensor, Motor
colorScanned = ColorSensor(Port.S1)
colorSeen = colorScanned.color()
colorReflected = colorScanned.reflection()
leftMotor = Motor(Port.B)
rightMotor = Motor(Port.C)
colourNames = ["na","black","blue","green","yellow","red","white","brown","orange","purple"]
colorMap = {}
while colorSeen != Color.RED:
colorSeen = colorScanned.color()
colorReflected = colorScanned.reflection()
if type(colorSeen) is int:
print("colorSeen ",colourNames[colorSeen],colorSeen)
colorMap[colorReflected] = colourNames[colorSeen]
leftMotor.dc(10)
rightMotor.dc(10)
print(colorMap)

Using it you can build a map linking colors to reflections. Different colours have different reflective properties. An interesting exercise and one in which you’ll notice the major challenges when using this sensor to detect anything.

The challenge being that the results area greatly affected by the external light conditions and indeed even distance the sensor is from the source.

Of course you’re most lightly not actually interested in the colour or the reflectiveness of it. But would use said information to control some other aspect. Example in hand we set out a track on the floor with coloured tape, blue on the outside and red on the inside. The robot was then told [programmed of course] to bear left if it saw blue tape and bear right if it came across red. The two colours effectively showing the robot what it needed to get around the track.

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store