Measuring Resistance Using Digital I/O
Measuring Resistance Using Digital I/O
com
Measuring Resistance Using Digital I/O
Using a Microcontroller for
Measuring Resistance Without using an ADC.
Copyright 2011 John Main
http://www.best-microcontroller-projects.com Page 1 of 10
http://www.best-microcontroller-projects.com
Table of Contents
Legal........................................................................................................
Resources.................................................................................................!
"ssential "#ine........................................................................................!
$%C C Course..........................................................................................!
$%C %nterrupt &ecrets...............................................................................'
&tate Machine &ecrets.............................................................................'
Measuring Analogue $ara(eters With )o ADC ...............................................*
Measuring A Resistance...........................................................................*
$C Joystic+ Metho,..................................................................................-
Cali.ration............................................................................................./
Con0ersion............................................................................................./
Applie, to the $%C.................................................................................../
1peration..............................................................................................2
R&22 &ettings.....................................................................................10
R&22 output.......................................................................................10
De0ice.................................................................................................10
Co(piler..............................................................................................11
Co,e Do3nloa,.....................................................................................11
http://www.best-microcontroller-projects.com Page 2 of 10
http://www.best-microcontroller-projects.com
Rights
NOTICE: You DO Hae the !i"ht #ie $wa% This !eport&
$s lon" as %ou 'on(t chan"e an%thin"& use )*$+ or "enerall% 'o
somethin" %ou(re not suppose' to 'o.
, Cop%ri"ht -.// 0ohn +ain
http://www.best-microcontroller-projects.com
$ll ri"hts resere'. No part o1 this e2oo3 ma% be repro'uce'& store'
in a retrieal s%stem or transmitte' in an% 1orm or b% an% means
electronic& mechanical& photocop%in"& recor'in" or otherwise without
e4press written& 'ate' an' si"ne' permission 1rom the author 0ohn
+ain.
Legal
DISCLAIMER AND/OR LEGAL NOTICES:
4er(s of use5
$ll so1tware& har'ware an' in1ormation 1oun' in this eboo3 or
associate' with it shall be herea1ter re1erre' to as the pro'uct. $ll
persons usin" or actin" on the pro'uct shall be herea1ter re1erre' to
as the user.
2% usin" the pro'uct the user a"rees to abi'e b% the 1ollowin"
con'itions.
Disclaimer:
$ll pro'ucts are proi'e' 5as is5 an' without warrant% o1 an% 3in'& either e4presse' or
implie'. The entire ris3 as to the 6ualit% an' per1ormance o1 the pro'uct is borne b% the
user.
)houl' the pro'uct proe 'e1ectie in an% respect& the user will assume the entire cost o1 an%
serice an' repair an' will assume 1ull liabilit% 1or an% in'irect& special& inci'ental or
conse6uential 'ama"es arisin" out o1 the use o1 or inabilit% to use the pro'uct& inclu'in"&
without limitation& 'ama"es 1or loss o1 "oo'will& wor3 stoppa"e& computer 1ailure or
mal1unction& or an% an' all other commercial 'ama"es or losses& een i1 a'ise' o1 the
possibilit% thereo1& an' re"ar'less o1 the le"al or e6uitable theor% 7contract& tort or
otherwise8 upon which the claim is base'.
http://www.best-microcontroller-projects.com Page 3 of 10
http://www.best-microcontroller-projects.com
Resources
PIC C Course
A Co(plete C course for $%C (icros in 11 parts.
CL%C6 the i(age.
1r Clic+ 7ere
PIC Interrupt Secrets
7o3 to Use $%C %nterrupts to Ma8i(u( effect.
CL%C6 the i(age.
1r Clic+ 7ere.
State Machine Secrets.
7o3 to use state (achines to create Solutions to
co(ple8 pro.le(s inclu,ing fully ,e.ugge, e8a(ples 9
one is co,ing a scrolling (enu on a stan,ar, LCD
,isplay.
CL%C6 the %(age
1r Clic+ 7ere.
http://www.best-microcontroller-projects.com Page 4 of 10
http://www.best-microcontroller-projects.com
Measuring Analogue Parameters With No ADC
7ere:s a tric+ % ha0e .een (eaning to try an, it coul, .e useful for you if you
either run out of ADC inputs or your ,e0ice ,oesn:t ha0e one in the first place. %t:s
not a true analogue (easure(ent .ut allo3s you to (easure a resistance that
results in an analogue 0oltage e.g. co(ponents that 0ary their resistance in
response to a physical para(eter.
;uite often you 3ill .e using co(ponents that change their resistance ,epen,ing
on a physical para(eter e.g. te(perature <4CR=> hu(i,ity> Light <LDR=>
potentio(eter <$14= an, the (etho, ,escri.e, here 3ill let you (easure their
resistances 3ithout using an ADC.
4he i,ea is not ne3 since it 3as first use, on $Cs to i(ple(ent 0ery cheap
analogue input for ?oystic+s using only ,igital inputs.
A single ?oystic+ is (a,e fro( t3o orthogonal potentio(eters 100+@!-0+ an, the
tric+ is getting the 0alue fro( the pots into the $C since> at the ti(e> ADCs
3ere tons of (oney so there 3as no 3ay they 3oul, (anufacture a ?oystic+ 3ith
ADCs @ ?ust for ga(e playingA
%t:s ,one using capacitors.
Measuring A Resistance
As any engineer 3ill tell you their first e8perience of <,ifficult at the ti(e=
eBuations is 3hen you sol0e the ,ifferential eBuations for charging an,
,ischarging a capacitor an, you figure out the RC ti(e constant 4or.
4hat ti(e constant is the +ey since if you +eep the capacitance the sa(e an, 0ary
the resistance then the ti(e ta+en for charging the capacitor 3ill 0ary in
proportion to the 0alue of the resistance.
%f you choose the capacitance an, (a8. pot 0alue appropriately then you can
cycle through (easure(ents Buic+ly enough <for ga(e playing= so that it appears
that the pot is continuously rea,.
All you nee, is a 3ay of ,ischarging the capacitor an, then apply a +no3n 0oltage
to the capacitor 0ia the pot.
http://www.best-microcontroller-projects.com Page 5 of 10
http://www.best-microcontroller-projects.com
The Basic PC Joystick Interface
<rea,s one of the four potentio(eters @
so(e ga(e pa,s ha0e t3o ?oystic+s=
PC Joystick Method
4his is the 3ay that the $C ?oystic+ 3as rea, <although no3 (ore relia.le
(etho,s are use,= an, it e(ploys a (onosta.le 3hich 3hen triggere, ,ischarge,
the capacitor. 4he (onosta.le then sets its output lo3 until the capacitor 0oltage
reaches a threshol, at 3hich point the output goes high.
4herefore the ti(e ta+en for the capacitor to charge is proportional to the 0alue
of the pot so the 0alue of the pot can .e esti(ate,. Cy (easuring the ,uration of
the (onosta.le signal you o.tain a 0alue that is proportional to the 0alue of the
resistance.
Dor the $C this 3as not a spectacular success for the follo3ing reasons5
$C co(puter see!s 3ere ,ifferent fro( each other.
$ort interfaces 3ere ,ifferent fro( each other.
$ro.le(s 3ith OS ,ifferences.
Cig tolerance ,ifferences in co(ponents <,ifferent ?oystic+ (anufacturers=.
Co(puter intensi0e olling of the port.
4a+es a long ti"e to reach (a8 pot 0alue.
...an, although the ?oystic+ 3or+e, it reBuire, cali.ration for each co(puter an,
each ga(e> an, reBuire, slightly ,ifferent soft3are to cope 3ith ,ifferent $C
har,3are an, 1&.
http://www.best-microcontroller-projects.com Page 6 of 10
http://www.best-microcontroller-projects.com
4his gi0es you an i,ea of 3hy this (etho, is not for co((ercial usage.
Cut you 3on:t ha0e this huge pro.le( .ecause you 3ill )14 .e running the
soft3are on a (ulti@purpose platfor( so all you shoul, .e concerne, a.out is the
co(ponent tolerances < capacitorEpotentio(eter change 3ith te(perature an,
ti(e <aging= = an, po3er supply tolerance.
Note: You may need to calibrate depending on your application.
Caliration
1ne pro.le( is that you (ay nee, to cali.rate the syste(. An, this is for t3o
reasons5
1. Un+no3n inaccurate po3er supply.
2. %naccurate potEcapacitance.
Note: Just remember to check and test and don't use this technique in a
commercial system (where you run out thousands of boards because you will get
into trouble) alternati!ely reduce the resolution required e.g. for a "# system
measure $"%&"'%&("%&)''% of the supply etc. then the tolerance will be less
important.
Con!ersion
Although the 0alue is proportional it is on an e8ponential cur0e i.e. it:s not linear
so you nee, to use a ta.le to con0ert it into resistance 0alues or use a calculation
in the co,e to con0ert fro( e8ponential.
Applied to the PIC
4he ,ifference .et3een the $%C an, the $C is that the $%C ,igital pin can .e
configure, as an input or as an output on the fly <so you can ,ischarge the
capacitor .y setting the pin as an output an, at logic lo3= an, then set it as an
input an, .y using the stan,ar, $%C input 0oltage le0els <2F @ logic high= as the
threshol, you ,on:t nee, any e8tra %Cs.
http://www.best-microcontroller-projects.com Page of 10
http://www.best-microcontroller-projects.com
"peration
4he proce,ure for using it is5
1. Change the pin to an output.
2. &et the output lo3 <to ,ischarge the capacitor= for G2(s.
. &et the pin to an input.
!. &tart the ti(er.
'. &top the ti(er 3hen the input goes high.
*. Rea, out 0alue fro( ti(er 3hich is proportional to resistance.
Cy changing the ,igital input to an output <3ith the output set lo3= the capacitor
3ill .e ,ischarge, to #ero current li(ite, to 'FE220 H 2(A. <you coul, pro.a.ly
get a3ay 3ithout using the 220R since the internal pin circuitry li(its the current
sin+ to 2'(A @ actually % forgot it 3hen testing an, it 3or+s fine 3ithout it @ it:s
really a protection if the pot is set to #ero resistance @ % ?ust teste, it using a
selection of resistors so % ,i, not ha0e that pro.le(=.
4hen changing the pin to an input @ it effecti0ely .eco(es isolate, fro( the circuit
since 0ery little current is ,ra3n .y an input pin @ the capacitor 3ill charge fro(
nearly #ero <nearly #ero .ecause of the 220R resistor @ the input can:t Buite reach
#ero @ this 3ill .e lo3 enough to register as logic #ero= to 'F 0ia the
potentio(eter. As the input 0oltage crosses the 2F <ish= threshol, for logic high
the $%C input 3ill .e logic 1.
Note: *or a more accurate (repeatable) threshold choose a +chmitt trigger input
or use an internal comparator.
Measuring this ti(e fro( reset to logic 1 3ill gi0e a 0alue proportional to the
0alue of the potentio(eter <one ar( of the pot H 0aria.le resistor=.
Dor the 100+ I 10n co(ponents 4or H 1(s 3hich is *J of the ti(e to charge
http://www.best-microcontroller-projects.com Page ! of 10
http://www.best-microcontroller-projects.com
to the final 0oltage an, this gi0es so(e i,ea of the or,er of ti(e to .e ,etecte,
.y the (icrocontroller.
4he progra( outputs t3o 0alues5
A confi,ence loop counter <?ust a 0aria.le that incre(ents=
4he 4i(er1 0alue.
Note: ,f you use #ista putty.e-e (google for it) is a suitable replacement for
hyperterminal. .yperterminal was remo!ed from #ista/
RS#$# Settings
Kou shoul, set hyperter(inal or putty to the follo3ing settings5
Cau, 2!00
Data Cits /
$arity )one
&top .its 1
Dlo3
control
)one
RS#$# output
4he ter(inal output loo+s li+e this5
2'
11'
4he upper 0alue is the confi,ence output an, lo3er is 4i(er1 0alue. 4his is +ept
at the sa(e position on the screen using F4100 co,es <% really hate the screen
scrolling continuously @ then again there:s no history=.
Something to Notice
%f you re(o0e the resistance co(pletely then the co,e is al3ays 3aiting for the
capacitor to charge an, it ne0er 3illA 4his is an e8a(ple of the type of pro.le(
that can .e create, .y an unforeseen circu(stance. Although this e8a(ple is
http://www.best-microcontroller-projects.com Page " of 10
http://www.best-microcontroller-projects.com
tri0ial it ,e(onstrates that if you ,on:t allo3 for e0ery ,ifferent e8ternal or
internal state the co,e coul, hang. 4he co,e is fore0er stuc+ unless the state
changes. %n this case it ,oes not (atter .ecause the co,e is a ,e(onstration an,
not critical .ut the 3ay aroun, it is to use the 3atch,og ti(er to force a reset.
Cy changing the co,e i.e. using a (ore co(ple8 pro?ect ha0ing a user interface it
3oul, .e possi.le to allo3 the user to stop the test or put up a 3arning (essage.
At the 0ery least it 3oul, allo3 the user to interact 3ith the syste( .efore it gets
stuc+ again.
De!ice
4he ,e0ice use, is the 12D*-' <3ith the ADC turne, off= this is ?ust a ,e0ice %
ha, han,y .ut you can run the co,e on any $%C chip.
Compiler
4he co(piler use, is the Mi+roC co(piler 3hich lets you use it free for the 1
st
2+
7e8 output.
Code Do%nload
Do3nloa, the co,e here5 Clic+ 7ere 4o Do3nloa,
http://www.best-microcontroller-projects.com Page 10 of 10