Let's measure colour with a PiicoDev Colour Sensor and a Raspberry Pi Pico. 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!


G'day, I'm gonna show you how to get started detecting color using a PiicoDev color sensor and a Raspberry Pi Pico.

We'll connect these two together, get some example code running to identify colors, and then we'll even remix the code to make a fruit and veg sorting machine. Let's do it.

To follow along, you'll need a Raspberry Pi Pico with the pin solid facing down, a PiicoDev color sensor and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect everything together. Also handy to have lots of nice colorful objects to test the sensor with. And I also have some colorful LEDs just to help out.

Plug the Pico into your expansion board, making sure the USB connector is on the same side as the two-pin battery connector. Plug the PiicoDev cable in at the bottom and connect your sensor to the other end. And I've just mounted everything to a PiicoDev platform to keep it nice and stable for the rest of the tutorial.

Finally, connect your Pico to your computer with a USB lead. We're going to use Thonny for this tutorial. If you've never used Thonny with the Raspberry Pi Pico before, check out our guide for that.

In the article for this tutorial, find the download section and right-click each of the three links and select save link as. These are the source files for this tutorial. It makes sense to save them somewhere that makes sense. I'm putting mine in a PiicoDev directory in my documents.

Open Thonny, connect to your Pico and upload the three files. Select the first file, hold shift and select the last one with a left click, then right-click and upload to.

And we can see in our connected Pico that we have the three files. Main is our demo code and the other two files are just drivers for this sensor. When we open up main, we can see what the script will do.

I'll open it from the Pico. This is our example script and it'sReady to run. So press control D to reboot your Pico and immediately you should see in the shell three values. One for blue, green and red coming up in the shell. If you have the plotter open, you'll also see a red, green and blue line that correspond to those colors. And I'm in a bright studio, so we already have some quite high values.

I have here three really bright LEDs in red, green and blue, which are the same as the three channels of light that this color sensor can detect. So if I point each LED at the color sensor in turn, I should be able to affect the values that we see in the plot.

Here's the blue LED. Oh yeah, and that blue line has just shot up. Here's the green. There's the green line. And finally the red. So we can see there in the plotter, exciting each of the red, green and blue channels in sequence.

This onboard LED is for illuminating colorful objects to measure the reflected light that comes back. Now for situations where you want to measure emitted light, like from these LEDs, you might want to connect a wire between ground and LED, and that will disable the LED.

Let's take a look at the example code a little closer to see what's going on. We first import the color sensor driver, and we also import a sleep function to create a delay. There's something called fruit list. We'll get back to that in a moment, but we next initialize the color sensor as an object called color sensor.

Then in the infinite loop, we call color sensor dot read RGB to read the RGB values of light coming in. And we store that in a dictionary called data. And just to make the following print statement a little bit cleaner, I access data red and store that in just a variable called red and the same for green and blue.

Finally, in the print statement, we take that blue value, convert it to a string and concatenate it with blue.And then we do the same with green and the same with red. And that's how we create this formatted print statement where we have the value followed by the color right after it.

Now this is just one color space called RGB where there are measuring different intensities of red, green, and blue light. What that means is the light intensity information is kind of wrapped up in the color information.

Here's what I mean. If I hold this orange over the sensor, okay, we get some red, green, and blue values, but who's to say that those mixtures are orange? Like it's not very intuitive. You'll also note that as I move the orange up and down, it actually changes all of those values. So the RGB mode really useful for measuring those colored lights, but for things like this fruit or like some other colored samples, we might need to look at HSV or hue, saturation, and value.

HSV separates color into hue, saturation, and value. Value is the amount of light, how bright that light is going to be. A very low value would be completely black and a very high value would be really, really bright, like blinding bright. The saturation is how strong the color is. You can almost think of saturation as a scale from pure color all the way down to gray.

What we're most interested in though is hue and hue is the type of color, nameable colors like red, green, blue, orange, yellow. That's what we're interested in for objects like these fruits.

Let's work on that color classification and see if we can tell the difference between these three colorful fruits. I'll comment out the print statement and I'll uncomment everything under example two. So I'm using alt four to remove those hashes, to uncomment that whole block. Example two is going to use hue, saturation, and value or HSV and just extract that hue information, the type of color that we're looking at. So.We can see we assign the HSV values to a variable called data, and then we extract the hue from that dictionary data and store that in a variable called hue. We also call a classifying function to classify that hue and store that label. And that will be by default, that's red, green, blue, magenta, et cetera. We print that hue that we measured first and we also print the label.

I'm gonna press control R to just run it straight away. And here's what I mean. We have a hue value. And at the moment, the light being received by the sensor has a hue value of 67. Hue can be a value between zero and 360-degrees and that's on a color wheel. And so the light under these studio lamps, it's pretty yellow. And our classifier function has labelled the light as yellow.

Now what happens when I cover the sensor with this apple? There we have it. The hue is about 20 and the label has changed to red. And that means that for a hue of 20, the nearest predefined label is red. So I know the hue of my apple is 20. I'm gonna scroll up to this fruit list that we saw earlier and just include 20 here. So now we have the value 20 associated with the key apple.

See where this is going. I'll do the same with my orange. And that has a hue of about 36. I don't have a carrot, but I have an orange. The hue of 36. And my lime has a hue of about 70.

Just for completeness, we'll return to those LEDs. We can see the blue LED has a hue of about 235 and it gets labelled blue. The green LED has a hue of about 100, labelled green. And the red LED has a hue of about 27. And of course it's labelled red.

Now that we've constructed this fruit list, we can actually pass it into the classify hue function as an argument. We can pass in predefined hue label pairs so that we can use classify hue to not just say red, green, blue, but to say apple, lime, orange. Let's give it a go.Hues equal fruit list. I'll press Control R to run.

And here with a hue of 23 is an apple. Here's an orange with a hue of 43. And here's a lime with a hue of 75. How good is that? We were so easily able to make a fruit and veggie sorter just by predefining a few hue label pairs and putting them in the classify hue function. So easy.

It's probably best that you run your own experiments and use your own hue values rather than just using mine to be most reliable. And of course, this list could be as long as you want.

So the way this classify hue function works is it measures the hue as that numeric angle between zero and 360-degrees. And then it looks inside the list that you pass it to find the nearest neighbor. So here, if you were at 25, well, the closest number to that is 20 and the key associated with 20 is apple. So you get a return of apple.

There you have it, a fruit and veggie sorter using the PiicoDev color sensor and a Raspberry Pi Pico and a few pieces of colorful fruit.

If you make something cool out of 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.



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.