CYTHEIA AIOpen the editor

Potentiometer LED Dimmer

Turn the knob to control LED brightness with PWM.

Open this circuit and run it

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

Wiring

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

Open this circuit and run it

Next challenge

More projects in LED & Lighting