Loading...

Learning   Examples | Foundations | Hacking | Links

RCtime

In situations where all of the Duino's A/D pins are used, RCtime is a workaround for reading any kind of resistive sensors on any digital pin.

This RCtime function duplicates the Basic Stamp's function of the same name. It can be used to read resistive sensors of any type. Change the capacitor size to achieve the desired resolution.

The function can also be used to read voltage output type sensors, such as the Sharp infrared distance sensor's with some caveats.

One virtue of RCtime is that it can be very wide ranging, reporting values that would require a 16-18 bit A/D input to read. One downside is that it's not perfectly linear, because charging a capacitor through a resistor does not yield a linear curve.

/* RCtime
 *   Duplicates the functionality of the Basic Stamp's RCtime
 *   Allows digital pins to be used to read resistive analog sensors
 *   One advantage of this technique is that is can be used to read very wide ranging inputs.
 *   (The equivalent of 16 or 18 bit A/D)
 *  

 Schematic
                  +5V
                   |
                   |
                  ___
                  ___    Sensing Cap
                   |      .001 ufd  (change to suit for required resolution)
                   |      (102) pfd
                   |
sPin ---\/\/\/-----.
       220 - 1K    |
                   |
                   \   
                   /     Variable Resistive Sensor
                   \     Photocell, phototransistor, FSR etc.
                   /
                   |
                   |
                   |
                 _____ 
                  ___
                   _

*/

int sensorPin = 4;              // 220 or 1k resistor connected to this pin
long result = 0;
void setup()                    // run once, when the sketch starts
{
   Serial.begin(9600);
   Serial.println("start");      // a personal quirk 
}
void loop()                     // run over and over again
{

   Serial.println( RCtime(sensorPin) );
   delay(10);

}

long RCtime(int sensPin){
   long result = 0;
   pinMode(sensPin, OUTPUT);       // make pin OUTPUT
   digitalWrite(sensPin, HIGH);    // make pin HIGH to discharge capacitor - study the schematic
   delay(1);                       // wait a  ms to make sure cap is discharged

   pinMode(sensPin, INPUT);        // turn pin into an input and time till pin goes low
   digitalWrite(sensPin, LOW);     // turn pullups off - or it won't work
   while(digitalRead(sensPin)){    // wait for pin to go low
      result++;
   }

   return result;                   // report results   
}