#StackBounty: #pwm #arduino-due #frequency Trouble with setting the PWM frequency for Arduino Due

Bounty: 100

I’m using Arduino Due and I set the analog read and write resolution to 12-bit for better resolution. Basically all I do is to vary PWM duty cycle by controlling analog DC input voltage from 0 to 3.3V. The simple code as it is now works fine as follows:

int pwmPin9 = 9;      
int analogPin0 = 0;   
int val = 0;         

void setup() {


  pinMode(pwmPin9, OUTPUT);  // sets the pin as output    


void loop() {

  val = analogRead(analogPin0);  // read the input pin    
  analogWrite(pwmPin9, val); 


My problem is I need to set the frequency of the PWM output to 25kHz or at least something nearest above 25kHz.

I have been searching forums hours and couldn’t find any solution. Here I came across a way but there is no library called variant.h in my folders. And that code if I include in my script doesn’t change anything.
What could be a workaround for this?

(I used to achieve this via this site by using Arduino Uno but for Due couldn’t find any working solution)

Edit: I found the file and made the following change in variant.h:

 * PWM
#define PWM_INTERFACE       PWM
#define PWM_FREQUENCY       20000
#define PWM_MAX_DUTY_CYCLE  4095
#define PWM_RESOLUTION      12

 * TC
#define TC_INTERFACE        TC0
#define TC_INTERFACE_ID     ID_TC0
#define TC_FREQUENCY        20000
#define TC_MAX_DUTY_CYCLE   255
#define TC_MIN_DUTY_CYCLE   0
#define TC_RESOLUTION       8

But it only works for 20kHz. If I set to 25kHz or 30kHz I don’t get any error but also don’t get any output :((

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.