Analog pins don't PWM with analogWrite()

Hello, I’m new to the Photos and I’m nit sure what I’m doing wrong or if there’s something I just don’t know. I’m running through the new book “Programming the Photon” and about 60 pages in there’s a simple circuit with simple code to perform basic PWM on pin A0. When I wire everything up and run the following code, the LED remains as dark as the recesses of the universe.

int ledPin = A0;

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

void loop() {
for(int fadeValue=0;fadeValue<=255;fadeValue++){
analogWrite(ledPin, fadeValue);
delay(30);
}

for(int fadeValue=255;fadeValue>=0;fadeValue--){
    analogWrite(ledPin, fadeValue);
    delay(30);
}

}

Now, if I change analogWrite to digitalWrite, the LED lights. That makes me think the circuit and the code are correct. But, if I change things back to analogWrite, the void greets me again. Just to make sure, I also tried pin A1 and received the same results. I read on this forum that when using PWM, pinMode(ledPin, OUTPUT); may need to get called each time. I tried that with no results. Is there some setting I’m missing to get PWM working for this simple experiment?

The photon is awesome, by the way. Thanks!

On the Photon, A0 cannot be used for PWM, so if the book says to use that pin, it is incorrect (A0 does work on the Core). Look at the docs on analogWrite to see which pins work for the Photon.

1 Like

Interesting, thank you. The book does say to use A0, so perhaps this is unedited residue from an older Core experiment? Regardless, I hooked everything up to A4 and now I’m seeing beautiful PWM. Thank you!

1 Like