Let's measure colour with a PiicoDev Colour Sensor and a Raspberry Pi. 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 going to show you how to start measuring color using the PiicoDev Color Sensor and a Raspberry Pi. We'll connect these two together, get an example working, and by the end of the tutorial, we'll have written a little bit of code that can identify different fruits based on their color alone. Let's get started.

To follow along, you'll need a Raspberry Pi set up to run like a desktop computer. Check out our Raspberry Pi for Beginners workshop for help with that. You'll need a PiicoDev Color Sensor and adapter for Raspberry Pi, and a PiicoDev cable. For Raspberry Pi projects, it's best to have a cable that's 100mm or longer. It's also handy to have a few colorful objects to have a play with. Today, I'm using these colorful fruits.

Plug your adapter onto your Raspberry Pi, making sure that the arrow is pointing towards the Ethernet connector on a Raspberry Pi 4. If you're using a Raspberry Pi 3, this will be the USB connector. Connect your PiicoDev cable to one of the ports on the adapter, and connect the other end to your PiicoDev Color Sensor. And I've mounted everything to this PiicoDev platform to keep it nice and steady for this tutorial.

Once you've powered on your Pi, navigate to the article for this tutorial, and find the Downloads section. Right click the example script, which is called main.py. Right click that link, and select Save Link As. I'm going to save mine to a PiicoDev directory in my Home folder, and I'll rename it to Color.py, just to give it some context.

Open that script in Thonny with a double click, and you should see a window that looks like this. We can install or upgrade PiicoDev by going to Tools, Manage Packages, andSearching for PicoDev. Open that result, and just click the Upgrade or Install button if necessary.

We have our first line, which imports the device driver for this Color Sensor. We also import a sleep function to create a delay. There's something called Fruit List, we'll come back to that later.

But next, we initialize the Color Sensor by calling the initialization function. And then in the while true loop, we call ColorSensor.readRGB. This reads the red, green, and blue components of the measured light and stores it in a dictionary called Data.

We then break out the red, green, and blue components from that dictionary and just save them in new variables to make things a little bit easier later on. And then we call a print statement, which prints the blue, green, and red contributions of that measured light, just as a helpful print statement.

And down the bottom is a sleep for 1000 milliseconds. One second. Hit the green play button to run the script, or use Ctrl R, and in the shell, you ought to see some blue, green, and red data streaming.

In the plot, there is also a plot of that RGB data. If you don't see this plot, just go to View and enable the Plotter setting there.

Here I have some bright red, green, and blue LEDs. Now if I shine the red LED on the Color Sensor, we'll see a big spike in the red channel there. Likewise for green, we'll see a big spike in the green channel, and finally blue. So this readRGB function is really useful for reading colored lights like these.

Now if I hold this apple over the Color Sensor, the LED on board will illuminate the surface and the reflected light will be measured.

Okay, so we've seen some change.In the red, green, and blue values, but it's not really intuitive what's going on here. We don't, how do we figure out that the mix of red, green, and blue is this particular shade of red on this apple?

And you'll also notice that if I move the apple around and bring it further away from the sensor, the values change quite a lot as well. And that's because things like brightness information are locked up in that sum of red, green, and blue. It's not just the ratios of red, green, and blue that make the color, but also the amount of red, green, and blue that describe things like the brightness of that color, or the lightness rather, how much light there is.

So the RGB mode is 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.

And so this brings us to example two. I'm going to highlight all of example one and comment it out using Alt 3. That's the shortcut in Thonny to make a block comment. Then under example two, I'll highlight all the code and hit Alt 4 to uncomment.All of that code. Now when we run the script, we get a label for yellow and a hue, which is just a single number. And this number is an angle between zero and 360-degrees on a color wheel where you have red at zero degrees, green at 120, and blue at 240.

Now I'm sitting under some rather yellowish studio lights, and so we're getting a classified hue of yellow. If I bring these colored LEDs back into the mix, you'll see that when I point the red LED, we get a hue of about 20 and a label for red. Nice. If I point the green, we have a hue of about a hundred and a label for green. And finally, blue gets a hue of about 220 from this LED and a label blue. Nice.

So what's going on in this example? We're calling color sensor dot read HSV. This is different to the read RGB. So this is the read hue saturation value. We store that in a dictionary called data, and then we just extract the hue from that dictionary. Then we call classify hue and we store the result in a variable called label. Finally, we just print that label and print the hue that we measured.

And so this classify hue function is taking that single value for hue and it's returning the nearest label that it has by default, like red, green, blue, orange, yellow, magenta, et cetera. So now if I bring my apple back over, it has a hue of about 25 and it gets the correct label red. This is a red apple. So that's pretty good.

Now what if instead of returning red, green, blue, I wanted to return or classify the names of these fruits like apple, lime, and orange. Well, we can actually pass an argument into classify hue to give it a custom list of colors and hues to classify. And that's where we come.Back to this fruit list. Fruit list is a Python dictionary that associates a key, like the label of a fruit to a value, like its associated hue. So I measured that my apple has a hue of about 25. I don't have a carrot. I have an orange, so I can change that key. And my orange has a hue of about 46, let's say. 46.

And finally my lime, I'll find a nice green spot on it. And that has a hue of about 67. Now I can take the name for this fruit list, copy it, and pass it in as the argument hues to the classify function. And now when I run the script, if I hold my apple over the sensor, we have an apple with a hue of 26. My orange gets classified correctly as an orange with a hue of about 46. And my lime has a hue of about 66, classified as a lime. How good!

We've managed to make a fruit and veggie classifying machine that can identify different objects based on their color alone. If you make something cool out of this example project, we'd love for you to share it over on the Core Electronics forums. That's also the best place if you're looking for any technical help with this project. 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.