You are on page 1of 11


Understanding Color
In reality, color doesn't actually exist - color is simply a figment of your
imagination. No I'm not joking, serious.
So what about blue and green and orange? Figments of your imagination.
Confused? Light, an electromagnetic wave, can come in different wavelengths.
These wavelengths enter your eye and the energy gets absorbed by cells on the
back of your eye. This light energy then gets converted into chemical energy,
gets processed by a few hundred million specialized neurons, and after a few
fractions of a second magically your brain says wavelength 450nm is blue and
wavelength 520nm is green (much more useful information, anyway).
At the conscious level you never actually see these wavelengths, only what
your brain processed them as. Through this process a lot of other wavelengths
get rejected, too, such as infrared or ultraviolet. These wavelengths also exist,
but you just can't see them.
For more information on how your brain can 'see' check out my computer
vision tutorials.
So what's the point of this rambling? Stop thinking of light as something
visible, and start thinking of light as colors you both can and can't see. Green is
a color, but infrared is also a color. Once you realize the color green is no
different than the electromagnetic waves your cell phone uses, then you will
start to understand how a robot really can 'see.' For example, what would the
world look like if you could see infrared but not blue, green, and yellow?

Color of Objects
Again, objects don't actually have any color - its a figment of your imagination.
For example, when you look at a red apple, the apple isn't actually red. Instead,
the molecular surface on the apple reflects wavelengths that appear red in your
brain and absorbs the other wavelengths molecularly so that you can't see them.
Now if your robot wanted to see a red apple, how would it do this? I bet you
would guess, 'Well, I need a sensor that can see the color red.' Wrong!
Actually, you need a sensor that can tell the difference between red and another
color. For example, suppose your sensor can't see red but it can see all other
colors. If this sensor saw nothing, then it was looking at red.
Basic Color Sensor
Suppose you have a sensor that can see many different colors, such as
aphotoresistor. How would you use this sensor to detect red apples vs. green
apples? Well, consider brightness comparisons.
Red apples reflect red light but absorb green light. Green apples reflect green
light but absorb red light.
If you shine a red light (such as from a red LED) on both apples, the red apple
will reflect much more light than the green apple. As such, the apple that
appears the brightest to your sensor will be the red apple. If you shine green
light from a green LED on both apples, the green apple will appear the

Suppose you had some M&M's and you wanted your robot to tell the difference
between the blue, green, yellow, and orange candies. How would you do this?
Well, get a blue LED, a green LED, and a red LED. Then shine each onto the
M&M's, one light at a time, and record the brightness values.
Obviously, the blue M&M's will read the highest brightness values when the
blue LED is turned on, but very low values otherwise. The green and the
yellow M&M's can be detected in a similar manner. So how do you tell the
difference between the yellow and the orange M&M? Well, the orange one is
closer to red in the light spectrum, and as such will reflect more red light than
the yellow one.
But it gets a little more complicated . . . first, photoresistors have different
levels of sensitivity to different colors. Typically photoresistors are optimal and
peak at around 520nm. They are also sensitive to distance away from the target.
As you can see, this particular sensor can detect the most light, and hence get
the best readings, at .12 inches away. This information can be obtained from
the datasheets.

If you move the object to a different distance, or change the angle of the object,
the reading will also change.
And yet it gets more complicated. The ambient light (for example, sunlight
coming in through your window) can change throughout the day. You will need
to shield your sensors from this light.
And even more complicated . . . different color LED's shine at different
wavelengths and different brightnesses, but they also do a spread to where they
can often even share some wavelengths (obtained from the datasheets):

Similarity Matching vs. Thresholding

In reality you must calibrate your sensors before they will work. This means
you must use your sensor(s) to sense the object, record the readings, then make
a chart using this data. That way when your robot is doing its thing and senses
the same object, it can compare the similarity of the new reading vs. the
calibrating reading.
For example, suppose your robot needs to follow a white line on a grey floor.
Your robot would use a microcontroller to sense the analog value from the
sensor. During the calibration phase your robot measured an analog value of 95
for the grey floor, 112 for the white line, and then stored these values in
memory. Now your robot is on the line, and a sensor reads 108. What does that
mean? Is it on the line or not?
Using the thresholding method, you add both calibrated numbers and divide
by two to find the average middle number. For example, (95+112)/2 =
threshold. Anything above that threshold would be the white line, and anything
under would be the grey floor.
But what if you had three or four colors? How do you threshold that? Well,
instead what you would do is called similarity matching. What you do is
determine how similar each color of the object is to the calibrated value.
Staying with our white line example, using similarity matching, you do a little

abs(new_reading - calibrating_reading)/calibrated_reading * 100 =

now using our numbers:
grey floor = (108 - 95)/95 * 100 = 13.7% different
white line = (108 - 112)/112 * 100= 3.6% different
compare: white line < grey floor
therefore the sensor sees a white line
You can use this method for any color and any number of colors, given that you
do a calibration beforehand. Consider calibration as a way of teaching your
robot the difference between colors.
Assembling and Programming a Color Sensor
To detect two colors, of an infinite number of shades, you just need one LED
and one sensor. For example, with infrared emitter detectors you will see a
clear emitter diode (LED) and a black detector diode (phototransistor):

With a photoresistor, here is an example of where I used a green LED with a

photoresistor shielded with black electrical tape:

One advantage to having bright LEDs on your robot for color sensing is that
your robot can look really cool when the lights are off. This is what my
undocumented secret 2006 MOBOT, which used color sensing for line
detection, looked like:

And of course if you wanted three different colors it would look something like

Programming your color sensor is very easy. You simply turn on a LED with a
digital output pin, wait about 50ms for the photoresistor to change
(photoresistors are much much slower than infrared sensors), take a reading
with an analog pin, then turn off the LED (if you have more than one LED).
For example, if your robot has three different color LEDs, this would be your
turn on green LED
wait 50ms
record sensor reading G
turn off green LED
turn on red LED
wait 50ms
record sensor reading R
turn off red LED
turn on blue LED
wait 50ms
record sensor reading B
turn off blue LED
Now using a similarity-matching algorithm, with precalibrated numbers, your
robot can then identify the target.
Range Detection with Shades
What is the difference between bright green and dark green? The only
difference is that with bright green there are more electromagnetic waves being
detected and/or emitted. For example, suppose you have a green apple and your
robot takes a green color measurement. Then you move the apple an inch back

and take another measurement. What will happen? Simple, less green light
from the apple will reach your sensor. So how is this useful? Range detection!
Going back to the previous chart, can you see how you can calculate distance
from an object?

Unfortunately your color range sensor won't have a range of more than a few
inches at most, depending on the brightness of your LED. You could of course
use a green laser pointer for maximum range, or apply a trick I am about to
show you.
This following trick employs the same method TV remotes use to increase
sensor range. Normally, if you apply a large amount of power to an LED to
increase brightness it will fry. But what if you put a huge amount of power into
it but over a very short period of time? Then you could make it say 5x brighter
for ~5x increase in range!
Power (watts) is voltage x current. A typical LED can only work at a few 100
milliwatts before they fry (check your datasheet) - this is why you should
always put a resistor in series with an LED!

Now watts is a measurement of energy divided by time: energy/second. So

what if we had the LED shine for only 50ms? Well, that's 1 / .05 = 20, or 1/20th
of a second. This means that if you shine your LED for only 50ms, then it can
take about 10 to 20 times more current, and therefore shine about 10 to 20
times brighter. The exact numbers would depend on the thermal cooling rate of
the LED - something that can only be determined by frying a few LEDs with
testing . . . Just so you know, its quite common to find half an amp going
through a TV remote IR emitter!
There is yet another method to increasing detectable range of your sensor called
modulation. It's somewhat complex so Ill write about it in another tutorial, but
it requires a very fast sensor. Basically you switch your emitter on/off really
fast so your sensor can therefore ignore background noise. As such you need a
fast sensor. Infrared sensors respond within microseconds, but unfortunately
photoresistors respond within milliseconds (bad!). If you were to do
modulation, it would be better to use IR, such as a Sharp IR Rangefinder.
Unless . . .
Unless you use these neat color sensors that are made by TAOS.

These sensors can be bought for many different specific wavelengths in the
visible spectrum, yet have the frequency response rate of IR sensors! They also
require zero interface electronics - just plug them in to power and analog I/O
and wallah! I've done some experimentation with them, but found the ones I
bought were over-sensitive. In a dimly lit room they already max out in voltage
So this concludes my color sensor tutorial, I hope you learned something!