Let's measure colour with a PiicoDev Colour Sensor and a Micro:bit. We'll read raw colour data in RGB and HSV, and by the end of this tutorial we'll be able to classify different coloured objects and make a fruit & veggie sorting machine!

Transcript

I'm going to show you how to measure color using a PiicoDev color sensor and a Micro:bit V2. We'll connect these two together and get some example code running so that we can measure both emitted color from a colored light source and reflected color from a colorful object. By the end of this tutorial we'll even be able to identify what kind of colored object we're looking at. Let's do it.

To follow along you'll need a Micro:bit V2, a PiicoDev color sensor and adapter for Micro:Bit. and a PiicoDev cable to connect them together. It's also best to have some colorful objects to measure the color of. I'm using some colorful fruit today.

First, plug your Micro:bit into the adapter and make sure the buttons are facing up. Connect your PiicoDev cable to the adapter and connect the other end to your color sensor. And I've mounted everything to this PiicoDev platform to keep it nice and steady for the rest of the tutorial. Finally, connect your Micro:bit to your computer with a USB lead.

Next, we'll download the example code as well as the drivers for this device. Find the download section of the article and you'll see three links. Right-click each link and select "save link as". I'm saving my files to a PiicoDev folder in my documents.

We're going to use Thonny to program our Micro:bit for this tutorial. If you haven't used Thonny with Micro:Bit. check out our getting started guide for that. And if you prefer not to use installed software, check out our guide for python.MicroBit.org for programming the Micro:bit in your web browser.

Open Thonny and navigate to where you've saved your files and connect to your Micro:Bit. Select the first file with a left click, hold down shift and select the last file, then right-click and upload to Micro:Bit. And we can see under the Micro:bit menu our three files have appeared. Let's open up main and take a look at it. ThisIn this example, we first import the device driver and a function to create a delay. There's a fruit list that we'll discuss later, but we then initialize the color sensor as color sensor. In an infinite loop, we call color sensor dot read RGB, which reads the red, green, and blue components of the light that the sensor detects. We split them up into three variables called red, green, and blue and then print those values in a print statement.

The script is ready to run, so press ctrl d to restart your Micro:Bit. You should see some blue, green, and red data streaming up the shell. You can also see that data in the plot to the right. If you don't see the plot, go to view and plotter to show it.

Light can be measured in terms of red, green and blue. These are the contributions of red, green, and blue light that mix together to create the color of the light that the sensor is seeing. Under the ambience studio lights, we can see that the dominant colors are green and red with a little bit of blue mixed in. That makes sense because green and red creates yellow, and we're in a yellowish light here.

There are three LEDs in red, green, and blue. These have a very pure color. If we shine the red light on the sensor, we can see a big spike on the plot for the red value. We'll see a green spike when we shine the green light on it. Now, if we shine the blue light on it, we can see a blue spike. This RGB measurement is really useful for measuring emitted light like light emitted from these colorful LEDs.

What happens when we hold a colorful object over the sensor, such as this orange? There's not a lot of intuition that this reading gives us. We can see we have some blue, a lot of green, and a lot of red, but there's not much more to it than that.degrees, blue at 240-degrees and so on. So, in this case, yellow is around 60-degrees. With these codes, we can make use of a feature called commenting and uncommenting to choose which parts of the code we want to run. This is really handy if you're trying to test different parts of your code to figure out what's working and what isn't.

In this particular case, we're using the RGB sensor to detect the color of fruits. However, while the RGB mode is useful for measuring colored lights, for things like fruits, we might need to look at the HSV mode, which separates color into hue, saturation, and value.

Hue refers to the nameable colors like red, green, blue, orange, and yellow – something that is especially important for objects like fruits. Saturation measures the strength of the color, while value measures the amount of light, or how bright the light is going to be.

By uncommenting example two and running the code, we can extract the hue of a fruit and use it to classify the fruit's color. The hue is represented as an angle between 0 and 360-degrees, where red is 0-degrees and green is 120-degrees, for instance. In this example, the fruit is classified as yellow and has a hue of around 60-degrees.

Overall, we encourage our customers to make use of the commenting and uncommenting features to test and refine their code, especially when it comes to working with color sensors and detecting the colors of objects like fruits.At 120 and blue at 240. So that means if I hold this apple over the color sensor we have a hue of about 26 and that makes sense because this apple is pretty red and we're getting a hue that's fairly close to zero and it's being classified as red.

So now if I shine the red LED on I have a hue of about 26 with the label red, the green has a hue of about 98 with the label green and the blue has a hue all the way up at about 230 with the label blue. Nice!

So we can really easily use that classify function to tell us what color we happen to be looking at. So this hue classification function measures the hue, finds the hue angle and then returns like a color label of the nearest nameable color like red, yellow, green, blue etc.

Here's the thing, we can give this classify function a custom list of hue and label pairs and that's where fruitlist from before comes in. Fruitlist is a python dictionary that has key and value pairs.

So here we have a key called apple and I'm going to associate that with a hue of about 27 I think I measured. I don't have a carrot but I do have an orange and that orange has a hue of about 45. So I'll change that 60 to a 45 and here I have a nice green lime and that has a hue of about 62. So I'll change that to 62.

Last step is to pass this fruitlist into classify hue as an argument and that is hues equals fruitlist. Finally restart the script with ctrl r and we are now running our custom fruitlist.

If I hold the apple over the sensor we have an apple with a hue of 25. If I hold the orange over the sensor we have an orange with a hue of about 46 and a lime with a hue of about 70.

So now classify hue is taking our fruitlist, it's measuring the hue from the sensor and it's finding the nearest match in the list that we gave it. So there you have it we made a fruit and vegetable sorting machine using nothing but.We just finished putting together a color sensor and running a little experiment to capture those hues to create our fruit list. If you make anything cool from this starter project, we'd love for you to share it over on the Core Electronics forums. That's also the best place if you have any questions.

Until next time, thanks for watching!

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.