Potentiometer LED Dimmer
Turn the knob to control LED brightness with PWM.
- Beginner
- LED & Lighting
- 5 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
The potentiometer’s wiper goes to analog pin A0, as in the reading example, and an LED with a resistor is connected to pin 9. Pin 9 supports PWM: analogWrite() switches it on and off hundreds of times a second, and the fraction of time it spends on sets how bright the LED looks.
loop() reads the knob (0 to 1023) and uses map() to rescale that to 0 to 255, the range analogWrite() expects. Turn the knob all the way down and the LED is off; all the way up and it is fully bright. The brightness value is also printed to the Serial tab.
Parts
- Arduino Uno
- Potentiometer
- Resistor
- LED
- GND
Wiring
- Arduino Uno 5V → Potentiometer VCC
- Potentiometer Wiper → Arduino Uno A0
- Arduino Uno GND → Potentiometer GND
- Arduino Uno D9 → Resistor A
- Resistor B → LED Anode (+)
- LED Cathode (-) → GND
Arduino code
// Potentiometer LED Dimmer
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
int val = analogRead(A0);
int brightness = map(val, 0, 1023, 0, 255);
analogWrite(9, brightness);
Serial.print("Brightness: ");
Serial.println(brightness);
delay(50);
}
Try changing it
- Invert it so turning the knob up dims the LED: change map(val, 0, 1023, 0, 255) to map(val, 0, 1023, 255, 0).
- Cap the maximum brightness by changing the last 255 to 100.