Arduino Nano - Potentiometer

This tutorial instructs you how to use Arduino Nano with a potentiometer, which is also known as pot, trimmer, variable resistor, rheostat, or rotary angle sensor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Potentiometer
1×(Alternative) Potentiometer Kit
1×(Alternative) Potentiometer Module with Knob
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Potentiometer

A rotary potentiometer, also known as a rotary angle sensor, is used to manually alter the value of something. Examples include the volume of a stereo, the brightness of a lamp, and the zoom level of an oscilloscope.

Potentiometer pinout

The Potentiometer Pinout

A potentiometer typically has three pins:

  • The GND pin should be linked to ground (0V).
  • The VCC pin should be connected to VCC (5V or 3.3v).
  • The output pin provides the voltage to the Arduino's input pin.
Potentiometer pinout

※ NOTE THAT:

The GND and VCC pins can be swapped.

How It Works

The rotatable shaft of the potentiometer has a range from 0° (closest to GND) to the maximum angle (closest to the VCC pin), referred to as ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX is determined by the manufacturer. Generally, we do not need to consider this value unless we are calculating the angle of rotation (see the use cases section).

How Potentiometer Works

The working principle:

  • An user rotates shaft of the potentiometer
  • ⇒ The angle of the potentiometer is changed
  • ⇒ The resistance of the potentiometer is changed
  • ⇒ The voltage in the output pin of the potentiometer is changed
  • ⇒ The analog value read by Arduino Nano is changed

Arduino Nano - Rotary Potentiometer

Arduino Nano's pins A0 through A7 are capable of functioning as analog inputs. These analog input pins convert the voltage (ranging from 0 volts to VCC) into integer values (from 0 to 1023), referred to as ADC value or analog value.

We can connect an output pin of the potentiometer to an analog input pin. This allows us to read the analog value from the pin and convert it to a meaningful value.

The value Arduino Nano receives is not an angle or voltage; rather, it is an integer value ranging from 0 to 1023.

Once we have obtained the integer value from the analog input pin, we can rescale it to a different value. Let us consider the applications of this.

Use Cases

  • Rescale to the angle of the potentiometer.
  • Rescale to the voltage of the potentiometer.
  • Rescale to a controllable value, such as volume of a stereo, brightness, or speed of a DC motor - this is the most frequent use.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0VVCC
ADC valueread by Arduino 01023
Other valueconverted by Arduino VALUE_MINVALUE_MAX

Wiring Diagram

The wiring diagram between Arduino Nano and Potentiometer

This image is created using Fritzing. Click to enlarge image

How To Program For Potentiometer

  • Retrieve the value from an input pin, linked to the output pin of the potentiometer, using the analogRead() function.
analog_value = analogRead(A5);
  • Use the map()function to adjust the potentiometer's angle.
angle = map(analog_value, 0, 1023, 0, ANGLE_MAX);
  • Adjust the voltage to the potentiometer's level.
voltage = map(analog_value, 0, 1023, 0, VCC);
  • Rescale the value to something that can be managed or adjusted (e.g. volume of a stereo, brightness, speed of a DC motor... )
value = map(analog_value, 0, 1023, VALUE_MIN, VALUE_MAX);
  • For instance, adjusting the brightness of an LED. As stated in this tutorial, the brightness of an LED can be regulated by using a PWM value ranging from 0 (always OFF) to 255 (always ON). Thus, we can map the analog value to the brightness of the LED (from OFF to the brightest) like this:
brightness = map(analog_value, 0, 1023, 0, 255);

If you desire to adjust the LED from a dim nightlight to its brightest level,

nightlight = 100; // depending on your desired brightness brightness = map(analog_value, 0, 1023, nightlight , 255);

※ NOTE THAT:

The map() function is only applicable for rescaling an analog value to either the int or long type. If the controllable value is of float type, floatMap() should be used instead of map().

The floatMap() function:. It is a function that takes a list of floats as an argument and returns a new list with the same number of elements, where each element is the result of applying the given function to the corresponding element of the original list.

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-potentiometer */ float floatMap(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // The setup function runs once on reset or power-up void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); } // The loop function repeats indefinitely. void loop() { // read the input on analog pin A5: int analog_value = analogRead(A5); // Rescale to potentiometer's voltage (from 0V to 5V): float voltage = floatMap(analog_value, 0, 1023, 0, 5); // print out the value you read: Serial.print("Analog: "); Serial.print(analog_value); Serial.print(", Voltage: "); Serial.println(voltage); delay(1000); }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
Arduino IDE Upload Code
  • Open the Serial Monitor.
  • Turn the potentiometer.
  • Check out the result on the Serial Monitor.
COM6
Send
Analog: 0, Voltage: 0.00 Analog: 0, Voltage: 0.00 Analog: 126, Voltage: 0.62 Analog: 281, Voltage: 1.37 Analog: 517, Voltage: 2.53 Analog: 754, Voltage: 3.69 Analog: 906, Voltage: 4.43 Analog: 1023, Voltage: 5.00 Analog: 1023, Voltage: 5.00
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Challenge Yourself

Utilize the potentiometer to accomplish one of these projects:

Additional Knowledge

  • GND and VCC pins can be swapped without any specific convention. However, one thing to bear in mind is that the voltage value at the output pin will be inverted when these pins are interchanged.

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!