# POT

{PBASIC 1.0}

POT Example

Syntax: POT Pin, Scale, Variable

## Function

Read a 5 kΩ to 50 kΩ potentiometer, thermistor, photocell, or other variable resistance.

- Pin is a variable/constant (0 - 7) that specifies the I/O pin to use. This pin will be set to output mode initially, then to input mode.
- Scale is a variable/constant (0 - 255) used to scale the command's internal 16-bit result. See Explanation below for steps to finding the scale value to use for your circuit.
- Variable is a variable (usually a byte) where the final result of the reading will be stored. Internally, the POT command calculates a 16-bit value, which is scaled down to an 8-bit value.

Note: See RCTIME for all BS2 models.

## Explanation

POT reads a variable resistance and returns a value (0 - 255) representing the amount of time it took to discharge the capacitor through the resistance. Pin must be connected to one side of the variable resistance, whose other side is connected through a capacitor to ground, as shown below.

POT works by first setting the specified I/O pin to an output and setting its state high. This step places +5 volts on one side of the capacitor and ground (0 volts) on the other side, which charges the capacitor. POT will hold the pin high for 10 milliseconds to charge the capacitor. It is important to select component values that will allow the capacitor to charge in this period. If, for example, a 50 kΩ potentiometer is used with a 0.1 µF cap, the maximum charge time would be:

Charge time: 5 x (50 x 103) x (0.1 x 10-6) = 25 x 10-3

The full charge time (5 TC) is 25 x 10-3 or 25 milliseconds. Obviously, the capacitor would never fully charge when the potentiometer is at its maximum position. In this case, changing the capacitor to 0.01 µF reduces the charge time to 2.5 milliseconds; well within the range of the POT function.

After the capacitor is charged the I/O pin to an input mode and POTstarts its timer. Initially the I/O pin will see a high (1) that will eventually drop to a low (0) when the capacitor discharges past the 1.4-volt threshold. The timer stops once the low is seen. The value of the variable resistor affects the time it takes to discharge the capacitor from 5 volts to approximately 1.4 volts.

The 16-bit reading is multiplied by (Scale ÷ 256), so a scale value of 128 would reduce the range by approximately 50%, a scale of 64 would reduce to 25%, and so on. The amount by which the internal value must be scaled varies with the size of the resistor being used.

### Finding the best Scale value:

1. Build the circuit shown above and plug the BS1 into the PC.
2. Select Run | POT Scaling.
   A special calibration window appears, allowing you to find the best value.
3. The window asks for the number of the I/O pin to which the variable resistor is connected. Select the appropriate pin (0 - 7).
4. The editor downloads a short program to the BS1 (this overwrites any program already stored in the BS1).
5. Two numbers will be displayed: scale and value. Adjust the resistor until the smallest number is shown for scale (assuming you can adjust the resistor, as with a potentiometer).
6. Once you've found the smallest number for scale, you're done. This number should be used for the Scale in the POT command.
7. Optionally, you can verify the scale number found above by selecting the POT Value checkbox. This locks the scale and causes the BS1 to read the resistor continuously. The window displays the value. If the scale is good, you should be able to adjust the resistor, achieving a 0-255 reading for the value (or as close as possible). To change the scale value and repeat this step, just uncheck the box. Continue this process until you find the best scale.

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012