Shop OBEX P1 Docs P2 Docs Learn Events
Accurate Rctime measurements — Parallax Forums

Accurate Rctime measurements

Luis_PLuis_P Posts: 246
edited 2010-09-20 17:30 in BASIC Stamp
I’m getting different values when a measure resistance with the Rctime code below. Example sometimes I get 57 or 56 or 55. How can I get always the same value?
THIS IS THE CODE:

results VAR Word
LOW 0
PAUSE 50
top:
HIGH 0
PAUSE 500
RCTIME 0,1,results
DEBUG DEC results, CR
GOTO top

THE CIRCUIT ATTACHED:
I have a ceramic cap (0.1uF) one end connected to +5V other end to P0, one resistor(10K) connected to P0 other end to VSS(ground).
271 x 128 - 4K

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-19 21:18
    You can't really. RCTIME depends on the Stamp input pin sensing a voltage dropping below a threshold. The circuitry internal to the Stamp is not very accurate. The threshold is temperature sensitive, supply voltage sensitive, depends on the phase of the moon, etc. Not only that, but you're using a ceramic capacitor which itself is temperature sensitive.

    If you want accuracy, you can improve on RCTIME by using a comparator like 1/4 of an LM339 or 1/2 of an LM393. You can also do better using a polyester or mylar capacitor instead of a ceramic one. You may still get some variation in values, maybe movement between two adjacent values. You can improve that with averaging of a series of 3 or 5 values.
  • Luis_PLuis_P Posts: 246
    edited 2010-09-20 16:53
    I see... I have a new cap (Mylar) and I will use something to average the value like "IF results => 58 and =<60 then goto ...

    Thanks.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-20 17:30
    An easy way to find a "running average" is to keep 2 or 3 past values, add the last 2 or 3 values together and divide by 2 or 3 like:

    rctime ???,???,newValue
    average = (average + newValue) / 2

    oldValue = newValue
    rctime ???,???,newValue
    average = (average + oldValue + newValue) / 3

    You'd read one initialization value for average or two initialization values for
    average and newValue.

    The idea here is that newer values have greater significance than older values,
    yet the averaging causes a smoothing of the values and the average doesn't
    change wildly.
Sign In or Register to comment.