Arduino - Potentiometer

by DIYables in Circuits > Arduino

12 Views, 0 Favorites, 0 Comments

Arduino - Potentiometer

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-potentiometer

This build shows how to read a rotary potentiometer with Arduino. The potentiometer lets you manually set a value, like volume or brightness, and Arduino converts its position into a number your sketch can use. You will wire the potentiometer, read it with analogRead(), and print the result to the Serial Monitor.

Supplies

Wiring

The potentiometer has 3 pins: GND, VCC, and an output pin. Connect GND to Arduino GND, VCC to Arduino 5V (or 3.3V), and the output pin to Arduino analog input A0. GND and VCC are interchangeable; if you swap them, the output voltage is simply inverted.

How It Works

The potentiometer shaft rotates from 0 degrees (nearest GND) to an upper bound called ANGLE_MAX (nearest VCC). Output voltage is proportional to the shaft angle: 0V at 0 degrees, VCC at ANGLE_MAX, and output_voltage = angle x VCC / ANGLE_MAX in between. Arduino's analog input pins (A0 to A5) convert this voltage into an integer ADC value from 0 to 1023, which is neither angle nor voltage until you rescale it.

Code

The sketch reads the raw ADC value from A0 with analogRead(). From there you use map() to rescale that 0-1023 value into something meaningful: the potentiometer's angle, its voltage, or a controllable value such as LED brightness (0 to 255 PWM) or motor speed. The result is printed to the Serial Monitor so you can watch it change as you turn the shaft.

arduino-potentiometer.ino - the sketch from the tutorial, unchanged.

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

Things That Go Wrong

If turning the potentiometer shows no change on the Serial Monitor, check that the output pin is wired to A0 and that VCC and GND are not swapped with the output pin. If the reading runs backwards from what you expect, GND and VCC have likely been interchanged, which inverts the output voltage.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-potentiometer