Learn Arduino Tutorials Open the 3D lab

Project 4 · Beginner · 6 min read · 15 minutes to build

Potentiometer with Arduino — analogRead(), map() and a dimming LED

Turn a knob and an LED fades smoothly from dark to bright — the same idea as a volume knob or a light dimmer. You will read your first analog value, scale it with one line of code, and learn how a digital pin can fake "half on".

An Arduino UNO wired to a potentiometer and a yellow LED with a 220 ohm resistor on a breadboard
Try this project in 3DRun the code, press the parts, break things safely — free, in your browser.Open in 3D →

What you need

  • Arduino UNO R3 (or a compatible board)
  • USB cable (A to B)
  • Half-size breadboard
  • 10 kΩ potentiometer
  • 5 mm LED
  • 220 Ω resistor
  • Jumper wires (male–male)

Don't have the parts? Build it in 3D first.

What you will build

A knob (a potentiometer) and a yellow LED. Turn the knob one way and the LED glows brighter; turn it back and it fades out. The serial monitor shows both numbers live, like knob 512 -> LED 127.

Knobs like this are everywhere: volume controls, oven dials, joysticks (two knobs in one), and the speed control on a fan. Any time you want a person to choose how much, a potentiometer is the cheapest way.

How it works

A potentiometer is a 10 kΩ resistor with a sliding contact, called the wiper. The two outer legs go to 5 V and GND. As you turn the knob, the wiper slides along the resistor, and the middle leg gives any voltage between 0 and 5 V. This is a voltage divider. More in what is a potentiometer.

The UNO reads that voltage on A0 with analogRead(). Its converter splits 0–5 V into 1024 steps, so each step is about 4.9 mV:

Knob positionVoltage on A0analogRead()map() → LED
All the way to GND0 V00 (off)
A quarter1.25 V≈ 256≈ 63
Middle2.5 V≈ 512≈ 127
All the way to 5 V5 V1023255 (full)

To dim the LED, the program uses PWM (pulse-width modulation): pin 9 switches on and off hundreds of times a second. analogWrite(9, 127) keeps it on about half the time, so the LED looks half as bright. Only the pins marked ~ (3, 5, 6, 9, 10, 11) can do this. See PWM explained.

At full brightness the yellow LED (about 2.0 V) takes (5 − 2.0) V ÷ 220 Ω ≈ 14 mA — safe for the LED and the pin.

Close-up of the potentiometer's three legs in the breadboard
Outer legs to 5V and GND; the middle leg, the wiper, goes to A0.

The wiring

  1. Push the potentiometer into the breadboard with its three legs in 5c, 7c and 9c.
  2. Jumper wires: 5V → 5a, 7a → A0 (the middle leg), 9a → GND.
  3. Push the yellow LED in with its long leg (+) in 15d and its short leg in 16d.
  4. Put the 220 Ω resistor from 16b to 20b.
  5. Jumper wires: pin 9 → 15a, 20a → GND.
Part pinGoes toWhat it does
Potentiometer outer leg (5c)5VThe top of the divider
Potentiometer wiper (7c)A0The voltage you set, read by the UNO
Potentiometer outer leg (9c)GNDThe bottom of the divider
LED long leg (+)Pin 9 (~)PWM dims the LED
LED short leg (−)220 Ω → GNDLimits the current
Schematic of the knob circuit: potentiometer between 5V and GND with its wiper on A0, and D9 to an LED and 220 Ω resistor
The schematic from the 3D lab's Wiring tab.

The code, line by line

const int KNOB = A0;
const int LED = 9;   // a ~ pin: it can dim

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(KNOB);                // 0 … 1023
  int brightness = map(reading, 0, 1023, 0, 255); // 0 … 255
  analogWrite(LED, brightness);
  Serial.print("knob ");
  Serial.print(reading);
  Serial.print("  ->  LED ");
  Serial.println(brightness);
  delay(100);
}
  • const int KNOB = A0; names the analog pin. LED = 9 — a ~ pin, so it can dim.
  • analogRead(KNOB) measures the voltage on A0 and returns 0 to 1023.
  • map(reading, 0, 1023, 0, 255) rescales one range to another: 0 stays 0, 1023 becomes 255, and everything in between moves in proportion. It uses whole numbers, so 512 becomes 127, not 127.6.
  • analogWrite(LED, brightness) sets the PWM level, 0 (always off) to 255 (always on). You do not need analogRead's 0–1023 here: analogWrite only goes to 255.
  • The four Serial.print lines build one line of text: knob 512 -> LED 127. Only the last one is println, which ends the line.
  • delay(100) reads ten times a second — fast enough to feel instant, slow enough to read the monitor.

Common mistakes

  • The LED is either fully on or fully off. It is on a pin without ~. Move it to pin 9 (or 3, 5, 6, 10, 11) — wire and code.
  • The reading jumps around randomly. The wiper is not on A0, so the pin floats. Check that the middle leg goes to A0.
  • The reading is stuck at 0 or 1023. One outer leg is missing its wire, or the middle leg is wired to 5V or GND by mistake.
  • analogWrite(LED, reading) without map(). Values above 255 wrap around, so the LED goes bright, dark, bright as you turn. Always scale with map() (or divide by 4).

Try these challenges

  1. Flip the direction in code: map(reading, 0, 1023, 255, 0).
  2. Make the LED blink, with the knob setting the speed: delay(reading); between on and off.
  3. Light the LED only when the knob is past halfway (if (reading > 512)).
  4. In the 3D lab, drag around the knob, or type 512 in its number box, and compare the serial monitor with the table above.

Check yourself

  1. What range of numbers does analogRead() return on the UNO?

  2. Why is the LED on pin 9 and not on pin 8?

  3. What does map(1023, 0, 1023, 0, 255) give?

Questions people ask

How do I connect a potentiometer to an Arduino?

Outer legs to 5V and GND, the middle leg (the wiper) to an analog pin such as A0. Then analogRead(A0) gives 0–1023 as you turn the knob. It does not matter which outer leg gets 5V; swapping them only reverses the direction.

What value potentiometer should I use with Arduino?

10 kΩ is the usual choice and what this project uses. Anything from about 1 kΩ to 100 kΩ works; smaller values waste more current, much bigger ones give noisier readings.

Why use map() instead of dividing by 4?

For 0–1023 to 0–255, reading / 4 gives the same result. map() is clearer and works for any ranges — for example 0–1023 to a servo angle of 0–180.

Can analogWrite() output a real analog voltage?

No. On the UNO it makes PWM: fast on/off pulses at 5 V. An LED or motor averages them, so they behave as if the voltage were lower.

What's next

One LED, one brightness. Now control three at once and mix any colour you like: the RGB LED project.

Try this project in 3DRun the code, press the parts, break things safely — free, in your browser.Open in 3D →
Next: RGB LED with Arduino — mix any colour with three PWM pins →

Your progress stays on this device — no account, nothing sent anywhere.

Teach with this project

Put the live 3D project on your own class page or blog — free. Copy this code:

<iframe src="https://learnarduino.tech/?embed#/projects/knob" title="Potentiometer with Arduino — analogRead(), map() and a dimming LED — live in 3D" width="100%" height="480" style="border:0;border-radius:12px" allow="fullscreen" loading="lazy"></iframe>

Last updated 2026-10-29

Read next