Overview
The Uno cannot output an in-between voltage. Pulse-width modulation (PWM) switches a pin fully on and off hundreds of times per second; the fraction of time spent on, the duty cycle, sets the average. An LED flickers too fast to see, so it simply looks dimmer.
You will need
- Arduino Uno R3
- LED and 330 Ω resistor
- 10 kΩ potentiometer
- Breadboard and jumper wires
Wiring
| Uno pin | Connects to | Notes |
|---|---|---|
| 9 | 330 Ω resistor → LED anode | PWM pin (~) |
| GND | LED cathode | |
| 5V | Pot outer leg | |
| A0 | Pot wiper | |
| GND | Pot other outer leg |
Diagram
The sketch
const int LED_PIN = 9; // must be a PWM pin: 3, 5, 6, 9, 10 or 11
const int POT_PIN = A0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int reading = analogRead(POT_PIN); // 0..1023
int brightness = map(reading, 0, 1023, 0, 255); // 0..255
analogWrite(LED_PIN, brightness);
delay(10);
}How the code works
analogWrite() takes 0–255, where 0 is always off and 255 always on. Pins 5 and 6
run at about 980 Hz, the others at about 490 Hz. The Servo library disables PWM on pins 9 and 10,
and tone() interferes with pins 3 and 11, so plan pins when combining features.
Perceived brightness is not linear: the change from 0 to 30 looks bigger than from 200 to 255. For an even-looking fade, square the value before writing it.
What you should observe
- Turning the knob dims the LED smoothly from off to full.
- Waving the LED quickly in a dark room at mid brightness shows a dotted trail: the pulses.