Reading a Potentiometer with Arduino

Learn how to measure potentiometer position values using Arduino

What is a Potentiometer?

A potentiometer is a device that allows you to manually vary its resistance between a minimum value (usually 0 ohms) and a maximum value (Rmax). The common values for Rmax are 5kΩ, 10kΩ, or 20kΩ.

It consists of a resistive track with a moving contact. As the contact moves along the track, the resistance changes. Potentiometers have three terminals, two at the ends of the track, and one at the moving contact.

Key Applications of Potentiometers

Potentiometer Applications

Some common uses:

  • Adjusting LED brightness
  • Controlling DC motor speed
  • Adjusting servo motor position
  • Reading sensor values in variable resistance applications

Electrical Schematic

The potentiometer acts as a voltage divider, providing a varying output voltage based on its position. This voltage can be read by an Arduino analog input.

Potentiometer Schematic

The potentiometer's resistance varies between Rmin and Rmax, allowing the Arduino to read its position using the voltage divider formula:

Vout = Vin × (R2 / (R1 + R2))

Potentiometer Assembly

Below is an image showing the correct assembly of the potentiometer with the Arduino:

Potentiometer Assembly

Potentiometer Reading Code


const int analogPin = A0;
int value;      // variable to store the raw analog reading
int position;   // position of the potentiometer as a percentage

void setup() {
  Serial.begin(9600);
}

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

const int analogPin = A0;
int value;      // variable to store the raw analog reading
int position;   // position of the potentiometer as a percentage

void setup() {
  Serial.begin(9600);
}

void loop() {
  value = analogRead(analogPin);       // perform the raw analog reading
  position = map(value, 0, 1023, 0, 180);  // convert to servo position range (0-180)

  //... do whatever you want with the measured position value

  delay(1000);
}