This is only a preview of the June 2005 issue of Silicon Chip. You can view 39 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "The Coolmaster Fridge/Freezer Temperature Controller":
Items relevant to "PICAXE Colour Recognition System":
Items relevant to "PICAXE In Schools, Pt.2":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
A radio-control (RC) servo can be added
to the colour sensor unit for a bit of
fun. Here, the servo arm is used as a
pointer to indicate which sweet is under
the sensor. The dial is made from a
CD-ROM and the servo itself is simply
connected to the servo driver PC board.
PICAXE
COLOUR
Recognition System
Use a PICAXE micro and a state-of-the-art
optoelectronic IC to create this low-cost
colour recognition system.
By CLIVE SEAGER
A
LTHOUGH COLOUR recognition systems are not new, until
recently they would have been far too
complex and expensive to feature in
a PICAXE project. Texas Advanced
Optoelectronic Solutions (TAOS)
from Plano, Texas, have changed all
that with their new line of low-cost
colour light-to-frequency (LTF) converter ICs.
The TAOS TCS230 LTF converter
was selected for this project because
it integrates all of the functions neces70 Silicon Chip
sary for colour sensing into a single
miniature 8-pin package, including a
digital output for easy interfacing to
our PICAXE microcontroller.
The project consists of two individual PC boards that plug together
to form a complete colour recognition
system. The first of these is the Colour Sensor Module, which includes
the TCS230 sensor and a handful
of support components. Revolution
Education is supplying this board
preassembled, as the TCS230 is only
available in a tiny surface-mounted
(SOIC) package that would be difficult
to solder by hand.
The second PC board is a PICAXE08M Servo Driver. Although primarily
designed to control servos in robotic
projects, this board is also suitable for
use with the Colour Sensor Module.
In this article, we’ll describe how
to assemble the Servo Driver board, as
well as how to connect it to the Colour
Sensor Module to build a complete
colour recognition system. Naturally,
we’ll also show you how to program
it to recognise colours!
TAOS TCS230 colour sensor
What makes the TCS230 sensor
unique in the optoelectronic world
is the integration of the light sensing,
signal conditioning and analog-todigital conversion (ADC) functions in
siliconchip.com.au
Fig.1: the complete circuit diagram for the Colour Sensor Module. 33kW resistors set the default conditions (high
or low) for the sensor’s input pins. Information on the function of the S0 & S1 inputs (here set for 100% relative
frequency scaling using two 33kW pullup resistors) can be obtained from the TCS230 datasheet. A Mosfet (Q1) is
used to switch the LEDs via the “L” signal on the connector.
Fig.2: there’s even less to the Servo Driver circuit. As the sensor’s interface is digital, it is connected directly to
the PICAXE port pins (via the 10-way header). A series diode (D1) reduces the 4-cell battery pack voltage to a
safe level (5.4V nominal) to power both the Servo Driver and Colour Sensor boards.
a single IC. The output from the sensor
is a square wave of a frequency that is
directly proportional to light intensity
(irradiance). This can be connected directly to a microcontroller, thereby enabling extremely simple, cost-effective
siliconchip.com.au
light sensing solutions.
So how can the TCS230 be used to
differentiate one colour from another?
Well, the sensor includes an array of 64
light sensors (photodiodes), organised
in an array of 8 x 8. In all, 16 photo-
diodes have blue filters, 16 have red
filters, 16 have green filters and 16 are
clear (no filters). All 16 photodiodes
with the same filter colour are connected in parallel, with only one of
the colours (red, green, blue or clear)
June 2005 71
Fig.3: the TCS230 sensor is
contained in a tiny 8-pin surfacemount package. The package is
manufactured from a transparent
material, allowing light to reach
the photodiode array.
can be gained simply by counting
the number of pulses over a given
sample period (eg, 50ms). This can
be achieved with the PICAXE-08M’s
count command.
The process for measuring the RGB
light intensity from a sample can be
simplified as follows:
(1). Select red filters (S2=0, S3=0)
(2). Count pulses for sample period
(result = red value)
(3). Select blue filters (S2=0, S3=1)
(4). Count pulses for sample period
(result = blue value)
(5). Select green filters (S2=1, S3=1)
(6). Count pulses for sample period
(result = green value)
How accurate is it?
enabled at any one time.
Two digital control lines are provided so that external devices (such
as our PICAXE) can select between the
four arrays. By enabling each of the
arrays (colours) in turn and measuring
the proportional light intensity falling
on the sensors, a good approximation
of the red-green-blue (RGB) content
of the light source can be established.
As white light is composed of these
three primary colours, it’s a relatively
simple task for the microcontroller
to differentiate any colour across the
spectrum.
Selection of each of the photodiode
arrays is achieved via the S2 and S3
digital input pins, as defined in Table
1. The output of the sensor is a square
wave with the frequency directly proportional to the light intensity. Therefore, a reliable indication of intensity
Most colour sensors vary in accuracy across the RGB spectrum and
the TAOS TCS230 is no exception. In
theory, the three RGB sensors should
record an equal value for pure white
light but in practice, they don’t.
This problem can be addressed by
performing a white balance test. Its
purpose is to calculate a scaling factor
to apply to each colour to correct the
error. In the case of a video camera, for
example, this makes the playback of
the colours more accurate on a television screen.
However, with a simple microcontroller colour detection system, it isn’t
necessary to scale the readings, as
we won’t be reproducing the colours
elsewhere. We are simply interested
in a threshold point for each colour.
Therefore, if we account for the imbalance within the thresholds set for each
colour by experimentation, no white
Fig.4: the Colour Sensor Module is supplied
preassembled but we’ve provided the overlay
diagram here for reference. All you have to
do is fit the two LEDs (and their “posts”) as
shown in the photo above right. Links J1-J4
are left open for this project.
72 Silicon Chip
Table 1: Filter Selection
S2
S3
Filter
0
0
Red
0
1
Blue
1
0
None
1
1
Green
balance test is required. However, for
more advanced applications, a white
balance test can easily be added to the
program if required.
Note that background lighting conditions and distance from the sample
will also make a small difference
to the readings, so you may need to
recalibrate the sensor when moving
its position.
Colour sensor module
As well as the TCS230 sensor IC, the
Colour Sensor Module also includes
its own light source in the form of two
white LEDs. These are angled at 45° to
provide a point of light to illuminate
the sample. The reflected light is then
focused by a small lens (as typically
used in CCD cameras) onto the TCS230
chip. The lens also filters out unwanted
background infrared light.
The full circuit diagram for this
module is shown in Fig.1 and the
board overlay in Fig.4. These are included mainly for reference, as apart
from the two LEDs, the board is supplied preassembled.
Instructions supplied with the kit
show how to install the LEDs. The
most important point to remember
is that the cathode (K) side of a LED
is identified by its shorter lead and a
“flat” side on the housing, as indicated
on the circuit and overlay diagrams.
In addition, the LED must be threaded
into the right-angle post so that it will
be angled towards the centre of the
board when installed in the PC board
holes.
When testing the assembly later,
note that you should check to make
sure that the two LEDs are correctly
aligned. Ideally, their light output
should merge to produce a single
light dot at a focal length of about
30mm from the PC board. If you have
a “figure-8” light pattern instead, try
tweaking the angle and position of the
LEDs slightly.
As the LEDs consume considerable
power in comparison to the other
parts of the circuit, battery life can
siliconchip.com.au
Fig.5: follow this diagram when assembling the
Servo Driver board. In particular, check that
you have the 33mF capacitors, diode (D1) and
microcontroller (IC1) around the right way. The
10-way socket mounts on the opposite side of the
board to the other components.
be maximised by switching the LEDs on only when a
“scan” is to take place.
PICAXE control of the sensor
As presented here, control of the Colour Sensor Module
requires only three outputs and one input of a PICAXE
microcontroller. A suitable circuit could be constructed
on a prototyping board but a better way is to use a
PICAXE-08M Servo Driver board for the job. This board
includes a 10-way dual-row header socket to mate with
the header on the Colour Sensor Module.
Fig.2 shows the circuit diagram for the Servo Driver
board. Output 0 of the PICAXE-08M controls the LEDs
on the Colour Sensor Module, whereas outputs 1 & 4
connect to the TCS230’s S2 & S3 inputs to select the
desired photodiode array (colour). Input 3 is connected
to the TCS230’s frequency output.
On the '08M chip, this leaves only one output (output
2) available for other uses. Naturally, if you require more
input or output pins for a project, then the circuit and
program is easily ported to the PICAXE-18X or 28X. If
desired you could also leave the white LEDs permanently
on, freeing up a PICAXE pin for use elsewhere (eg, connected to a pushbutton switch to activate sensing).
Note that this board requires 6V (4 x AA cells) instead
of the more usual 4.5-5V supply. This higher voltage is
needed because typical RC servos require at least 6V in
order to generate useable amounts of torque. A series
diode (D1) drops the rail down to about 5.4V to power
the PICAXE micro and the Colour Sensor Module,
Table 2: Sample RGB Values
Sweet
Red Value
Green Value
Blue Value
blue
green
0<w4<50
50<w6<150
200<w5<350
0<w4<50
200<w6<300
100<w5<200
red
50<w4<100
20<w6<80
20<w5<100
yellow
150<w4<250
230<w6<350s
80<w5<120
siliconchip.com.au
June 2005 73
Program Listings
Listing 1
'******************************************************
' PICAXE-08M input/output pins
symbol LED = 0
symbol S2 = 1
symbol ser = 2
symbol CSI = 3
symbol S3 = 4
'colour sensor white LEDs (output 0)
'colour sensor select S2 (output 1)
'servo or serial LCD (output 2)
'colour sensor pulse (input 3)
'colour sensor select S3 (output 4)
'******************************************************
' Variables (w4-w6 uses b8-b13!)
symbol red_value = w4
symbol blue_value = w5
symbol green_value = w6
'colour sensor red content
'colour sensor blue content
'colour sensor green content
'******************************************************
' Scan and display every second
main:
gosub colour
'scan the colour
sertxd ("Red =", 9, #red_value, 9)
sertxd ("Blue =", 9, #blue_value, 9)
sertxd ("Green =", 9, #green_value, CR, LF)
pause 1000
goto main
'******************************************************
' Sub to scan colours
colour:
high LED
low S2
low S3
count 3, 50, red_value
high S3
count 3, 50, blue_value
high S2
count 3, 50, green_value
low LED
return
'LED on
'read red into w4
'read blue into w5
'read green into w6
'LED off
Listing 2
servo 2, new_pos
pause 1000
goto main
'move the servo
'***********************************************
' Sub to scan colours
colour:
high LED
low S2
low S3
count 3, 50, red_value
high S3
count 3, 50, blue_value
high S2
count 3, 50, green_value
low LED
return
'LED on
'read red into w4
'read blue into w5
'read green into w6
'LED off
'***********************************************
' Sub to evaluate colour and then set the servo position
evaluate:
new_pos = 190
'preload reject position
' Now identify correct colour using the threshold values
if red_value > 150 and red_value < 250 then test_yellow
if red_value > 50 and red_value < 100 then test_red
if red_value < 50 then test_blue_or_green
return
test_blue_or_green:
if blue_value > 200 and blue_value < 350 then test_blue
if blue_value > 100 and blue_value < 200 then test_green
return
test_blue:
if green_value > 50 and green_value < 150 then is_blue
return
is_blue:
new_pos = 170
return
main:
gosub colour
'scan the colour
serout 2,N2400,(254,128,"R=",#red_value, " ")
serout 2,N2400,(254,136,"B=",#blue_value, " ")
serout 2,N2400,(254,192,"G=",#green_value, " ")
pause 1000
goto main
test_green:
if green_value > 200 and green_value < 300 then is_green
return
Listing 3
test_red:
if blue_value > 20 and blue_value < 100 then test_r2
return
'******************************************************
' PICAXE-08M input/output pins
symbol LED = 0
symbol S2 = 1
symbol ser = 2
symbol CSI = 3
symbol S3 = 4
'colour sensor white LEDs (output 0)
'colour sensor select S2 (output 1)
'servo (output 2)
'colour sensor pulse (input 3)
'colour sensor select S3 (output 4)
'******************************************************
' Variables (w4-w6 uses b8-b13!)
symbol new_pos = b1
symbol red_value = w4
symbol blue_value = w5
symbol green_value = w6
'new servo position
'colour sensor red content
'colour sensor blue content
'colour sensor green content
'******************************************************
main:
gosub colour
'scan the colour
gosub evaluate
'set the servo position
74 Silicon Chip
is_green:
new_pos = 90
return
test_r2:
if green_value > 20 and green_value < 80 then is_red
return
is_red:
new_pos = 145
return
test_yellow:
if blue_value > 80 and blue_value < 120 then test_y2
return
test_y2:
if green_value > 230 and green_value < 350 then is_yellow
return
is_yellow:
new_pos = 120
return
siliconchip.com.au
Fig.6: the output from the test program,
as it appears in the Programming
Editor’s serial terminal window.
which have a maximum input voltage of 5.5V.
Assembling the servo driver
Assembly of the PC board is very
straightforward. Install the low-profile
components first, starting with the
resistors. When installing the two
33mF tantalum capacitors, make sure
that you have their positive (+) leads
oriented as shown (see Fig.5). Also,
take care that you have the PICAXE08M (IC1) around the right way; the
notched (pin 1) end must be next to
the programming socket.
Leave the 10-way socket until last.
It must be mounted on the opposite
side of the board to all the other components (see photos) so that it can mate
with the corresponding header on the
colour sensor module.
Once assembly is complete, fit 4 x
12mm threaded spacers in each corner
mounting hole using the supplied
M3 x 6mm screws. The colour sensor
module can now be plugged into the
servo driver’s socket and a further 4 x
30mm spacers fitted as legs to support
the whole assembly. You should end
up with a “tower”, as shown in the
lead photograph.
Testing
To check that your completed unit
is working properly, a simple program
can be run to “learn” the reflected light
properties of various samples. We’ve
used some small sweets as samples
but you can use what ever you have on
hand. Note that you may need to adjust
the height of the tower to compensate
for the height of your samples.
The test program is shown in Listing
1. You can type this into the Programsiliconchip.com.au
ming Editor directly or download it
from the SILICON CHIP website at www.
siliconchip.com.au.
Download and run the test program
in the PICAXE-08M and then go to the
terminal menu (via PICAXE -> Terminal from the toolbar) and choose a baud
rate of 4800. The terminal window will
then display the RGB data being output
via the sertxd command.
If you wish to perform the testing
away from your computer, you can
use a serial LCD module (Part No.
AXE033) connected to output 2. Listing 2 shows the “main” section of the
program altered to support a serial
LCD module.
Par t s Lis t For
AXE024 Servo Driver
Colour identification
Capacitors
2 33mF 16V tantalum
1 100nF MKT polyester
Table 2 shows a list of values for
each of the sweet colours that were
determined by experimentation. Note
that we’ve used very broad thresholds
to allow for the variations seen with
even slight movements of the sweets
from measurement to measurement.
Regardless of the broadness of our
figures, the important point is that
each colour of sweet can be uniquely
identified from these values.
The third program adds a radiocontrol type servo for a bit of fun.
The servo arm is used as a pointer
to indicate which sweet is under the
sensor. A simple dial is made from a
blank CD-ROM, which is then placed
under the servo arm. The servo itself
is simply connected to output 2 of the
PICAXE chip.
This time, the program is a little
more involved as it has to determine
which sweet is which colour using
mathematical comparisons. This is
achieved by testing the threshold
values for each of the RGB values in
Table 2.
Summary
The TCS230 light-to-frequency
1 Servo Driver PC board
1 3.5mm stereo socket
1 battery clip
1 4 x AA battery holder
1 8-pin IC socket
1 10-way 2.54mm (0.1-inch)
SIL header (separate into 3 x
3-way headers)
Semiconductors
1 PICAXE-08M microcontroller
(IC1)
1 1N4001 diode (D1)
Resistors (0.25W 5%)
1 22kW
1 10kW
3 330W
Also required (not in kit)
PICAXE Programming Editor
software (v4.1.0 or later)
PICAXE download cable (Part
No. AXE026)
4 x AA alkaline cells
1 Colour Sensor Module (Part
No. AXE045)
1 10-way 2.54mm (0.1”) pitch
DIL header socket
Mounting hardware (standoffs,
screws)
converter is an economical solution
for many colour-sensing projects. It
is easily interfaced to a PICAXE microcontroller and is a versatile sensor
that can be incorporated into many
mechatronic and robotic applications.
More information on TAOS sensors
can be obtained from their website at
SC
www.taosinc.com.
Obtaining Kits & Software
The design copyright for this project is owned by Revolution Education Ltd.
The Colour Sensor Module can be purchased individually (Part No. AXE045)
or as part of a kit (Part No. AXE112S). The kit includes a PICAXE Servo Driver
kit (Part No. AXE024), a 10-way header socket and the mounting hardware
necessary to allow the boards to be stacked together. All items are available
from authorised PICAXE distributors – see www.microzed.com.au or phone
Microzed on (02) 6772 2777.
The PICAXE Programming editor software can be downloaded free of
charge from www.picaxe.co.uk or ordered on CD (Part No. BAS805).
June 2005 75
|