Language: EN

lectura-de-un-potenciometro-con-arduino

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.

arduino-potenciometro-conexion

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

arduino-potenciometro-tipos

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.

arduino-potenciometro-curvas

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.

arduino-potenciometro-esquema

Assembly

The necessary assembly is shown in the following image

arduino-potenciometro-montaje

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() {
  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);
}

Try it online

Download the code

All the code from this post is available for download on Github. github-full