Electronics Projects: How to Read a Value from a Potentiometer

A potentiometer (often called a pot) is an electronic variable resistor with a knob you can turn to vary the resistance. Pots of various types are often used as input devices for BASIC Stamp projects. For example, you might use a simple pot to control the speed of a pair of flashing LEDs: As you turn the pot’s knob, the rate at which the LEDs flash changes.

Here, a 10 kΩ pot is placed in parallel with a 0.1 μF capacitor. In addition, a 220 Ω resistor is placed in series with the pot. This is done to protect the BASIC Stamp from damage that might be caused by excess current if you turn the pot's knob so that the resistance of the pot drops to zero.

The capacitor in this circuit is small enough (0.1 μF) that the circuit will charge and discharge very fast — within about a millisecond or so, depending on where the pot knob is set. Thus, your program won’t be delayed significantly while it waits for the capacitor to discharge so it can determine the resistance of the pot.

image0.jpg

So given this circuit, how would you go about measuring the resistance of the pot? The answer requires a clever bit of programming: First, you set pin 13 to HIGH, which charges the capacitor. Then, you set up a loop to monitor the input status of pin 13. Each time you check the status of pin 13, you add one to a counter.

When the capacitor has discharged, pin 13 will go LOW. When pin 13 is LOW, the loop ends, and the counter indicates how long it took to discharge the capacitor. Knowing the size of the capacitor and the length of time it took to discharge the capacitor, you can calculate the resistance of the pot.

Fortunately, PBASIC includes a command called RCTIME that does all of this automatically. All you have to do is tell the RCTIME command what pin the RC circuit is on, whether you want to measure how long it takes the RC circuit to charge or discharge, and the name of a variable to store the resulting time calculation in.

Here’s how to use the RCTIME command to determine how long it takes an RC circuit on pin 13 to discharge, storing the answer in a variable named Timer:

RCTIME 13, 1, Timer

This RCTIME command sets the variable named Timer to a value that indicates how long it took the RC circuit to discharge. Immediately before this command, you should set the I/O pin (in this case, pin 13) to HIGH to charge the capacitor. You’ll also need to pause for a short time (usually, 1 ms is enough) to allow the circuit to charge.

Although you can use this technique to calculate the actual resistance of a pot, you don’t usually have to know the exact resistance. Instead, it’s usually sufficient to know that the counter increases when the resistance of the pot increases, and it decreases when the resistance of the pot decreases.

For this circuit, the RCTIME command calculates time values ranging from about 12 when the resistance of the pot is near 0 to about 54 when the resistance of the pot is at its maximum (10 kΩ).

The program below alternately flashes LEDs connected to pins 0 and 2. The rate at which the LEDs flash is set by a pot in an RC circuit on pin 13. The program multiplies the time value calculated by the RCTIME command by ten to determine how long the program should pause between flashes. As you turn the pot’s knob, the pot’s resistance decreases and the LEDs flash at a faster rate.

' Potentiomter LED Flashing Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0 and 2
' at a rate determined by an RC circuit on pin 13.
' {$STAMP BS2}
' {$PBASIC 2.5}
Time VAR Word
Led1 PIN 0
Led2 PIN 2
Pot PIN 13
DO
 HIGH Pot
 RCTIME Pot, 1, Time
 HIGH Led1
 LOW Led2
 PAUSE Time * 10
 LOW Led1
 HIGH Led2
 PAUSE Time * 10
LOOP

This project shows how to build a circuit that includes a 10 kΩ potentiometer and a capacitor so that you can test the code. This shows the completed circuit.

image1.jpg image2.jpg image3.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com