BS1 icon {PBASIC 1.0}

POT Example




Syntax: POT Pin, Scale, Variable


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

Note: See RCTIME for all BS2 models.


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 PBASIC Language home page

Open Getting Started with Stamps in Class

Open Connection Troubleshooting

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.