Do you know where it is documented that you can only use 4 pins for PWM using Mbed OS for the Nano 33 BLE?
The PWM module of the Arduino Nano 33 BLE only has 4 channels (see datasheet). The PWM signal is created by hardware and not software. I think some older Arduino use software PWM and therefore can create PWM signals on more pins. But this has its own limitation (CPU load, jitter, lower max frequency). You can do this as well, but you would need to write your own software. For slow frequencies and resolution software PWM is fine e.g. to control LEDs.
How do you free a PWM channel? I can't find a destructor for PwmOut.
Here are two examples. They create a PWM signal on a different pin every second.
PwmOut is destroyed every time at loop exit.
#include "mbed.h"
void setup()
{
}
void loop()
{
static unsigned int pinNumber = 2;
pinNumber = ( ( pinNumber + 1 ) % 10 ) + 2;
mbed::PwmOut pin( digitalPinToPinName( pinNumber ) );
pin.period( 0.002 );
pin.write( 0.5 );
delay( 1000 );
}
PwmOut is destroyed and created inside loop.
#include "mbed.h"
void setup()
{
}
void loop()
{
mbed::PwmOut *ptr;
unsigned int pinNumber = 2;
while ( 1 )
{
pinNumber = ( ( pinNumber + 1 ) % 10 ) + 2;
ptr = new mbed::PwmOut( digitalPinToPinName( pinNumber ) );
ptr->period( 0.002 );
ptr->write( 0.5 );
delay( 1000 );
delete ptr;
}
}
Note: The delay() function has only been used to limit the example to the necessary. In real application, delay should be avoided. The same is true for the while(1) in loop.