Digital input only tells you on or off. A potentiometer — the same component behind volume knobs and dimmer sliders — lets you read a continuous range of values. This tutorial uses one to control an LED's brightness.
Beginner
⌛ 20 min
🔧 What You'll Need
- Arduino Uno
- 1x 10kΩ potentiometer
- 1x LED
- 1x 220Ω resistor
- Breadboard
- 4x jumper wires
Wiring the Circuit
Connect the potentiometer's outer two pins to 5V and GND. Connect the middle pin (the wiper) to analog pin A0. Wire the LED through a 220Ω resistor to a PWM-capable digital pin — pin 9, marked with a ~ on most boards.
The Code
const int potPin = A0;
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
// analog pins don't need pinMode() to read them
}
void loop() {
int potValue = analogRead(potPin); // 0 to 1023
int brightness = map(potValue, 0, 1023, 0, 255); // scale to 0-255
analogWrite(ledPin, brightness);
}
analogRead() returns a value from 0 to 1023, representing the voltage at the pin as the potentiometer's wiper moves. analogWrite(), on the other hand, only accepts 0 to 255 — that's what PWM (pulse-width modulation) uses to simulate a variable voltage. The map() function rescales one range to the other in a single line.
💡 Tip: Not every digital pin supportsanalogWrite()— only the ones marked with a~on the board silkscreen (typically 3, 5, 6, 9, 10, 11 on an Uno). Using a non-PWM pin compiles fine but won't dim the LED at all.
Troubleshooting
- LED is always full brightness or always off: Confirm the potentiometer's outer legs are actually on 5V and GND, not both on the same rail.
- Brightness jumps instead of smoothly fading: This is expected with a cheap potentiometer's mechanical noise — not a code issue.
- Nothing changes as you turn the knob: Double check the wiper (middle pin) is on A0, not one of the outer pins.
Analog input is the gateway to real sensors — temperature, distance, light — which all work the same way under the hood. The next tutorial puts this to use with an actual sensor.
Menu