analogWrite on pin 9 turns led A3 on

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.

void setup() {
}
void loop() {
  analogWrite(A3, 0);
  analogWrite(9, 0);
  delay(1000);
  analogWrite(9, 200);
  delay(1000);
}

It only fails when I use pin 9 and A3 in the same code. Controlling pin 9 or pin A3 alone works as expected.
This works:

void setup() {
}
void loop() {
  analogWrite(A3, 0);
  delay(1000);
  analogWrite(A3, 200);
  delay(1000);
}

Thanks for any tip you can give

Welcome to the forum!

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.

Thanks for your reply.

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.

Hi keesver,

Looking at the "variant.cpp" file for the Nano 33 IoT located here:

C:\Users\Computer\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\variants\nano_33_iot

I can see that pins A3 and D9 share the same timer output.

Here's the entry for pin D9:

{ PORTA, 20, PIO_DIGITAL, (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER_ALT), No_ADC_Channel, PWM0_CH6,   TCC0_CH6,     EXTERNAL_INT_4    },

...and for A3:

{ PORTA, 10, PIO_ANALOG,  (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER_ALT), ADC_Channel18,  PWM0_CH2,   TCC0_CH2,     EXTERNAL_INT_NONE },

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.

Thanks for the explanation. I'll try to rewire my led's.

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.

Thanks for your great support.

Hi keesver,

What am I missing here?

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.

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