Sei sulla pagina 1di 11

PICAXE CAT

1
Author: Jeremy Leach Email: ukc802139700@btconnect.com June 2006
Introduction
This article describes an experimental PICAXE-
based transducer that detects angular displacement of
a shaft. It relies on the variation in capacitance
between 'plates' of aluminium foil, therefore the name
"Capacitive Angle Transducer" or CAT !
Commercial transducers are available that rely on
variation in capacitance and there are a number of
techniques used, often with considerable complexity
(Capacitive Angular Displacement Transducers).
This project is a relatively basic idea compared to
commercial versions, but proves that a basic, cheap
transducer giving 32-Sector resolution seems
possible.
It has proved fascinating to make working capacitors
out of household materials, and there is much more
experimentation that can be done. This project also
demonstrates the power and versatility of the
PICAXE08M chip.
v
ACKNOWLEDGEMENTS
PICAXE is a trademark of Revolution
Education Ltd.
PICAXE Forum http://www.rev-
ed.co.uk/picaxe/forum/
IN THI S ARTI CLE
2 Basic Principles
3 Details of the CAT
3 Measuring Capacitance
5 Real Capacitance Graphs
5 Algorithm to read the Graphs
7 Conclusion
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
2
Figure 1 : Ideal Graph
0 90 180 270 360
Degrees
C
a
p
a
c
i
t
a
n
c
e
Figure 2 : The Base
Figure 3: The Disc
Basic principles
A variable Air Capacitor
An 'Air capacitor' consists of two conducting 'plates'
a small distance apart. The 'dielectric' is air and the
capacitance is represented by this general formula:
C = A/D + C
0
Where:
= Relative-permittivity of the dielectric.
A = area of overlap of the plates.
D = distance between the plates.
C
0
= a constant for the 'stray' capacitance
between the plates and other conductors.
By having plates that rotate, it's possible to make a
variable capacitor, where the capacitance varies
depending on the overlap between the plates. Radio
sets have typically used variable air capacitors, which
have two sets of interleaved plates or 'vanes'.
This is the basic principle behind the CAT. It
measures the capacitance between plates, and
'knowing' the relationship between overlap and
angular displacement, calculates and outputs angle
values.
Unambiguous output
The transducer must generate a unique output for
each unit of angular displacement. A bit of thought
shows that, whatever the shapes of the plates, it's not
possible to have a unique capacitance value for each
angle. In general there will be at least two positions
of the shaft that give the same capacitance value.
One way of getting round this problem would be to
know the starting angle and for the transducer to
'deduce' the most likely angle out of the two or more
choices. This is entirely feasible, although it relies on
the shaft position being sampled rapidly compared to
the rotation speed.
Dynamic duo
My solution uses two variable capacitors. This does
allow a unique transducer output per angular unit.
Figure 1 shows an ideal output of capacitor values
against angular displacement. Each angle has a
unique combination of capacitance values. The
graphs for each capacitor are out of phase by 90
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
3
Figure 4 : Shaft Contact
degrees. The straight-line segments would be
relatively easy to implement in code.
This output is also useful for 'auto-scaling', which is
discussed later.
Details of the CAT
The title picture shows the prototype unit. A disc is
attached to a copper shaft and rotates over a 'base'. I
stuck a computer-generated pattern on the disc
showing sectors to approximately 5.625 degrees.
Figure 2 shows the base, with three separate sectors
of aluminium foil, but actually only two adjacent
sectors being used. The foil is glued to the MDF base
using contact-adhesive, which seems to work well
and produces a very flat foil surface.
Figure 3 shows the disc, which rotates above the
base. I made the disc out of the lid of a CD holder.
The plastic is very brittle to cut but is nice and flat.
The copper shaft is literally a piece of thick copper
wire, carefully rolled until straight. The shaft rests in
a hollow on a small piece of metal sheet, shown in
Figure 4. The shaft is electrically connected to the
disc by a thin wire on top of the disc.
For best operation it's critical to get the disc as close
as possible to the base in order to achieve the
maximum fluctuation in capacitance. It's also
important to avoid any tilt in the disc. I managed to
get a pretty consistent gap of around 1 to 2 mm.
Measuring capacitance
There are two sectors on the base each of 90 degrees,
and a single 180 sector on the rotating disc. This
arrangement gives two independent variable
capacitors.
Figure 5 shows the CAT circuit, with C1 and C2
being the variable air capacitors. Figure 7 shows the
circuit constructed on Strip-board. Figure 8 and
Figure 9 show the Strip-board layout.
The PICAXE generates a 250KHz signal on Output
2, which is fed into the variable capacitors via the
copper shaft. Each variable capacitor has an
associated resistor (C1 and R4; C2 and R7). Each R-
C pair is therefore a high-pass filter.
The output from each filter feeds into a unity gain
buffer in the LM358N dual Op-amp, and then to a
low-pass RC pair which extracts a DC voltage
corresponding to the amplitude of the AC voltage
from the filter. This DC voltage feeds into a
PICAXE ADC input. The PICAXE reads the ADC
values, and translates them to angular displacement
information that is output serially via Output 0.
The frequency of the driving signal and the values of
R4 and R7 have been chosen carefully. A simple
PICAXE program was written that generated a range
of frequencies and captured ADC values while the
variable capacitor C1 was fixed at it's maximum
overlap and R4 = 47K. The response curve is shown
in Figure 6. Note that the PICAXE can only generate
a selection of frequencies, which are shown as steps
in the graph. I've added a smooth trend-line to the
graph.
From this graph the ADC value reaches it's
maximum value when the driving frequency is
around 230KHz. If the driving frequency is fixed at
this value, then variation in capacitance will give a
good variation in ADC values.
250KHz is the nearest value that can be generated by
the PICAXE, so this is the frequency I've used.
R4 and R7 should ideally be metal film to minimise
the effect of temperature. 50ppm per degree is a
quoted figure. The Op-amps have high input
impedance and low output impedance with unity
gain, and so are not expected to show any
temperature-related effects.
The values of R5 and R6 aren't too critical because
they effectively just determine the responsiveness of
the smoothing capacitors C4 and C5 to changes in
DC level. I chose tantalum capacitors for C4 and C5
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
4
Figure 5 : CAT Circuit
0V
O0/
Serout
I/O1
I/O2
+ V
Serin
I/O4
I3
P
I
C
A
X
E
0
8
M
IC1
C3
0.1uF
+
22K
R2
1
0
k
R
1
Download
4
K
7
R
3
R
7

(
M
e
t
a
l
F
i
l
m
)
4
7
K
+ 5V
GND
250KHz Signal
C4
1uF
+
R
4

(
M
e
t
a
l
F
i
l
m
)
4
7
K
+ V
Out B
InB -
InB+
Out A
InA -
InA+
- V
L
M
3
5
8
N
IC2
R
5
1
0
K
C1
C2
R
6
1
0
K
C5
1uF
+
D
1
(
S
h
o
t
t
k
y
)
Figure 6 : Frequency response of R-C combination
Frequency Response
0
50
100
150
200
250
0 20 40 60 80 100 120 140 160 180 200 220 240 260
F KHz
A
D
C

V
a
l
u
e
C1ADC
Poly. (C1ADC)
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
5
Figure 7 : Strip-boarded circuit
Figure 8 : Strip-board topside
GND
I/O1 I/O4
SOut SIn
I/O2 I3
Vcc
IC8
P
i
c
A
x
e

0
8
M
R
3
4
K
7
R
2
2
2
K
R
1
1
0
K
2
1
3
J1
C
3
S
T
1
1
2
C
5
+
R
7
4
7
K
A+ B-
OB
V-
V+ OA
B+
A-
IC5
L
M
3
5
8
N
R
6
1
0
K
R
5
1
0
K
C
4
+
R
4
4
7
K
GND
I/O1 I/O4
SOut SIn
I/O2 I3
Vcc
IC8
P
i
c
A
x
e

0
8
M
R
3
4
K
7
R
2
2
2
K
R
1
1
0
K
2
1
3
J1
C
3
S
T
1
1
2
C
5
+
R
7
4
7
K
A+ B-
OB
V-
V+ OA
B+
A-
IC5
L
M
3
5
8
N
R
6
1
0
K
R
5
1
0
K
C
4
+
R
4
4
7
K
GND
I/O1 I/O4
SOut SIn
I/O2 I3
Vcc
IC8
P
i
c
A
x
e

0
8
M
R
3
4
K
7
R
2
2
2
K
R
1
1
0
K
2
1
3
J1
C
3
S
T
1
1
2
C
5
+
R
7
4
7
K
A+ B-
OB
V-
V+ OA
B+
A-
IC5
L
M
3
5
8
N
R
6
1
0
K
R
5
1
0
K
C
4
+
R
4
4
7
K
Figure 9 : Strip-board underside
GND
I/O1 I/O4
SOut SIn
I/O2
I3
Vcc
IC8
P
i
c
A
x
e

0
8
M
R
3
4
K
7
R
2
2
2
K
R
1
1
0
K
2
1
3
J1
C
3
S
T
1
1
2
C
5
+
R
7
4
7
K
A+ B-
OB
V-
V+ OA
B+
A-
IC5
L
M
3
5
8
N
R
6
1
0
K
R
5
1
0
K
C
4
+
R
4
4
7
K
GND
I/O1 I/O4
SOut SIn
I/O2
I3
Vcc
IC8
P
i
c
A
x
e

0
8
M
R
3
4
K
7
R
2
2
2
K
R
1
1
0
K
2
1
3
J1
C
3
S
T
1
1
2
C
5
+
R
7
4
7
K
A+ B-
OB
V-
V+ OA
B+
A-
IC5
L
M
3
5
8
N
R
6
1
0
K
R
5
1
0
K
C
4
+
R
4
4
7
K
because this type has low leakage current.
Normal temperature variation is not likely to affect
the capacitance of the variable capacitors
significantly. The expansion of the copper shaft etc
should not be too great, and if it was then the shaft
could be shortened. The aluminium in the disc and
base shouldn't expand or contract much at all.
Overall I believe that temperature effects are
therefore minimised. However a significant factor to
try to take account of is the variation in capacitance
with humidity. This is discussed later.
Real capacitance Graphs
Figure 10 shows graphs of the ADC values recorded
for one rotation of the disc. The values were
surprisingly stable at the time of reading them,
fluctuating by only +/- 1 digit. A clear difference
between these graphs and the 'Ideal' of Figure 1 is the
sloping top sections. My explanation for this is that
there is probably a slight tilt to my shaft - but I can't
see it easily!
The lack of 'flat tops' is a pity, but nevertheless these
are useable graphs, which demonstrate unique pairs
of values for each angle of rotation.
Another feature of the graphs is the general
smoothness, which is possibly due to effects at the
edges of the foil plates.
Also, the values fall to zero, or almost zero, when
there is no overlap of foil. This is a good thing and
shows that stray capacitance in the foil, connecting
wires and strip-board tracks is very low. Which
means that the capacitance equation is more like:
C = A/D
This means that changes in the permittivity of the air
due to fluctuations in humidity are likely to increase
the capacitance values in a linear fashion.
Algorithm to read the Graphs
The full code listing is given at the end of this article.
The basic job of the PICAXE software is to take the
pair of ADC values and translate them into angular
displacement. However in addition to this, the
software must also allow for tolerance in the
readings, and fluctuation of the readings due to
humidity changes.
There are a number of methods that could be used to
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
6
Figure 10 : ADC values vs Angle
ADC Value vs Angle
0
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
0 45 90 135 180 225 270 315 360
Angle in Degrees
A
D
C

V
a
l
u
e
C1
C2
achieve this, and I've experimented with a few.
However the programming space on a PICAXE08M
is limited, so whatever method is used has to be
simple and effective.
Translation
I've stored the graphs in 64 bytes of EEPROM as
sequential pairs of readings. When a new pair of
values is obtained, the values are compared to those
on the graph until a matching 'scenario' is found.
What this means in detail is that the reading relating
to Capacitor1 lies between a pair of data-points on
the C1 graph, and the reading relating to Capacitor2
lies between the associated pair of data-points on the
C2 graph.
Tolerance
Because there is slight fluctuation in ADC readings
for a fixed shaft position, a tolerance value is
employed, which allows values to be 'matched' that
fall slightly outside the data-point ranges.
The tolerance value starts small. The graph data is
scanned. If the end of the graph is reached and no
matching scenario has been found, then the graph
data is scanned again with a slightly increased
tolerance.
This process will repeat a number of times until a
match is found, or the tolerance reaches a maximum
value, at which point an error value (Sector = 32) is
generated.
Auto Scaling
Humidity is likely to change the capacitance in a
linear fashion. This will affect the range of ADC
values obtained when the disc is rotated. The ADC
values are not a direct measure of capacitance and
the relationship between how the ADC values
change with capacitance is complex. However as a
fairly good approximation, I've assumed that the
ADC Value graphs scale in a linear way as Humidity
changes. To do anything more complicated is not
easy with the limited code-space available!
The code detects the special condition where the
ADC values are equal (or nearly equal) then
compares this value to the same point on the internal
-graphs, and calculates a 'gain' value to apply to the
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
7
Figure 11 : Flowchart
ADC Values equal
(special condition)?
Initialise
Adjust Gain
Calculate Normalised
ADC Values
Loop
Yes
No
Find Matching
Scenario from
internal Graphs
Output Sector
Sector = 32 (Error)
Tolerance =
MinimumTolerance
Increment
Tolerance
Tolerance
Maximum
Value?
No Match
No
Yes
Match
Read ADC Values
ADC values. There are two shaft positions where the
special condition is met, but only the one with the
higher ADC values is used, in order to get best
granularity of the gain value.
The gain 'normalises' the range of ADC values to
match those of the internal graphs. The gain is
adjusted automatically whenever the shaft is in the
correct position. (In the ideal graph, the auto-scaling
could occur over a whole 90 degree range).
Whether this solution to address humidity variation
really works well is difficult to say as I've only tested
the CAT over a few days, and the automatic
adjustment in gain has been small.
The gain value is held as 'GainNumerator' and
'GainDenominator' bytes in code. This is an effective
way of performing an accurate multiplication using
integer arithmetic.
CAT Output
The CAT outputs values via a Serout command. At
present I read the output via the 'Terminal' program
in the PICAXE Programming Editor. However in a
real application, the output pin could be connected to
other PICAXE chips etc.
The CAT outputs a 'Sector' number from 0 to 31
(which avoids using a Word value for 'Angle'). Also
the gain numerator and denominator value, to
indicate when re-scaling has occurred.
An outline software flow is shown in Figure 11.
Conclusion
This project should be classed 'proof of concept'. It
demonstrates that it's possible to build working
variable air capacitors out of basic household
materials, and it's possible to make a working
transducer with a simple circuit, relying on the power
and versatility of the PICAXE08M chip. It also hints
at further possibilities using home-made capacitors.
The likely accuracy of this transducer is admittedly a
bit questionable. I haven't put it through any thorough
testing, but it seems to work well to 32-sector
resolution.
There are limitations in the design that would need to
be considered if it was used for any serious
application. For instance, the copper shaft simply
rests on a metal 'dent' contact, which is not fantastic.
I've used a small piece of aluminium for this contact
and think I've already experienced some oxidation
problems at the point of contact.
It's also not clear how this transducer could be used
in real-life. A key feature is low shaft-friction with
no mechanical contact with any gears etc.
Optical solutions also have this advantage, although
to make your own optical encoder to 32-sector
accuracy might need multiple sensors and perhaps be
harder to achieve.
v
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
8
PICAXE Code
'***************************************
'* *
'* CAPACITIVE ANGLE TRANSDUCER (CAT) *
'* Version:1 *
'* Author:Jeremy Leach 2006 *
'* *
'***************************************
'**********************
'**** LOOKUP TABLE ****
'**********************
'NOTE: Data is stored in the Lookup Table in a sequence of 32 pairs of
'<Cap1Value>Cap2Value>.
'The first pair in the table is for Angle = 0 degrees,
'the last pair for Angle = 348.75 degrees (11.25 degrees * 31).
Eeprom
0,(169,9,164,19,159,29,152,44,146,59,141,76,136,93,127,118,118,143,94,142,70,142,50,138
,31,135,20,131,10,128,5,122,0,116,0,98,0,81,0,65,0,49,0,36,1,24,3,17,5,10,18,8,32,6,58,
5,85,4,111,4,137,4,153,6)
'**************************
'**** GLOBAL VARIABLES ****
'**************************
'Word0 (b0 and b1)
Symbol Cap1ADCValueLSB = b0
Symbol Cap2ADCValueLSB = b1
'Word1 (b2 and b3)
Symbol TempValue = b2
'Word2 (b4 and b5)
Symbol CheckValue = b4
Symbol Tolerance = b5
'Word3 (b6 and b7)
Symbol SectorIndex = b6
Symbol IsBetween = b7
'Word4 (b8 and b9)
Symbol GainW = w4
Symbol GainNumerator = b8
Symbol GainDenominator = b9
'Word5 (b10 and b11)
Symbol Address1 = b10
Symbol Address2 = b11
'Word6 (b12 and b13)
Symbol Value1 = b12
Symbol Value2 = b13
'*******************
'**** CONSTANTS ****
'*******************
'Pin assignments
Symbol Cap1InputPin = 1
Symbol Cap2InputPin = 4
Symbol PWMOutPin = 2
Symbol ResultOutPin = 0
'Character constants
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
9
Symbol COM = 44 'comma
Symbol RET = 13 'carriage return
Symbol LFEED = 10 'line feed
'Other constants
Symbol MaxSectorIndex = 31
Symbol LookUpTableEndAddress = 63
Symbol Yes = 1
Symbol No = 0
Symbol C1C2HighCrossover = 125
Symbol ErrorSector = 32
Symbol MinimumTolerance = 2
Symbol MaximumTolerance = 10
Symbol DifferenceThreshold = 3
Symbol DoubledDifferenceThreshold = 6
'***********************
'**** RAM POINTERS *****
'***********************
'*********************
'**** INITIALISE *****
'*********************
Pwmout PWMOutPin,3,8 'Sets a 250KHz signal with 50:50 Duty
GainW = 257 'Set Gain Numerator and Denominator both to 1.
Pause 1000 'To let the smoothing capacitors charge
'************
'*** MAIN ***
'************
GetAndTransmitSector:
'READ THE ADC VALUES
'Although ReadADC10 is used, only the lower byte
'is relevant as the ADC values do not exceed 255.
Readadc10 Cap1InputPin,Cap1ADCValueLSB
Readadc10 Cap2InputPin,Cap2ADCValueLSB
'CHECK FOR 'HIGH CROSSOVER' SCENARIO
TempValue = Cap1ADCValueLSB - Cap2ADCValueLSB + DifferenceThreshold
If TempValue > DoubledDifferenceThreshold Or Cap1ADCValueLSB < 40 Then GATS_2
'ADJUST GAIN
GainNumerator = C1C2HighCrossover
GainDenominator = Cap1ADCValueLSB
'NORMALISE THE ADC VALUES
GATS_2:
Cap1ADCValueLSB = Cap1ADCValueLSB * GainNumerator / GainDenominator
Cap2ADCValueLSB = Cap2ADCValueLSB * GainNumerator / GainDenominator
'FIND SECTOR
'Scan the Lookup table for a scenario that fits the normailised ADC
'values and return the associated SectorIndex.
'If a Scenario can't be found then set SectorIndex = ErrorSector
Tolerance = MinimumTolerance - 1
FS_1:
Tolerance = Tolerance + 1
For SectorIndex = 0 To MaxSectorIndex
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
10
'Check if Cap1ADCValue is between the indexed Cap1 data points from the
'Lookup Table.
CheckValue = Cap1ADCValueLSB
Address1 = SectorIndex * 2
Gosub CheckIfBetween
If IsBetween = No Then FS_2
'Check if Cap2ADCValue is between the indexed Cap2 data points from the
'Lookup Table.
CheckValue = Cap2ADCValueLSB
Address1 = Address1 + 1 'Note Address1 is not corrupted by CheckIfBetween.
Gosub CheckIfBetween
If IsBetween = Yes Then GATS_3
FS_2:
Next
'Try relaxing the tolerance
If Tolerance < MaximumTolerance Then FS_1
'If even this doesn't work then report error .
SectorIndex = ErrorSector
GATS_3:
'OUTPUT SECTORINDEX
Sertxd
(#SectorIndex,COM,#GainNumerator,COM,#GainDenominator,COM,#Tolerance,RET,LFEED)
Goto GetAndTransmitSector
CheckIfBetween:
'Checks if CheckValue lies between Value1 and Value2 (inclusive of
'Value1, and Value2).
'ON ENTRY: Value1 is held at Address1 and Value2 at Address1 + 2.
'ON EXIT: IsBetween is set to Yes or No.
Read Address1,Value1
Address2 = Address1 + 2
'Loop Address2 back if necessary.
If Address2 <= LookUpTableEndAddress Then CIB_1
Address2 = Address2 - LookUpTableEndAddress - 1
CIB_1:
Read Address2,Value2
'Extend the range depending on the Tolerance constant.
If Value1 >= Value2 Then Extend_2
Value1 = Value1 - Tolerance
Value2 = Value2 + Tolerance
Goto Extend_3
Extend_2:
Value1 = Value1 + Tolerance
Value2 = Value2 - Tolerance
Extend_3:
'If the values are < 0 then make them 0.
PDF created with pdfFactory trial version www.pdffactory.com
PICAXE CAT
11
Value1 = Value1 / 240 Xor 1 * Value1
Value2 = Value2 / 240 Xor 1 * Value2
'Check if the CheckValue is between Value1 and Value2.
CIB_2:
IsBetween = No
If CheckValue = Value1 Then CIB_5
If CheckValue > Value1 Then CIB_3
If CheckValue >= Value2 Then CIB_5
Return
CIB_3:
If CheckValue < Value2 Then CIB_5
Return
CIB_5:
IsBetween = Yes
Return
PDF created with pdfFactory trial version www.pdffactory.com

Potrebbero piacerti anche