Read a potentiometer
Level
Beginner
By admin
1 min read
3 readers
Log in to track your progress.
0 of 3 lessons completed · 0% complete
Map knob rotation to a numeric value and display it in the Serial Monitor.
Wiring
| Potentiometer | Arduino |
|---|---|
| Left pin | 5V |
| Center pin | A0 |
| Right pin | GND |
The center pin is the wiper: rotating the knob changes the voltage read by the ADC.
Code
CPP
const int potPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(potPin);
int mapped = map(raw, 0, 1023, 0, 255);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" mapped=");
Serial.println(mapped);
delay(50);
}
Open Serial Monitor at 9600 baud and rotate the potentiometer — values should change monotonically from 0 to 1023 (aside from noise at the extremes).
Filter noise
For more stable reads, compute a moving average over 8 samples:
CPP
long readFiltered() {
long sum = 0;
for (int i = 0; i < 8; i++) {
sum += analogRead(potPin);
}
return sum / 8;
}
Creative uses
- Control LED brightness via PWM on pin 9
- Modulate an oscillator frequency in Pure Data via Serial → OSC (later tutorials)
Materials
- 10 kΩ potentiometer × 1
- Arduino Uno × 1
Comments (0)