decrot
Posts: 10
Joined: Sat Mar 16, 2013 3:47 pm

RC timing circuit for resistive sensor

Sat Mar 16, 2013 11:16 pm

I need to build a RC circuit to read 'analog' values from a resistive sensor. I saw and implemented this code from adafruit

Code: Select all

import RPi.GPIO as GPIO, time, os      
 
DEBUG = 1
GPIO.setmode(GPIO.BCM)
 
def RCtime (RCpin):
        reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        time.sleep(0.1)
 
        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1
        return reading
 
while True:                                     
        print RCtime(18)     # Read RC timing using pin #18
What I produce is a wildly varying values being read from a potentiometer. even dividing the readings by 10, I still get values that vary in the range of 20 readings. So my question is, during this while loop

Code: Select all

while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1
        return reading
how did they determine that the reading time is 1 millisecond to execute this loop? Is this a dependable value?

User avatar
MattHawkinsUK
Posts: 538
Joined: Tue Jan 10, 2012 8:48 pm
Location: UK
Contact: Website

Re: RC timing circuit for resistive sensor

Sun Mar 17, 2013 8:19 am

The loop takes a random time as it just counts loops. Why do you think it takes 1ms?

The measurements will vary a lot. I use this technique with a light sensor. I do a lot of averaging to get a usable reading. I average over a few readings and round the number.

Here is my take on the Adafruit article : http://www.raspberrypi-spy.co.uk/2012/0 ... -gpio-pin/

So in my security system I can use it to tell if my garage is dark, day light or lights on.

Didn't work so well with a thermistor so I am going to use a DS18B20 1-wire sensor for temperature.
My Raspberry Pi blog and home of the BerryClip Add-on board : http://www.raspberrypi-spy.co.uk/
Follow me on Google+, Facebook, Pinterest and Twitter (@RPiSpy)

User avatar
Burngate
Posts: 6253
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: RC timing circuit for resistive sensor

Sun Mar 17, 2013 11:15 am

MattHawkinsUK wrote:The loop takes a random time as it just counts loops. Why do you think it takes 1ms?....
He thinks it does, because Adafruit says so.
As for why Adafruit thinks that, you'll have to ask them

User avatar
aTao
Posts: 1091
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: RC timing circuit for resistive sensor

Sun Mar 17, 2013 11:28 am

decrot wrote: how did they determine that the reading time is 1 millisecond to execute this loop? Is this a dependable value?
No!, if this was the only code running then sure its reliable, however theres is a not insignificant issue that your code is just one thread in a bloody huge tangle of whatever Linux is messing around at.
Bare metal, sure, go for this kind of code, any hint of multithreading (as per just about any OS) then dont touch it with a barge pole (RISCOS might have some middle ground).

What you should do is select RC time constant in the order of 10ths of a second. Log the time when the GPIO out is changed and log the time at each reading. Average out the time of the first input that shows change AND the previous sample time. Repeat as many times as you can bear to wait and then use that. Otherwise you may as well use a dart and calendar for your readings (as you have noticed)
>)))'><'(((<

Return to “Automation, sensing and robotics”