Introduction: Arduino Basics: RCtime

About: My name is Randy and I am a Community Manager in these here parts. In a previous life I had founded and run the Instructables Design Studio (RIP) @ Autodesk's Pier 9 Technology Center. I'm also the author of t…

RCtime is a function for the Arduino that finds its roots in Basic-based micro controller programming languages (such as the Basic Stamp). This function basically counts the amount of time it takes to charge a capacitor through a resistor and returns a digital value. In some sense, it is a simple hack for analog to digital conversion. The general purpose of such a circuit would be to read analog sensors when all of the analog pins on the board are occupied or, more commonly, when you need a higher resolution than the puny A/D pins on the Arduino currently have to offer. Although, keep in mind that unlike the built in analog pins, the response of RCtime is not linear.

Aside from reading analog sensors, you can do a number of other swell things like monitor voltage, current and capacitance. I'm not going to go over them here, but you can read more about them on this page.

Step 1: Go Get Stuff

To do this you will need:

An Arduino
A breadboard
A capacitor (0.1uF)*
A resistor (220 ohm to 1K)**
A potentiometer (or any resistive sensor like a photocell or FSR)
Jumper wires

*note that changing the capacitor will change the resolution of the circuit. To increase the resolution, you simply need to increase the size of the capacitor. Try using a 1uF electrolytic capacitor and compare the difference (remember that is is polarized and the + side should connect to the power source).

**The resolution can also be changed, but less significantly, by changing the resistor value. Just make sure to keep it in the range of 220 ohm to 1K.

(Note that some of the links on this page are affiliate links. This does not change the cost of the item for you. I reinvest whatever proceeds I receive into making new projects. If you would like any suggestions for alternative suppliers, please let me know.)

Step 2: Build the Circuit

Connect the 1K resistor to pin 4 of the Arduino.

Using the breadboard connect the other end of the 1K resistor to one of the legs of the capacitor and one of the outer pins of the potentiometer.

Connect the middle pin of the potentiometer to ground.

Connect the other pin of the capacitor to +5V.

Step 3: Program

The code that I used has come directly from the RCtime tutorial on arduino.cc

I have done this because this is solidly written code and there is no sense in reinventing the wheel for learning purposes.

Here it is:
<pre>/* 

/* 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)
 */

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   
}

Simply copy this code into your development environment and upload it to the board.

Step 4: Change It Up

Any resistive sensor should work. For instance, I have swapped out the potentiometer with a photocell (LDR) in this example. You can read a wide range of analog sensors this way. Experiment and see what works.

Did you find this useful, fun, or entertaining?
Follow @madeineuphoria to see my latest projects.

3rd Epilog Challenge

Participated in the
3rd Epilog Challenge