BLOG / ARDUINO / How to Read a Potentiometer with Arduino…
博客文章

How to Read a Potentiometer with Arduino (Raw + Percent)

Viktor Build ~2 min read

Wire a potentiometer to an Arduino and read its values via serial monitor, including a percent-mapped version for cleaner output.

// 在YouTube上观看

Reading a potentiometer with an Arduino is one of the first things worth knowing. It's three wires, a few lines of code, and gives you a variable input you can use for almost anything — speed control, brightness, scrolling, whatever.

Parts

  • Arduino (any model)
  • 10kΩ potentiometer
  • Jumper wires

How a potentiometer works

A potentiometer is a variable resistor with three pins. The two outer pins connect to power and ground. The middle pin — the wiper — slides along a resistive track, so as you turn the knob, it outputs a voltage somewhere between 0 and 5 volts. That voltage is what the Arduino reads.

Wiring

Connect the left outer pin to 5V, the right outer pin to GND, and the middle wiper pin to A0 on the Arduino. Any analog input pin works — A0 is just a common choice. That's the whole circuit.

Reading raw values

The analogRead() function returns a value between 0 and 1023, corresponding to 0–5V. Upload the sketch below, open the serial monitor at 9600 baud, and turn the knob — you'll see the number change in real time.

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

void loop() {
  int value = analogRead(A0);
  Serial.println(value);
  delay(100);
}

Mapping to percent

Raw numbers between 0 and 1023 aren't always the most readable. The map() function lets you convert them to any range you want. Here's a version that outputs 0–100%, which is much easier to read at a glance:

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

void loop() {
  int value = analogRead(A0);
  int percent = map(value, 0, 1023, 0, 100);
  Serial.print(percent);
  Serial.println("%");
  delay(100);
}

What you can use this for

Once you have a variable analog input, you can use it to control almost anything: LED brightness with PWM, motor speed via an H-bridge, servo position, or even scrolling through a menu. It's a simple component but it shows up everywhere.

Full code is on my site. If you build something with this, drop it in the Discord — I'd like to see what people make.

加入Discord社区

提问、分享你的作品,与其他创客交流互动。

加入Discord — 免费

喜欢这个教程吗?

在Patreon上支持频道,提前访问项目及更多内容。

在Patreon上支持 →