Potentiometer-Dimmed LED (Arduino)
Turn a knob to smoothly control LED brightness. Learn analogRead, map(), and analogWrite.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
pot_dimmer.ino
// Read a potentiometer and use it to dim an LED.
const int potPin = A0;
const int ledPin = 9; // PWM pin (~)
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int raw = analogRead(potPin); // 0–1023
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
}
Steps
- Wire the pot: outer pins to 5V and GND, wiper (middle) to A0.
- Wire the LED from D9 through a 220Ω resistor; cathode to GND.
- Upload, then turn the knob to dim the LED.