Map LED brightness with potentiometer Arduino

In this example we will change LED brightness based on the potentiometer position. We will use map() method to adjust 10 bit input values to 8 bit analog PWM output values.
Remember that potentiometer’s resistance needs to be >= 10k.

int lastPotValue = 0;

void setup()
{
  pinMode(3, OUTPUT);
}

void ChangeLedBrightness()
{
  int potValue = analogRead(A0);
  if (potValue == lastPotValue) return;
  lastPotValue = potValue;
  int ledValue = map(potValue, 0, 1023, 0, 255);
  analogWrite(3, ledValue);
}

void loop()
{
  ChangeLedBrightness();
}

Circuit

Scroll to Top