Go Down

Topic: Problem with LED brightness (Read 121 times) previous topic - next topic


Hi guys,
I have a problem with changing the brightness of my LED connected through Arduino Nano. I have a pot connected to an analog input and I mapped it from 0 to 255. I then applied this value in the digitalWrite func. When I check the values through serial monitor everything is ok, but the led is just on or off. The brightness doesn't change. Im using B100K pot and 220 ohm res with yellow LED. I have tried different pots and LEDs but nothing changes. I even set the brightness manually in the code outstepping the pot but nothing changed. Here is the code:

  int led = 2;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(A0, INPUT);

void loop() {
  int potval = analogRead(A0);
  int power = map(potval, 0, 1023, 0, 255);
  digitalWrite(led, power);

I really don't know what is going on. Thanks for help 



Yeah... I'm dumb. I haven't checked if the pin had PWM and of course it didn't. Thanks for the help, next time I'll be more patient  :D


Apr 07, 2021, 11:06 pm Last Edit: Apr 07, 2021, 11:18 pm by PaulRB
I haven't checked if the pin had PWM and of course it didn't.
Even if the pin did have PWM, if you use digitalWrite() on the pin, you still only get on/off.
next time I'll be more patient
...and use code tags please!


Do not feel bad, you and thousands of others have made the same mistook, You might consider watching a few arduino tutorials, they will help your understanding. The Arduino Cookbook is a good handbook as well.
This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!


@gilshultz, what do you have against spelling "mistake" correctly?
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


Check out the logic of PWM for your circuit to control the brightness of the LED

Go Up