# PBASIC Programming/RCTIME

## Contents

## Analog Circuits[edit]

Many times, the devices that we connect to our BasicStamp will be regular analog devices, not digital devices. The difference is that instead of sending out ones or zeros, we need to send out a voltage value. As an example, if we wanted to send the value 4, we could either send a digital signal (100), or we could send an analog signal (+4V).

## Resistors and Capacitors[edit]

A resistor is a device that slows down the flow of electric current. We have already seen Ohm's law before, but we will repeat it here:

*r* is the relationship between the voltage *v* and and the current *i*. The value of a resistor is measured in units of Ohms (Ω).

A capacitor is a special type of device that stores energy. When you put a voltage across a capacitor, it fills with energy. When you take the voltage away, the capacitor releases that energy. For an example, a flash bulb in a camera uses a capacitor to store lots of energy until you press the picture button. When you press the button, the capacitor releases the energy very quickly, and the camera makes a bright flash. Without the capacitor the batteries would never be able to produce enough energy so quickly.

The ability of a capacitor to store energy is measured in units called Farads. Most capacitors that will be used in small applications have very small values, such as 1 millifarad or less.

### RC Circuits[edit]

An electric circuit that contains both resistors and capacitors is known as an RC circuit. A capacitor does not charge or discharge it's energy instantly. There is a special value, known as the **time constant** that determines the amount of time the capacitor takes to charge. The time constant is dependant on the value of the capacitor and the value of the resistor.

### Time Constants[edit]

The time constant of a circuit is calculated as:

Where τ is the time constant, in seconds. *r* is the resistance, in Ohms, and *C* is the capacitance, in Farads. It takes approximately 5 time constants for an RC circuit to completely charge or discharge energy.

### Measuring a Time Constant[edit]

The BasicStamp can measure the time constant directly, without first needing to measure the resistance and the capacitance of the circuit. To do this, the BasicStamp outputs 5 volts into the circuit, long enough for the circuit to charge. Then, the BasicStamp turns the output port into an input port, allowing the circuit to discharge energy. When the amount of energy reaches zero, it has been 5 time constants. The BasicStamp divides the amount of time by 5 and returns the time constant value. Keep in mind that this is an approximate process, and it will fail for very large time constants (because the circuit will not have enough time to charge completely).

## RCTIME[edit]

The RCTIME function measures the time constant of a circuit attached to one of the ports. The other end of the circuit should be attached to ground. It will return a value for the time constant in milliseconds. To read the time constant on port 11, we would write:

MyByte VAR Byte RCTIME 11, MyByte

## Special Resistors[edit]

There are several types of resistors that we can use in our circuits to do different tasks.

### Potentiometers[edit]

A potentiometer, or more simply a "pot" is a resistor with variable resistance. Potentiometers typically have some sort of knob or dial that can be turned to alter the resistance. A good example of a potentiometer is a volume knob in a stereo. When the knob is turned up, the resistance decreases, more current flows, and the sound becomes louder. When the knob is turned down, the resistance increases, less current flows, and the sound becomes softer. Another example is a dimmer switch for a light bulb. The more dim the light, the higher the resistance in the switch must be.

If we have a circuit with a potentiometer, we know what C is, and we can measure the time constant, so we can calculate what the resistance must be. If we know what the maximum and minimum resistance is for the potentiometer, we can calculate how much the knob is turned, and in some cases we can even calculate the exact angle that the knob is pointing at.

Potentiometers can appear as knobs, but they can also be attached to wheels to measure the amount that a wheel has rotated. They can also be available as slide switches.

### Thermistors[edit]

Thermistors are resistors whose resistance changes with temperature. These are commonly used in electric thermometers. By measuring the RC time of a thermistor circuit, we can calculate the temperature.

### Photoresistors[edit]

A photoresistor is a resistor whose resistance changes depending on how much light there is. By measureing the time constant of the photoresistor, we can determine how much light is shined on it. An example of this is an automatic night-light, which turns on when it detects the light is off.