Introduction
Let's read colour data from a PiicoDev® Colour Sensor with a Micro:bit V2, and create a colour sorting machine. This guide will cover connecting the hardware, downloading some example code and remixing it.
To follow along, it's best to have:
- A Micro:bit V2
- A PiicoDev Colour Sensor VEML6040
- A PiicoDev Adapter for Micro:bit
- A PiicoDev Cable
- (Optional) A PiicoDev platform helps secure everything together.
We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).
Also, some colourful objects like coloured paper, fruit etc. will be useful for test-driving our colour sensor!
Contents
- Introduction - Colour Theory Crash Course
- Connect the Sensor
- Download MicroPython Modules
- Example Code - Read RGB data
- Remix - Fruit Sorting Machine
- Conclusion
- Resources
Introduction - Colour Theory Crash Course
Red, Green Blue
The Red, Green, Blue (RGB) colour model is an additive colour model, meaning that it mixes (adds) Red, Green and Blue light to create many colours. RGB is used for displaying the colours on the screen you are reading this article - each pixel emits a certain amount of Red, Green, and Blue light, which - when mixed - creates the colours you're seeing.
Hue, Saturation, Value (Brightness)
Hue is a property that describes the colour of the colour - the colour's identity - red, green, blue, yellow etc. Hue does not describe anything about how bright or intense the colour is.
Saturation is the intensity of colour; A bright red has high saturation, as opposed to a reddish-grey, which has very low saturation.
Value is the amount of light in a colour - A light red has a high value, a dark red has a low value. Usually, this is directly related to light; where there’s light there’s a high-value colour and the opposite in the shadow.
The HSV model is really useful for identifying or classifying colours because the hue property is the base colour. Hue is represented as an angle on a colour wheel, so you can identify a colour using a single variable - the angle. Referring to the colour wheel below, Red is described with a zero-angle, moving through to Green at 120 degrees, Blue at 240 degrees, and back to Red.
Source: https://commons.wikimedia.org
Connect the PiicoDev Colour Sensor to your Micro:bit
Plug your Micro:bit into the adapter, connect your colour sensor to the adapter via the PiicoDev cable, and finally connect your Micro:bit to your computer with a USB lead.
If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.
Download MicroPython modules
We will need three files to easily read data from the Colour Sensor:
- Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as").
- Download the device module: PiicoDev_VEML6040.py (right-click, "save link as")
- Download the example script: main.py (right-click, "save link as")
It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev
Example Code - Read RGB data
We'll be working in Thonny - if you're unfamiliar working with Thonny see our guide for Thonny and Micro:bit
Open Thonny, connect to your Micro:bit and navigate to where you stored the files from the last step. Right-click each file and select "Upload to micro:bit" (Hint: View the files menu with View > Files)
Restart your Micro:bit (Keyboard shortcut, Ctrl D) and you should see RGB colour data streaming up the shell.
Above: RGB Colour data streams up the Shell, while the Plot (bottom-right) shows historic data. I first hold a carrot over the sensor and then a lime. Referring to the plot; The proportion of red is significantly higher for the carrot than the lime.
This is the raw RGB data that the colour sensor is sampling - it's useful for sampling emitted lights like an RGB LED. What the RGB model is not so good at is identifying a colour, since the light-intensity information is wrapped up in the RGB values. A dimmer coloured light or a sample held farther away from the sensor means each parameter (RGB) will read less light and it's tricky to infer whether it is the colour changing or just the lightness.
Example 2 - Read HSV data
Now we'll identify some colours that we show the sensor. This is where the HSV colour module is useful because it separates the hue (colour) from the value (lightness). To classify a bright colour then, we can just read the hue, and ignore saturation or value.
Comment out the print statement in the first example (Example 1), and uncomment the code block under Example 2. Now, we are sampling colour in the HSV colour space and using just the hue to classify a colour. By default, the classifyHue() function will return one of the six simple colours covered shown in the Introduction's colour wheel.
Try holding a coloured object in front of the sensor and see what hues are returned - record these values for later.
Above: Classifying different hues - a yellow object is held over the sensor, then swapped for a red object.
Remix - Fruit Sorting Machine
The classifyHue() function returns one of six simple colours by default. We can provide the function with our own custom dictionary of hues and labels. For example, we could pass a Python dictionary of fruit names, and their corresponding hues to create a Fruit Sorting Machine.
Near the top of the script, define a Python dictionary with the samples you recorded in Example 2.
A Python Dictionary pairs fruit names with their associated hue - found experimentally. You will get the best results if you use results from your own experiments.
Next, modify the call to classify.Hue(), passing in the dictionary as an argument:
If you get stuck, you can copy the full code below:
from PiicoDev_VEML6040 import PiicoDev_VEML6040 from PiicoDev_Unified import sleep_ms fruitList = { "Apple":20, "Carrot":35, "Lime":62 } colourSensor = PiicoDev_VEML6040() while True: ### Example 1: Print Raw RGB Data data = colourSensor.readRGB() # Read the sensor (Colour space: Red Green Blue) red = data['red'] # extract the RGB information from data grn = data['green'] blu = data['blue'] # print(str(blu) " Blue " str(grn) " Green " str(red) " Red") # Print the data. Printing as BGR so the Thonny plot-colours match nicely :) ### Example 2: Classify the colour being shown - eg. a fruit sorting machine data = colourSensor.readHSV() # Read the sensor (Colour space: Hue Saturation Value) hue = data['hue'] # extract the Hue information from data label = colourSensor.classifyHue(fruitList) # Read the sensor again, this time classify the colour print(str(label) " Hue: " str(hue)) # Show the label and the corresponding hue sleep_ms(500)
Run the script and observe that we can now identify different fruits (or other objects) by their colour alone! Below, I hold a Lime then an apple over the sensor and the classification is printed in the shell.
Conclusion
We can measure colour in the RGB and HSV colour spaces, and classify different hues. We can even classify different objects by their hue.
If you make something cool with this starter project we'd love for you to share it on our forums! And if you'd like some help with this guide, start the conversation below - We're full-time makers and here to help!