Arduino Potentiometer – Complete Tutorial

In this Arduino tutorial you will learn how to work with a potentiometer.

We’ll start from scratch, building the circuit. Then you will write the Arduino code to interact with the potentiometer, so you can read some data. And finally you will learn how to make sense of the data you got from the potentiometer, in order to build your Arduino application.

Arduino potentiometer circuit

For this circuit you will need:

  • Arduino board (I use Arduino Uno but any other board with available analog pins will do).
  • Breadboard.
  • Potentiometer.
  • Wires (male to female).

Here is the circuit.

Arduino Potentiometer Circuit

To do the circuit and connect the potentiometer to the Arduino:


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


  • First make sure that the Arduino is powered off.
  • Plug the potentiometer on the breadboard, with each leg on an independent line. Note: some potentiometers have a different layout, with 2 pins facing one side, and the middle pin facing the other side. In this case, plug the potentiometer in the middle of the breadboard so it will be easier to add wires to the circuit.
  • Connect one of the external leg of the potentiometer to the ground of the Arduino (GND).
  • Connect the other external leg to the power supply (5V).
  • Plug the middle leg to an analog pin of the Arduino, for example here A0. (more info on Arduino Uno pins here)

Arduino code to read potentiometer data

Let’s write a very minimal code to just read what we get from the potentiometer, and print this data on the Serial monitor 10 times per second.

The code

#define POTENTIOMETER_PIN A0

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

void loop()
{
  Serial.println(analogRead(POTENTIOMETER_PIN));
  delay(100);
}

Let’s break down this code line by line.

#define POTENTIOMETER_PIN A0

As a best practice, we first create a define to keep the potentiometer pin. This will allow us to keep using POTENTIOMETER_PIN in the code every time we need to do something with the potentiometer. And later on, if we need to change the pin to A1 for example, then we just need to modify this single line at the beginning of the program, and nothing else.

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

In the void setup(), we initialize the Serial communication, because what we want to do is to print the result we read.

Note here that we don’t initialize anything related to the potentiometer pin. For a push button and an LED you have to use pinMode(), but why can you skip this for a potentiometer? Well, simply because an analog pin is already an input pin, made for reading inputs from sensors. We could actually rename “analog pins” by “analog input pins”.

void loop()
{
  Serial.println(analogRead(POTENTIOMETER_PIN));

In the void loop(), we print the result we read from the potentiometer, using the function analogRead() which takes 1 argument: the number of the analog pin to read from.

  delay(100);
}

After reading and printing the result, we add a delay() of 100 milliseconds, which is also 0.1 second. After this, the void loop() exits and is immediately called again, which will start reading the potentiometer again. By waiting 0.1 second between each sample, we can read 10 times a second.

The result – how does it work?

When you upload this code to your Arduino board, and open the Serial monitor on the Arduino IDE, you will see a new number every 0.1 second.

If you manipulate the potentiometer knob you will see the value increasing or decreasing, between 0 and 1023 (512 when in the middle position).

Note: the way you plugged the GND and 5V pins will change the way you read the data. For example, if you plug in this order: GND – analog pin – 5V, you will get values from 0 to 1023 (turning clockwise). If you plug in this order: 5V – analog pin – GND, you will get the opposite (still turning clockwise), from 1023 to 0.

Now, why do you get those values?

Well, basically the potentiometer will act as a variable resistor. When you turn the knob, the resistor will increase or decrease, which will also increase or decrease the voltage on the analog pin to which the potentiometer is connected.

So, with this example, the Arduino Uno has a max voltage of 5V. The voltage on pin A0 will vary from 0V to 5V when you turn the knob.

And how to translate that into something you can read into your code? That’s where the analogRead() function comes into play. This function will simply read the voltage and translate it into an integer number in a scale of 0-1023.

Arduino Analog Input Pin Function

For Arduino Uno we can talk about a 10 bit ADC (Analog Digital Conversion), because 2^10 = 1024. There are an infinite number of possible values between 0V and 5V (for example 3.45776V), and analogRead() will give you a 10 bit resolution with a number between 0 and 1023, which you can then directly use in your code.

Map Arduino potentiometer reading for your application

Great, you can now read some data from your potentiometer. But the next step you’ll need to take is to give some kind of meaning to this value.

Because, for example if you read 654, what does it mean?

Here I will show you how to “map” the data you got into a range that makes sense for you or your application.

Map to a percentage

A first easy example to illustrate this is to map the value into a percentage, between 0% and 100%. Having the result “50%” might make more sense to you than “512”.

For this you can do the math yourself, or use the Arduino map() function like this:

#define POTENTIOMETER_PIN A0

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

void loop()
{
  int data = analogRead(POTENTIOMETER_PIN);
  int percentage = map(data, 0, 1023, 0, 100);
  Serial.print("Potentiometer at ");
  Serial.print(percentage);
  Serial.println("%");
  delay(100);
}

Here, what we do is first to read the potentiometer with analogRead(). We store this into an int data type.

Then, we compute the percentage using map(), which takes 5 arguments:

  • Initial data to convert.
  • Min of range for the initial data (0).
  • Max of range for the initial data (1023).
  • Min of range for the result (0 for 0%).
  • Max of range for the result (100 for 100%).

map() will simply take care of converting the initial data to a new range that you choose. Note that all the arguments, and also the result, are integer numbers (everything will get rounded).

With this example we also create a better print message, so instead of just a number, you will get something like “Potentiometer at x%”.

Map to a number centered in the middle

Let’s say you want to map the value so it gives:

  • A max negative number when the knob is at the min position (0).
  • 0 when in the middle (512).
  • A max positive number when the knob is at the max position (1023).

Now let’s say you want this range to be between -90 and + 90.

You will use map() like this: map(data, 0, 1023, -90, 90);

Of course, the way you’ll name the variable for the result of the map() function, and the message you print (or not) will depend on your application.

Conclusion – going further with the potentiometer

In this tutorial you’ve learnt how to connect a potentiometer to your Arduino, how to read data from the potentiometer, and how to make sense of the data.

Potentiometers can be used in so many different applications, and can be combined with many other components.

For example you could use a potentiometer as a way to change the audio volume, set the contrast for an LCD screen, increase the power of a lawn mower, etc.

To go further from here with Arduino and potentiometers, check out:

Did you find this tutorial useful?

Do you want to learn Arduino from scratch?

If yes, this course is for you:

Arduino For Beginners - Complete Course

>> Arduino Programming For Beginners <<