I have 2 led's connected to a Nano 33 IOT. One on pin 9 and another on pin A3 (17). When I run the following code, both led's start blinking. I would expect that only the led on pin 9 would blink.
I do not have time today to figure out exactly how the error is created (*) but I can tell you roughly why. The SAMD21 on the Arduino Nano 33 IoT has one Digital-to-Analog Converter (DAC). That means you can only have one analog output.
On older Arduino the analogWrite function was software driven I believe and therefore you could have multiple analog out pins. On device with DAC you can only have as many analog pins at the same time as you have DACs.
In your code the DAC output is assigned to one pin and then to another. What happens to the other pin is unpredictable because your software does not explicitly state that. This likely also depends on how you wired the LEDs e.g., active high vs active low.
I suspect pin A3/D17 switches between active HIGH digital output and analog 0V. But that is just a guess.
Does this mean there is a limit to the number of PWM pins that can be used simultaneously? The IOT 33 has 10 PWM pins in total, I want to use all of them?
What I do not understand is why it works with all other pins, if I use 9 with A2 it does work.
Btw. there is no difference between using active-high or active-low wiring.
One pin uses TCC0 channel 6 while the other uses TCC0 channel 2, however the TCC0 timer has only four channels 0 to 3, repeated (to provide high-side outputs) on channels 4 to 7. This means that channels 2 and 6 are actually the same and therefore can't be shared.
If you move an output from say A3 to A2, the analogWrite() functions should work as expected.
One other thing, I checked the file variant.cpp and in this file pin 4 is also marked as a PWM compatible pin. However, this pin is not listed on the manual page for analogWrite() as being PWM enabled (analogWrite() - Arduino Reference). A small test shows that it actually works!! What am I missing here?
For now, I'll try to use this pin and see if my project works as expected.
I don't think you're missing anything. The analogWrite() timer allocation on the Nano 33 IoT is a bit of a mess.
The situation you encountered with A3 and D9 should never happen and as you noticed, D4 should really be documented as a viable PWM output.
To be fair, the SAMD21 datasheet section on the timer output channels is practically unintelligible, so the fact that A3 and D9 share a timer output might not be that obvious.
On one of the MKR boards I encountered the opposite problem, certain pins were apparently NOT_ON_TIMER in the "variant.cpp" file, even though there were actually perfectly usable timer outputs available.