Go Down

Topic: DUE PWM Frequency (Read 66293 times) previous topic - next topic


how can these forums help new people, like you, to post better questions?

Well I think that a software is always a buzzle of small parts.

And I beleive that there are standard things in my casw " H Bridge driver via Arduino Due"

I think itsa good idea after such a project really works. to test it as an admin or  user and make a small doecumentation like

Used  methods : PWM, Sine wave esc. and then write " tested and works"

So this will mean that I will not start from the beginning and that I will just concentrate my self on my problem

I saw in my case that my problem was mentioned in like 5 threads and really no one have a decent answer for it


Had a quick look at it and I can confirm that changing PWM_FREQUENCY in
https://github.com/arduino/Arduino/blob/ide-1.5.x/hardware/arduino/sam/variants/arduino_due_x/variant.h#L182 does change the freq. So if you just need the 4k freq it'll work fine. Still, a way to change the freq in the sketch would be preferred.

Thank you, It works! mine I change the value to 500 for 500Hz PWM freq (intended for motor ESC) and tested it
on simple code below which create a positive pulse of 500us in a 2ms period (500Hz) :
By default, PWM Output Frequency is 1000Hz.
To change it by changing the PWM_FREQUENCY constant in the file variant.h
on folder C:\Program Files (x86)\Arduino\hardware\arduino\sam\variants\arduino_due_x\variant.h
and look for line " #define PWM_FREQUENCY  1000 "

const int Resolution=12, Pin=12, PosPulse=500;
void setup(){ 
  // make our digital pin 12 an output
  pinMode(Pin, OUTPUT); 

void loop()
  analogWrite(Pin, PosPulse*2.05); //always multiply by 2 something


@webuser, just so you know, you can change frequency in your sketch using the "pwm01.h" library written and tested for the Due.  It also allows you to open two PWM channels simultaneously with different frequencies.  The link below has an example (2Hz and 5000Hz) and the library itself (login to download).  Good luck.



That's great! I look forward to try that. Thanks.


I keep hoping to find a way to impress on people like this how to better ask their questions.

Could the webmaster not just invoke a template on all members classified as "newbies" to enforce a common dialog that would request the needed information?  After their newbie status matures, this restriction can be removed, or earlier if the moderator(s) think it is appropriate.  By "pulling the information" from the user, it puts the need into the correct context for a proper answer from the more seasoned members.  It would also stop (or should severely reduce) the very negative comments from senior members about code, board type, and other items. 

The forum software runs on a computer - heck, it should be smart enough so that a newbie does not have to be blasted or embarrassed for asking a question.  And the code insert part of the template could force the
Code: [Select]
attributes which would be a major improvement!



The forum software runs on a computer - heck, it should be smart enough ......

But no it isn't.
Apparently treating posters differently depending on their number of posts is an almost impossible thing to do with this software, or so I am told.
Like most forums the webmaster has not written it from scratch but rather customizes it.
Therefore what you might consider a simple thing to do is not always possible. 


Mar 29, 2014, 01:18 am Last Edit: Mar 29, 2014, 01:58 am by mrburnette Reason: 1
Therefore what you might consider a simple thing to do is not always possible.

Fair enough.  So, it can be done but either licensing, customization fund$, or laziness/inexperience stands in the way of implementating something new.  40 years in military, university research, or corporate 100 and I put my money on not wanting to deviate from the base package because of increased costs and testing during version updates.

In engineering university back in the dark ages of lunch punch cards, my fortran professor stated, "There is nothing you cannot do with a computer (pregnant pause), If you have enough time and enough money."  I have long sense forgotten his name, but never his insight into equating computing with finance.



Even if these things are difficult or impossible today and here on this forum, they may not always be so unattainable always and everywhere.

Incomplete, time-wasting questions are a huge problem on every forum that tries to offer support for novices on technical platforms.  As the technology becomes more advanced and even more absolute beginners enter the community, it's an ongoing problem that's becoming more and more worthy of difficult work to customize forums.

Of course, as the creator of Teensy, I'm interested in trying to improve the PJRC forum.  I'm also interested in sharing these types of improvements, just like I try to contribute Teensy's improvements back to Arduino (which is another excellent example of something that isn't always easy or even possible, but still worth trying).  Helping beginners is something I care about doing.  Every day I answer lots of questions.  Every day, I waste time asking people to post info they should have put in their original question.  Every day, I see conversations that would have produced useful or more relevant answers if only the question had included better info.

Even if the ideas get tried first on my own forum, or any of the many other forums for various arduino-compatible boards, I think it's worthwhile to brainstorm ideas.  Even if they're "hard" in the context of today's forum software, in the long term some way to improve is needed.

Things will only get better if someone tries.  I know I want to, even if I have to dig into thorny forum customization/programming.  I'm really interested to hear any ideas of how forums could guide novices to ask better questions, so they get better answers and everyone's time is wasted less.  Even if those ideas seem impossible on today's forum systems, time, persistence and ingenuity have a way of making anything possible in the long run.


Hi all! Sorry for necroposting but I think this could be useful for other people reading this thread and using this library.

I need a 20Mhz clock on pin 6 but when I try to use Serial.begin and Serial.print it doesn't print anything.
It does print fine until 200000 pwm_freq, any frequency over that threshold degenerate the serial signal.
this is my code:

Code: [Select]
#include <pwm01/pwm01.h>

#define PIN_XCLK 6 // only pin 6, 7, 8, 9 are available for PWM

void setup() {
uint32_t  dutyRatio = 2;
uint32_t  pwm_duty = 65535 / dutyRatio;
uint32_t  pwm_freq = 20000000;

pwm_setup(PIN_XCLK, pwm_freq, 1);
pwm_write_duty(PIN_XCLK, pwm_duty);


void loop() {


in variant.h:
change the line:

#define TC_FREQUENCY        1000


#define TC_FREQUENCY        300000

and as said previously:

#define PWM_FREQUENCY           1000


#define PWM_FREQUENCY           300000

the frequency doesn't have to match exactly. it's governed by the mother clock divided by the pwm resolution (256 steps) divided by a power of two. the max would be 84 MHz / 256 = 328.125 kHz.

hope that clears things up. there could be more elegant ways and i still have to test what this does to micros() and delay().. on AVR-based Arduino that issue exists.
I have been doing this for a while now and never asked if this clock effects any other function? For example, delay(). Would you by any chance know if it solely affects the certain PWM pins and not any other function?

Dialup is the future.


Jan 06, 2015, 12:28 pm Last Edit: Jan 06, 2015, 10:41 pm by EvIl_DeViL
earx said
btw. . changing TC_FREQUENCY does not  mess up delay() or micros(). i'm a happy man
if all the functions are well written every time related function should be in function of those params

anyway you can add an #ifndef directive in the variant.h and define those params before the inclusion if you don't want to change them for every project. seems nicer...


May 16, 2015, 11:35 am Last Edit: May 16, 2015, 11:35 am by jlbechennec

I am trying to use the pwm01 library

I have the following sketch :

Code: [Select]

#include <pwm01.h>

void setup() {
  pwm_setup(8, 31250, 1);
  pwm_setup(9, 31250, 1);

void loop() {

I get nothing on the PWM 8 and 9 outputs (I use a scope). I get PWM signals with analogWrite and with the modification in variants.h. I reviewed the code in analogWrite for the Due and cannot find a difference with the pwm01 library. Am I missing something ?


May 20, 2015, 08:35 pm Last Edit: May 20, 2015, 09:04 pm by casemod
 The due has a motor control synchronized pwm with dead time generation. But that won't necessarily work with arduino functions. I would say that's the most effective way to get it going.

This link has some information. The design was not very efficient but the bits to configure the module are there.




I need to generate a two 65kHZ PWM, for my bi directional buck boost converter, i have gone through the forum and learnt that there are 2 method for changing the default PWM freq. to desired value...i.e. using timers and using PWM controller.......in my project the freq. required is 65khz, while the duty ratio must be varied based on the input to the converter...using a controller like a PID maybe.....i would like to know , which method (i.e. using timers and using PWM controller) of changing the frequency is better for my application?? any help..???



Hi all,

I have just announced a library which abstract ATSAM3X8E PWM channels in this post: http://forum.arduino.cc/index.php?topic=144446.msg2589358#msg2589358.

The name of the library is pwm_lib and is available at: https://github.com/antodom/pwm_lib.

The library provides two kind of objects associated with each PWM channel: pwm and servo objects. As those objects abstract the PWM channels available on the micro controller, using pwm_lib you can use, at most, eight independent pwm_lib objects in your application, each one with its own PWM characteristics (PWM signal period and pulse duration). In its current version, the maximum period for PWM signals you can get using pwm_lib is a period of 0.798915048 seconds (minimum frequency of 1.251697539 Hz). The maximum frequency you can get is the one provided by the hardware. I see no problems even to get to 1 Mhz but in this case the resolution of the duty will be limited to 84 clock ticks, at 2 Mhz 42 ticks, etc.

Two examples comes with pwm_lib to illustrate its use: basic_test.ino and servo_test.ino, who illustrate respectively the use of pwm and servo objects. Example basic_test.ino uses two PWM objects for generating two independent PWM outputs with different PWM characteristics (period and duty). Example servo_test.ino uses a servo object to generated a PWM output for a typical servo.

I hope it helps.

Go Up