0
0
AutocadHow-ToBeginner · 3 min read

How to Read Potentiometer in Arduino: Simple Guide

To read a potentiometer in Arduino, connect its middle pin to an analog input pin and use analogRead(pin) to get a value between 0 and 1023. This value represents the position of the potentiometer knob as a voltage level.
📐

Syntax

The basic syntax to read a potentiometer value is using the analogRead() function.

  • analogRead(pin): Reads the voltage on the specified analog pin and returns a value from 0 (0V) to 1023 (5V).
  • pin: The analog input pin number where the potentiometer's middle pin is connected (e.g., A0).
arduino
int sensorValue = analogRead(A0);
💻

Example

This example reads the potentiometer value connected to analog pin A0 and prints it to the Serial Monitor every 500 milliseconds.

arduino
void setup() {
  Serial.begin(9600); // Start serial communication
}

void loop() {
  int sensorValue = analogRead(A0); // Read potentiometer
  Serial.println(sensorValue);    // Print value
  delay(500);                     // Wait half a second
}
Output
512 523 510 ... (values change as you turn the knob)
⚠️

Common Pitfalls

Common mistakes when reading a potentiometer include:

  • Not connecting the potentiometer correctly: The middle pin must go to the analog input, and the other two pins to 5V and GND.
  • Using digitalRead() instead of analogRead(), which will not give the correct value.
  • Not initializing serial communication before printing values.
  • Ignoring the delay, which can flood the Serial Monitor with too many values.
arduino
/* Wrong way: Using digitalRead */
int sensorValue = digitalRead(A0); // Incorrect for potentiometer

/* Right way: Using analogRead */
int sensorValue = analogRead(A0); // Correct
📊

Quick Reference

Tips for reading potentiometers on Arduino:

  • Connect potentiometer pins: one to 5V, one to GND, middle to analog pin.
  • Use analogRead() to get values from 0 to 1023.
  • Initialize serial communication with Serial.begin(9600); before printing.
  • Use delays to avoid flooding output.

Key Takeaways

Use analogRead(pin) to read potentiometer values from 0 to 1023.
Connect the potentiometer middle pin to an analog input and the other pins to 5V and GND.
Initialize serial communication before printing values to Serial Monitor.
Avoid using digitalRead() for analog sensors like potentiometers.
Add delays to control the speed of reading and printing values.