Multiple PWM Outputs with Unique Frequencies

I noticed the same thing. I'm not getting any PWM signal on pin 8. I'm going to try reconfiguring my board to use pin7 which is kind of a bummer because the that means more manual labor on my motor controller to get the right pins where they are supposed to be :frowning:

Hmm, the source code to DuePMW library doesn't seem to do anything special with pin 8,
which is PWML5 (peripheral B on pin PC22). Have you checked normal PWM works on
pin 8?

Pin 8 works for me normally and with DuePWM library.

I did try that later and it worked fine in its own sketch. It must be a conflict with another feature of the SAM3X I'm using. I have this thing very busy. I'm using 32 digital inputs, 6 analog inputs (via ADC), the hardware quadrature decoder, a dual channel pwm motor controller, the SPI header, the CAN bus interface, and the two DAC pins to produce an analog signal from a digital value. I'm also using both USB ports for different communication packets and protocols. It's a very busy chip :slight_smile:

I added the DuePWM library to Github pointing to original author message, for easier further development.

GitHub - cloud-rocket/DuePWM: PWM Library for Arduino Due.

randomvibe. Thanks for the useful library. Sometimes I would like to have the exact PWM signal from two different pins. That is, the two pins should go high and low at the same time. Can this be done with this library? Would it be possible to do by other methods? Thank.

Yes, you can set the same frequency for two pins with the pwm01.h library. Just be advised that the libary can only produce frequencies in the range of 2hz to 40,000hz.

In fact, I tried this. At 10,000hz the two signals are slightly out of phase although they are exactly the same frequency. Oh well...

Hi

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 is appreciated...

Any one have a library for the 65khz pwm?

Thanks
Emmanuel

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: GitHub - antodom/pwm_lib: This is a C++ library to abstract the use of the eight hardware PWM channels available on Arduino DUE's Atmel ATSAM3X8E microcontroller..

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.

If any questions just tell me.
I hope it helps.

Hi,
I have problem with compiling example using antodom's library

code:
#include <tc_defs.h>
#include <tc_lib.h>

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}

ERROR

In file included from sketch_apr11a.ino:2:0:
C:\Users\webapps\Documents\Arduino\libraries\tc_lib-master/tc_lib.h:39:19: fatal error: cstdint: No such file or directory
#include
^
compilation terminated.
Error compiling.

In arduino IDE, i have changed flag in platform file to use -std=gnu++11
any ideas?

Hi there @kwasek83,

I am afraid that I need more information to be of any help.

At first sight, I suspect that is the -std=gnu++11 flag. In any case, to be sure, attach the output of the whole compilation process to know what is happening.

Antodom,
Thanks for the library I do get some nice clean signals driving some transducers at 40khz.

I wanted to ask if you might have an example or an idea of how to accurately control the phase of PWM using your library. Ideally Id like to get 12 channels or the maximum outputs I can with the DUE.

The basic_test code provided resulted in a out of phase signal of around 6us. Id like to be able to sync the phases or adjust the phase.

Tested on it on an tektronix scope

Hi there @carbon_adam,

For changing the period (the frequency) of a PWM signal with pwm_lib you can use the method set_period_and_duty(), it modifies the period (and the duty). Have a look to example changing_period_test.ino. Update the last version of the library, because this feature has had a big change this last week to solve an important bug.

As to how many PWM signals you can manage with pwm_lib, you can manage 8 independent PWM signals using pwm_lib (the ones available in the ATSAM3X8E). Take into account that you can have for each channel, two signals, a PWM signal and its negated version. You might also generate PWM signals using any of the nine timer/counters available in the TC module of ATSAM3X8E. Have a look to the data-sheet to know how.

In addition, according to ATSAM3X8E's data-sheet you will see than you might synchronize the PWM channels to be in phase, I don't know if this is what you need. Unfortunately this feature is not available in pwm_lib, I have not had the necessity to have PWM synchronized channels.

I hope it helps.

1 Like

Hello Guys, I am new to Arduino DUE. I need PWM frequency of 100 kHz. Please suggest me the ways how can i get it?

Hello, I need only one PWM port but with 100 kHz. How to get it on Arduino Due?

Antodom, I just wanted to thank you for sharing your pwm_lib library on GitHub. I was able to use it to generate PWM signals on an Arduino Due with no trouble.

I started this project with a Due with the intention of using PWM output on the higher-numbered pins (pins that don't exist on the Uno) and was surprised by the lack of support in the default libraries, so your library saved me a lot of work.

Hi there @NSFW,

You are welcome :), I am glad you have found pwm_lib useful.

Best.