# Reading a Potentiometer with Arduino

In this post 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 regulate the intensity of a Led light, the speed of a DC motor, or the position of a servo.

The measurement of the value of a potentiometer is similar (although not the same) to the measurement of the unknown resistance, something we saw in this post. The concepts we saw are necessary to understand the content of this post so, if you have not done so yet, I recommend reading it.

## What is a potentiometer?

A potentiometer is a device that allows its resistance to be varied manually, between a minimum value Rmin, (usually 0 ohms) and a maximum value Rmax. Common values for Rmax are 5k, 10k, or 20k ohms.

Internally, a potentiometer is constituted by a movable contact that moves along a resistive track. In this way, by moving the potentiometer, we move the contact along the track, and by varying the length of the track section with which we are in contact, and therefore 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 movable contact. This terminal varies its resistance with respect to the other two terminals as we actuate the potentiometer, with the sum of the resistance to the other terminals equal to Rmax.

With respect to the geometry, we can find linear or rotary potentiometers.

Finally, regarding the relationship between position and resistance, we find linear, parabolic, or exponential potentiometers. Linear ones present a proportionality between resistance and displacement, which means a more intuitive behavior. Meanwhile, exponential ones allow greater precision in 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 resistance, with an important caveat. We do not need a calibration resistance since the potentiometer itself acts as a voltage divider.

On the other hand, the resistance between the terminals is always Rmax of the potentiometer, while in the case of a calibration resistance 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 displacement is really simple. We simply use an analog input to read the voltage value, and transform it into the position by interpolating with the “map” function.

``````const int analogPin = A0;
int value;      //variable that stores the raw analog reading
int position;   //potentiometer position as a percentage

void setup() {
}

void loop() {