Problem with LED brightness

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

GOOGLE : Arduino analogWrite :slight_smile:

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 :smiley:

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.

@gilshultz, what do you have against spelling "mistake" correctly?

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

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.