# Arduino - Potentiometer

## Hardware Required

Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

Rotary potentiometer (also called rotary angle sensor) is used to manually adjust the value of something (e.g volume of the stereo, the brightness of lamp, zoom level of oscilloscope...) ### Pinout

Potentiometer usually has 3 pins:

• GND pin needs to be connected to GND (0V)
• VCC pin needs to be connected to VCC (5V or 3.3v)
• Output pin outputs the voltage to Arduino's input pin.
• ※ NOTE THAT:

GND pin and VCC pin are interchangeable

### How It Works

The shaft of the potentiometer is rotatable from 0° (nearest by GND) to an upper bound angle (nearest by VCC pin), called ANGLE_MAX.

The voltage at the output pin ranges from GND's voltage to VCC's voltage. The output voltage is in direct proportion to the rotated angle of the shaft.

• If the angle is 0°, output pin'S voltage is 0v
• If the angle is ANGLE_MAX, output pin'S voltage is VCC's voltage
• If the angle is in between 0° and ANGLE_MAX, output_voltage = angle × VCC / ANGLE_MAX

※ NOTE THAT:

ANGLE_MAX value is depended on manufacturers. In practice, we usually do NOT care about the value of ANGLE_MAX, except when we need to calculate the rotated angle (see use cases part). ### Arduino - Rotary Potentiometer

Arduino's pin A0 to A5 can work as analog input. The analog input pin converts the voltage (between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.

By connecting an output pin of the potentiometer to an analog input pin, we can read the analog value from the pin, and then converts it to a meaningful value.

The value Arduino get is NOT angle, NOT voltage. It is integer value ranges from 0 to 1023.

After getting the integer value from the analog input pin, we rescale this value into another value. Let's see the use cases.

#### Use Cases

• Rescale to potentiometer's angle.
• Rescale to potentiometer's voltage:
• Rescale to the controllable value (e.g volume of stereo, brightness, speed of DC motor... ). This is the most common-used case.

#### Rescale Range

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

## Wiring Diagram Image is developed using Fritzing

## How To Program

• Read the value from an input pin, which connected to the output pin of the potentiometer
• Rescale to the potentiometer's angle
angle = map(analogValue, 0, 1023, 0, ANGLE_MAX);
• Rescale to the potentiometer's voltage:
voltage = map(analogValue, 0, 1023, 0, VCC);
• Rescale to the controllable value (e.g volume of stereo, brightness, speed of DC motor... )
value = map(analogValue, 0, 1023, VALUE_MIN, VALUE_MAX);
• For example, rescaling to the brightness of LED. As mentioned in this tutorial, the brightness of LED can be controlled by using PWM value from 0 (always OFF) to 255 (always ON). Therefore, we can map the analog value to the brightness of LED (from OFF to the brightest) as follows:
brightness = map(analogValue, 0, 1023, 0, 255);

If you want to dim LED from the nightlight to the brightest,

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

※ NOTE THAT:

The map function can only be used to rescale the analog value to the int or long type value. If the controllable value is float type, you need to use the floatMap() function instead of map() function.

floatMap() function:

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 Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-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 routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin A0: int analogValue = analogRead(A0); // Rescale to potentiometer's voltage (from 0V to 5V): float voltage = floatMap(analogValue, 0, 1023, 0, 5); // print out the value you read: Serial.print("Analog: "); Serial.print(analogValue); Serial.print(", Voltage: "); Serial.println(voltage); delay(1000); }

### Quick Steps

• Copy the above code and open with Arduino IDE
• Click Upload button on Arduino IDE to upload code to Arduino
• • Open Serial Monitor
• Rotate the potentiometer
• See the result on Serial Monitor
• COM6
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

## Challenge Yourself

Use the potentiometer to do one of the following projects:

• GND pin and VCC pin are interchangeable. The is no convention about these two pins. If you select a pin as the GND pin, the other is the VCC pin. There is only one thing you need to pay attention to. The voltage value at the output pin is inverted when we interchange these pins.

## Alternative Potentiometers

The above code also works with the following rotary angle sensors:

## Potentiometer on Commercial Products

• Digital Oscilloscope