Go Down

Topic: Cannot set the PWM frequency for Arduino Due (Read 946 times) previous topic - next topic

cmd1024

Jul 12, 2019, 06:54 pm Last Edit: Jul 12, 2019, 06:56 pm by cmd1024
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:


Code: [Select]

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


void setup() {

  Serial.begin(9600);

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

}


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 it doesn't work above 20kHz.


I made the following change in variant.h:

Code: [Select]

* PWM
 */
#define PWM_INTERFACE       PWM
#define PWM_INTERFACE_ID    ID_PWM
#define PWM_FREQUENCY       20000
#define PWM_MAX_DUTY_CYCLE  4095
#define PWM_MIN_DUTY_CYCLE  0
#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 :((

ard_newbie

The right way to set the ADC frequency and the DAC frequency is by triggering conversions with a Timer Counter or a PWM Event Line (not by altering variant.h :o )

Search in the DUE sub forum for example sketches.

Go Up