Improving PWM Access in Arduino Nano 33 IoT

The ARM-based (SAMD21) Arduino Nano 33 IoT might have been rushed into production based on a somewhat deficient assignment of Timer/Counters to PWM pin functions. Arduino documentation claims 11 PWM pins: D2, D3, D5, D6, D9, D10, D11, D12, A2, A3, and A5. However the following is not documented:

  • D4 is also available for PWM (TCC1/1)
  • D9 and A3 cannot both be used for PWM as they use the same Timer/Counter channel. (TCC0/6 and TCC0/2 which are actually the same).
  • D10 and A2 cannot both be used for PWM as they use the same Timer/Counter channel. (TCC0/7 and TCC0/3 which are actually the same).

This means there are only really 9 PWM pins documented, once you remove A2 and A3 from the list.

But some improvements are possible by modifying the variant.cpp file for the nano 33 IoT. The D9-A3 conflict can be eliminated by changing A3 to use TCC1/0, which is otherwise unused, rather than TCC0/2. Replace the line:

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

with:

{ PORTA, 10, PIO_ANALOG, (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER ), ADC_Channel18, PWM1_CH0, TCC1_CH0, EXTERNAL_INT_NONE },

You can also configure D8 to use another unused Timer/Counter channel TC3/0. Replace the line:

{ PORTA, 18, PIO_DIGITAL, (PIN_ATTR_DIGITAL ), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },

with:

{ PORTA, 18, PIO_DIGITAL, (PIN_ATTR_DIGITAL|PIN_ATTR_PWM|PIN_ATTR_TIMER), No_ADC_Channel, PWM3_CH0, TC3_CH0, EXTERNAL_INT_NONE },

Now you can have 12 available PWM pins: D2, D3, D4, D5, D6, D8, D9, D10, D11, D12, A3, and A5. One third more that the documented available pins. Of course I recommend controlling the hardware directly and not using the Arduino library functions, but if you do want to use analogWrite making these library modifications will improve functionality.

I still consider the Arduino Nano 33 IoT to be the best buy of the SAMD21-based Arduino boards, and a better buy than any of the other Arduino boards other than the Arduino Nano Every. Availability of both of these has been sporadic, but as of this writing (March 24, 2023) both of these are available for purchase.