Change the PWM frequency on Arduino Nano 33 BLE

I want to change the PWM frequency on a digital pin to about 31000Hz. How can i do it ?

You can use the mbed functions. Note that the frequency will slightly differ from the value you ask for depending what values can be created by the hardware. I hope the formatting works. This is my first post with the new forum style.
The example first creates a static 50% duty cycle signal and when you open the Serial monitor will change the duty cycle from 0 to 100%.

#include "mbed.h"

#define PWM_PIN               10
#define PWM_FREQUENCY         31000

mbed::PwmOut pwmPin( digitalPinToPinName( PWM_PIN ) );

void setup()
{
  pwmPin.period( 1.0 / PWM_FREQUENCY );
  pwmPin.write( 0.5 );
  
  Serial.begin( 9600 );
  while ( !Serial );
  Serial.println( "mbed PWM pin example" );
}


void loop()
{
  static uint32_t dutyCycle = 50;
  
  dutyCycle = ( dutyCycle + 1 ) % 100;  
  pwmPin.write( dutyCycle / 100.0 );
  
  delay( 10 );
}
1 Like

Thanks alot. I will implement it and report on it. I'm really glad that you help me :grinning:

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