Sie sind auf Seite 1von 8

RS232

Measuring capacitance and/or resistance

This is a story about the construction of a simple device to measure a capacitor and/or
a resistance. Perhaps you are not interested in measuring the capacitance or
resistance... you need a device to measure a temperature or an amount of light.
Read on!
It is not difficult to translate temperature to resistance... just take a resistor of which
the value is depending on the temperature of its body. This can be a PTC (positive) or
a NTC (negative) resistor.
A PTC rises the resistance at a rising temperature...
A NTC lowers the resistance at a rising temperature...
"THE" resistance is given at typically 25 degrees centigrade.
To load a capacitor (or battery) via a resistor takes time. A high valued resistor drops
the current flowing into the capacitor... so it will take longer to load the capacitor.
If we were able to measure the time needed to load the capacitor than we would know
the value of the resistor and hence the temperature, since the resistance of a NTC or
PTC is temperature dependent.
To measure the time it takes to load the capacitor we can use the computer's RS232
port and a program written to perform this task.
Each RS232 (COMx) port supports 8 active lines, but we do not use the TxD or RxD
for this project. So we have 6 active lines left, 4 for input and 2 for output.
The output lines =DTR & RTS= carry +9 volts if "high" and -9 volts when "low",
maximum current is 10 milli-amperes.

After firing up our computer all the output lines are set low. With our little program
we will make the output line DTR (pin 4) high in order to load the capacitor via a
resistor. The maximum value of this resistor should be 10 kilo-ohms.
One of the input lines (CTS = pin 8) is connected to the capacitor and this line should
detect when the capacitor is 'loaded'. Our program will measure the time between
making DTR high and CTS becoming high.
Of course, all is measured to the ground potential = pin 5 = GND.
This is all folks... well, we need a program for the computer. Let's write one in
QBASIC (or any BASIC dialect).
First we have a decision to make: COM1, COM2, or?
The program is started by calling the COM-port by its Basic Address, BA.
Basic Address = BA
decimal hex
COM1 1016 3FB
COM2 760 2F8
COM3 1000 3E8
COM4 744 2E8
The output lines can be activated by the output register at the address BA+4.
Bit 0 is used for the DTR output and bit 1 for the RTS output.
A "1" sets the output line, makes it high.
A "0" resets the line to low.
DTR RTS total
off 0 off 0 0
on 1 off 0 1
off 0 on 2 2
on 1 on 2 3

**** program part 1 ****


BA = &H3F8 'COM1 (hex)
OUT (BA + 4), 1 'BA + 4 = output, 1 = DTR on = high
************************

The first line of the program makes COM1 active.


The second line sets DTR to high and starts the loading of the capacitor.
Now we must check regularly if the capacitor is filled or still empty. The time interval
of one check is short so we must expect that the capacitor is not full yet... we have to
look again, and again.
A little loop is used to measure the time...
'N' is the total count of the time the program has to look to detect a filled capacitor.
'IN' is the reading of the input line CTS.
IN = 0 means the capacitor is empty.
IN = 16 tells us the capacitor is filled up.

**** program part 2 ****


N = 0
COUNT: N = N + 1 'N = how many times do we look?
IN = INP(BA + 6) 'read CTS input line
IF IN = 16 THEN GOTO FILLED ELSE GOTO COUNT
************************
To read the input lines we have to call the input register at the Basic Address + 6,
BA+6. The value that is echoed by the state of the 4 input lines is the total of all the
input lines counted together... the first four LSB bits are not used.
Bit Name decimal
4 CTS 16
5 DSR 32
6 RI 64
7 DCD 128
"IF IN = 16 THEN GOTO FILLED"
If IN = 16... capacitor C is filled up and then we go to the label FILLED:
"ELSE GOTO COUNT"
If IN is not 16 then we have to count again until IN jumps to 16 => back to the label
COUNT:
Let us measure a capacitor of a known value with a resistor of an also known value.
For 100 micro-farads and 10 kilo-ohms my computer had to count 3138 times =>
N=3138. So if we divide N by 31.38 the result is a capacitor of 100 micro-farads.
The capacitor value can be counted linear so for N = 314 the capacitance is 10 micro-
farads. To calibrate the instrument we need a capacitor of a known value. The value of
the divisor depends on the clock rate of our computer.
31.38 is found for a 100 MHz pentium I.
In practice the accuracy is not very good so 8 measurements are done. However the
counter N is not reset and the total count 'TOT' is divided by 8 at the end in order to
reach an average.

****program part 3****


FILLED: T = T + 1 'we measure 8 times
TOT = TOT + N 'N is added to the total TOT
IF T = 8 GOTO HALT ELSE GOTO BEGIN

HALT: PRINT 'prints an empty line on the monitor


RES = TOT / 8 'RESult = division by 8
PRINT "RESULT ="; RES
CAP = RES / 31.38 'find YOUR divisor
PRINT "Capacitor ="; CAP; "micro-farad"
END
*****************

Part 1 of the program has to be changed since we introduced some new labels,
BEGIN: and START:. T and TOT must be set to zero at the beginning of the program.

****program part 1, revised


BA = &H3F8 'COM1
T=0
TOT = 0
BEGIN: N=0

START: OUT (BA + 4), 0 'DTR low


IN = INP(BA + 6) 'CTS read
IF IN = 0 THEN OUT (BA + 4),1 ELSE GOTO START 'DTR high?

COUNT: 'paste program part 2 here


Results
Determining the capacitance of capacitors ranging from a few micro-farads to about
1200 micro-farads is OK. So in principle this method does the job.
Reading a resistor is difficult since the relation between resistance and the time used
to load the capacitor is of a very non-linear nature *). However it must be possible to
include a reference list into the program giving either resistance or temperature values
to the monitor... or be written to disk.
*) This is caused by the input impedance of the input lines. They should be of very
high impedance but they are not => typical 10 kilo-ohms.
Much better results can be reached by introducing a NE555 timer IC. This makes
things a bit more complicated... but still low level technics. The NE555 will cost you
about half a US dollar.
Measuring capacitance and/or resistance
Part 2 - Using the "555"
The well known 555 (NE555, µA555, ..555) is very often used as an oscillator
(multivibrator). The output frequency is determined by the time it takes to load a
capacitor via a resistor... hence it depends on both the value of the resistor and the
value of the capacitor. The output frequency does not depend on the supplied voltage
to the 555 integrated circuit and has an accuracy of better than 1%.
The 555 is a versatile IC and can be wired as a 'one shot' device instead of an
oscillator... and that is what we need for our measuring instrument.
Let's start with an empty capacitor: the output of the 555 is 'low'. Upon a negative
trigger pulse at pin 2 the IC starts to load the capacitor and the output goes into the
'high' state. When the threshold (pin 6) reaches 2/3 of the supply voltage the output
goes back to 'low' and the capacitor is quickly discharged. Now the 555 is waiting for
the next trigger pulse.
The time it takes to load the capacitor can be calculated with the formula:
T=1,1*R*C. Resistors up to 10 mega-ohms might be used.
A RS-232 has two outputs we can control: RTS and DTR. With RTS we will generate
the trigger pulse and DTR will be used as the power source of the 555 IC. DTR can
deliver a current of 10mA (max) and that is enough to feed this little integrated
circuit.
In our program we first make the trigger 'high' (no trigger) before the power supply is
'on' in order to avoid a false triggering.

OUT(BA + 4), 2 'trigger high


OUT(BA + 4), 3 'trigger & supply high
Now, to start the loading of the capacitor we make the trigger 'low'.
OUT(BA + 4), 1 'trigger low & power supply high
Next the trigger is made high again; but the loading continues.
OUT(BA + 4), 3 'trigger high & power high

The next step, as in the old program, is to peek to see if the capacitor is already
loaded. There is a difference in the state of the output pin of the 555: if C is fully
loaded CTS goes into the 'low' state (was 'high' state)... CTS becomes 0 and not 16.
We have to change this in the new program.
Another change is that there is no need for 8 measurements since the 555 gives a far
better accuracy. The lines that deal with that in the old program are abolished.
The input impedance of the 555 is very high. So we can use a resistor up to 10 mega-
ohms to load the capacitor. I used two resistors, 1.5 kilo-ohms for large capacitors and
1.5 mega-ohms for small capacitors. The switch has been added to choose between
capacitors measured in nano-farads (1nF = 0.001µF) or micro-farads (µF).
The minimum capacity that could be measured was 220pF (0.21 nano-farad on the
monitor). The largest capacity I measured was 0.47 farad (470,000µF) and it took so
much time to load that capacitor that I went for a cup of coffee.

Calibration has been done using an accurate capacitor (1%) of 64.9nF. An accurate
capacitor is expensive and this one was found in my junk box. The value of the
capacitor is of no importance, but take a large one for the sake of accuracy.
Calibrating the micro-farad range was an 'educated guess' since no accurate
electrolitic capacitors were available.

With no capacitor attached to the input the counter still counts to 2. We have to
subtract this in the computer program.
N = N-2

The range switch not only chooses the load resistor and lights the red LED, but also
makes DSR high, adding a value of 32 to the read register "IN".
Capacitor loaded: N=0 or N=32. If N=32, the capacitor's value is printed on the
monitor in nano-farads, if N=0 this will be in micro-farads.
****program com555us.bas****

' Capacity measurements using RS232 and a NE555


' Variabels:
' BA = Basic Address of the COM-port
' N = count of loading time
' IN = value of read input
' OUT= value of combined outputs
BA = &H2F8 'COM2
BEGIN: N = 0
OUT (BA + 4), 2 'RTS high... no trigger
OUT (BA + 4), 3 'DTR hoog... power on

START: OUT (BA + 4), 1 'RTS trigger low + power on


OUT (BA + 4), 3 'RTS high, trigger pulse end + power on

TELLEN: N = N + 1 'Counting load time (loop)


IN = INP(BA + 6) 'CTS reading
IF IN = 0 OR IN = 32 THEN GOTO KLAAR ELSE GOTO TELLEN

KLAAR: PRINT 'blank line


N = N - 2 'Substract 2 for no capacitor at the input
PRINT "Counter = "; N
PRINT "IN ="; IN
IF IN = 0 THEN PRINT "CAPACITY ="; N / 9.4453; "nanofarads"
IF IN = 32 THEN PRINT "CAPACITY ="; N / 11; "microfarads"

' The factor to divide has to be calculated by YOU in the


' calibration proces and depends on YOUR computer.

END
Construction
A piece of double sided copper foil construction board was used. First the 555 was
placed at the side for the pins 6..9 on the board.
 Pin1 : drill a hole and solder the pin at both sides to the foil.
 Pin2 : bend up, no hole, solder 10 kilo-ohm resistor to pin 7 of DB-9
connector.
 Pin3 : solder directly to pin 8 of DB-9.
 Pin4 : drill a hole AND make a trace to pin 8 (under the 555).
 Pin5 : make an 'island' and solder a C of 10nF (SMD prefered) to ground.
 Pin6 and pin7 : drill two holes and make an 'island' to interconnect pins 6 and
7 on both sides of the board. From the islands the two load resistors are
mounted to the switch.
 Pin8 : see pin4.

The two LEDs on the photograph are NOT on the circuit diagram. The green LED is
connected to TxD via a 2.2 kilo-ohm resistor. The polarity of TxD is negative!
The red LED signals the position of the switch and is connected via a second 2k2
resistor to pin 6 of the BD-9 connector.
After we drilled the holes for the 555 into the board we can draw the traces with a
small felt pen. Now the traces can be milled by a 1.2 millimeter mill (ask your dentist
for NEW 'drills') for quick and easy milling.

File or saw the space for the switch and finally clean up the construction board. ALL
non-milled copper foil is ground.
Now solder the components to the right place and if all goes well... this measuring
instrument does its job at the first firing up!

Of course, we could use this instrument with a fixed capacitor and a variable resistor.
Resistors with their resistance related to temperature, the amount of light, moisture or
...
This way a wide range of measurements are possible.

Das könnte Ihnen auch gefallen