analogWrite not working? [SOLVED]

Folks,

I am sure this has been discussed before, not sure if a solution has been posted, but I am not having any luck with the analogWrite, I tried some analog pins, I get nothing :frowning:

Any idea? Thanks.

It would help if you show us what you tried, and what results that code gives. Please be specific.

2 Likes

No doing anything fancy, just testing some analog code. The way this code is, the LED stay ON it does not fade at all, if I change to, letโ€™s say, to A0, I dont get anything.

int ledPin = D0;  
int fadeValue;
void setup() {
  
}

void loop() {
  
  for (fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin, fadeValue); // <== This is not working 
    delay(100);
  }
for (fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(ledPin, fadeValue); //<==  THis is not working 
    delay(100);
  }
}

If you look at the documents for analogWrite, you should see this note:

NOTE: pinMode(pin, OUTPUT); is required before calling analogWrite(pin, value); or else the pin will not be initialized as a PWM output and set to the desired duty cycle.

4 Likes

Thatโ€™s all what it was, If the pin is not declare as an OUTPUT I will never see anything. All good now. thanks a lot for your help.

1 Like