---
title: "The Colour Sensor"
date: "2021-06-30"
categories: ["All", "Sensor Guides"]
tags: []
excerpt: "Learn what the colour sensor is and how to code it."
featuredImage: "/images/resources/colour-sensor.gif"
---

The colour sensors are one of the five sensor types on the Micromelon Rover. They’re located on the underside of the rover, directly under the ultrasonic sensor. The rover has 3 identical colour sensors.

The Rover is designed to have three sensors to make it possible to attempt more difficult challenges. Line following challenges can be completed with a single colour sensor, but using multiple sensors allow for more comprehensive solution.

**What Do They Do?**

The colour sensors are designed to detect what colour they are currently facing. They are located on the underside of the rover so we can determine what colour the surface is that the Rover is driving over.

![](/images/content/e01b1c-top-pointout.png)

![](/images/content/2b341f-underside-pointout.png)

### What Exactly is Light & Colour?

Before we learn about how the colour sensors work, let’s learn a bit more about what colour is. Colour as we see it on things around us like green in grass and brown in wood is produced by light. The visible light emitted by the sun or a light bulbs reflecting off of objects which makes it possible for us to see what colour they are. Light is actually made up of electromagnetic waves. The science behind electromagnetic waves and how we see them is complicated, for now all we need to know is they are types of light. Electromagnetic waves vary in length, similar to how there are big waves and small waves at the beach. The size of the electromagnetic wave determines what colour that light will be. All visible light we see with our eyes ranges from about 380nm (nanometres) to 750nm. Below is a diagram of the visible light spectrum.

![](/images/content/e333b0-visible-light-spectrum.png)

As we can see in our light spectrum, violets and blues are shorter electromagnetic waves at around 380-485nm and orange and reds have longer wave lengths at around 590-750nm.

**Where is White?**

White is a colour but why is white on the colour spectrum? The reason it’s not there is because white light is actually what we are seeing when we see all coloured light at once. White light is the kind of light emitted from the sun.

![](/images/content/6837dd-red-reflect.png)

**White Light Becomes the Colour We See in Objects**

When white light is produced it travels outwards and hits things in the world. When it hits objects, it is either absorbed or reflected. When a coloured light is reflected off of an object it continues to travel. This reflected light is the coloured light we see from objects. The light that isn’t reflected is absorbed by the object and we can’t see it. A red object is red because it will always reflect red light and absorb other coloured light.

### How Does the Colour Sensor Work?

To detect what colour the colour sensor is facing, all the colour sensor has to do is detect what coloured light is being reflected off of that surface. Each colour sensor has 4 LEDs and a light detector. Let’s breakdown how these components work together to detect surface colour.

![](/images/content/733b94-animation-diagram.png)

![](/images/content/928307-colour-animation.gif)

- The colour sensor LEDs illuminate with white light (white light consists of all coloured light from the visible light spectrum).

- The white light travels down and hits the surface. Some light is absorbed, some is reflected.

- The reflected light travels away from the surface.

- The sensor detects the returning reflected coloured light. ** **

**The colour sensor determines what colour it is facing by detecting what coloured light is reflected off of the surface.**

### Colour Sensor Problems & Limitations

**Light Pollution & Shadow**

When using the colour sensors, you may receive a different value on each sensor from the same surface. This might be caused by shadows moving over the surface or alternative light sources illuminating parts of the surface with a higher intensity of reflected light to the light detector. Unfortunately, light pollution is an unavoidable obstacle when programming a real colour sensor and you should always adjust and calibrate the light values you use for the environment your Rover is in.

**Colour Sensor Range Is Short**

The light sensors work most effectively when the target surface looking to be checked is within 1-1.5cm from the sensor. This matches the height of the sensor from the ground when the rover is placed flat on the surface.

When the sensor is within 1-1.5cm from the target surface the majority of detected light will be that which has reflected off the target surface. The further the sensor is from the target surface the more likely it is light from outside sources will pollute the readings from the light detector. This will lead to inaccurate colour readings.

**Colour Detection Accuracy**

The rover colour sensors are powerful however they are not as accurate as human eyes. Where humans have little trouble differentiating shades of green e.g. dark green and light green; the rover may only identify them as green. This is true for other like colours, e.g. a lighter shade of red and orange may be mistaken for the same colour. When completing colour based activities, its easier to have coloured components with a clear contrast. This is why typical line following challenges have a white background and black line.

### Programming The Colour Sensor

Open the Code Editor, connect to a Micromelon Rover and open up the sensor view. To open sensor view, click the ROVER button next to your rover’s name. For a refresher on the code editor, check out the [Getting Started With The Micromelon Rover](/resources/getting-started-with-the-micromelon-rover) post

In the sensor view dialog, the colour sensor values are located in the top right table. The table has a range of colour values. The top row contains the approximated colour the Rover colour sensors see.

![](/images/content/f16a1c-opening-sensor-view.gif)

![](/images/content/a45d64-colourtable.png)

**Understanding The Values** Unlike the ultrasonic sensor or IR sensor that have one default value, the colour sensors report to use a range of different values we can use in different scenarios. The left, middle and right sensor all return the same type of data. Each sensor has a ** hue, red, green, blue** and ** brightness** values. Let’s breakdown and understand the values that the sensors return.

**Hue**

Hue is a way to define colour based on a range of 0 to 360. This range is based on the 360 degrees of a colour wheel. All colours that humans perceive are represented on a degree of the wheel. But where exactly are black and white? Hue is one component of the HSV (hue, saturation, value) colour model. The saturation and value components of the HSV colour spectrum help us demonstrate black and white. Our Rover only communicates the hue of a colour and not the saturation or value.

![](/images/content/c7544e-hue-circle.png)

**Red, Green & Blue (RGB)**

The red, green and blue values are in the range of 0 to 255. The red values show the amount of red light the sensor detects, blue values for blue light and green values for green light. A sensor facing a pure red surface will show a high red value and low green and blue values. A sensor facing a pure yellow surface will show a high red & green value but low blue value. This follows the logic of the RGB colour model where combining colours creates new colours. You can use the RGB values to determine what colour you are looking at.

![](/images/content/0033cf-rgb-diagram.png)

**Brightness**

Brightness is a value in the range of 0 to 255. The brightness value shows how much white light the colour sensor is currently detecting. Like we’ve talked about previously white light isn’t the same as red, green or blue light. White light is all coloured light combined. A high brightness value indicates the colour sensor is detecting a range of coloured light. A pure white surfaces will always produce a large brightness value.

**Using the Colour Values in Code**

Let’s write a simple program to make our Rover set its LED colour to the colour it currently sees from the middle colour sensor.

![](/images/content/3d1589-programing-the-sensor.gif)

- Start with our **While True** loop as this program will need to be repeated many times in order for the sensor to keep gathering data.

- Let’s get a **Set All LEDs** block from the ** Lights/Sounds** category.

This block takes a colour block. We need a way to translate our colour sensor value to a colour block.

- In the **Colour** category there is a ** Hue Colour** block. This will translate a hue value to a colour block. Place the ** Hue Colour** block in the ** Set All LEDs** block.

- Now we just get a hue value of our middle sensor and attach it to our **Hue Colour** block. In the ** Sensors** category, grab the ** Read Hue From Middle Colour Sensor** block and attach this to the ** Hue Colour** block.

- Press Play!

While this code is running our rover will be changing its LEDs to match the colour it is currently approximating from the middle colour sensor.

### Wrapping Up

Now that we’re familiar with what the colour sensor is, how it works and how to program it, it’s time to start doing some activities. Try these activities that require using the colour sensors.
