In this entry we are going to learn how to read the position value of a potentiometer. Potentiometers are devices that we will frequently use to introduce analog values into our Arduino (for example, to adjust the brightness of an LED, the speed of a DC motor, or the position of a servo).
Measuring the value of a potentiometer is similar (though not identical) to measuring an unknown resistance, something we saw in this entry.
The concepts we covered are necessary to understand the content of this entry, so if you haven’t done so yet, I recommend reading them.
What is a potentiometer?
A potentiometer is a device that allows you to manually vary its resistance between a minimum value Rmin, (normally 0 ohms) and a maximum value Rmax. Common values of Rmax are 5k, 10k, or 20k ohms.
Internally, a potentiometer consists of a moving contact that slides along a resistive track. This way, by moving the potentiometer, we move the contact along the track, varying the length of the track segment we are in contact with, and thus varying its resistance.
Normally a potentiometer has three terminals.
- The two ends are connected to both sides of the track, so they will always register the maximum resistance Rmax.
- The remaining terminal corresponds to the moving contact, and it is the one where the resistance varies.
This terminal varies its resistance with respect to the other two terminals as we operate the potentiometer, with the sum of the resistance to the other terminals equal to Rmax.
Regarding geometry, we can find linear or rotary potentiometers.
Finally, in terms of the relationship between position and resistance, we find linear, parabolic, or exponential potentiometers
- Linear potentiometers show a proportionality between resistance and displacement, which means a more intuitive behavior.
- Exponential potentiometers allow greater precision at low resistance values, making them suitable when fine adjustment is needed over a wide range.
Electrical schematic
The schematic is similar to that used to measure a variable resistor, with one important caveat. We do not need a calibration resistor since the potentiometer itself acts as a voltage divider.
On the other hand, the resistance between the terminals is always Rmax of the potentiometer, whereas in the case of a calibration resistor it would be Rs+Rc, so the equations vary slightly.
The definitive schematic is as follows.
Assembly
The necessary assembly is shown in the following image.
Code
The code to read the potentiometer’s displacement is really simple. We simply use an analog input to read the voltage value, and we transform it into the position by interpolating with the “map” function.
const int analogPin = A0;
int value; // variable to store the raw analog reading
int position; // position of the potentiometer as a percentage
void setup() {
}
void loop() {
value = analogRead(analogPin); // perform the raw analog reading
position = map(value, 0, 1023, 0, 100); // convert to percentage
//... do whatever you want with the measured position value
delay(1000);
}
Download the code
All the code from this entry is available for download on Github.