DUE PWM Frequency

yes

the AVR has so-called "accurate PWM mode" to facilitate this h-bridge control (pulse is always centered around a unique determined point in the PWM period). it seems the Due's SAM has what Atmel call "complementary" mode. this is only available on the four PWM pins. the other pins are called TC (Timer/Counter) and do not feature complementary mode.

btw. . changing TC_FREQUENCY does not mess up delay() or micros(). i'm a happy man :slight_smile:

shit this sounds complicated to drive an h bridge with arduino due

Usually H-bridge driving is done with a pair of PWM signals that have a "dead time" between them, rather than the overlapping "phase correct" type of signal on 8 bit AVR chips.

But then, who knows what's really needed here. There's very little accurate description and a lot of complaining, which is hardly a good approach to asking for free tech support / help on a forum!

Hi thanks for your reply

I want to genrate 3 SINE PWM signals with a variable duty cycle and frequency. Each one of the three signals shall be 120 degree in phase to the other one.

I hope I could explain what I actually need

Is that really all you need? Just a moment ago you threw in a H-bridge requirement, but without specifying what signals it needs.

Maybe you really need 6 waveforms, with some sort of non-overlapping dead time?

yes to drive an asynchron motor you need 3 sine pwm signals in 120 degree phase shift

Generating the sinewaves is pretty simple.
You need to generate a look-up table (simple array) with the sine values, and then do analogWrite(pin, value).

//Sinus lookup table
static int sinusTabelle[255] = 
{
  128,131,134,137,141,144,147,150,153,156,159,162,165,168,171,174,177,180,
  183,186,189,191,194,197,199,202,205,207,209,212,214,217,219,221,223,225,
  227,229,231,233,235,236,238,240,241,243,244,245,246,248,249,250,251,252,
  252,253,254,254,255,255,255,255,255,255,255,255,255,255,255,255,254,254,
  253,253,252,251,250,249,248,247,246,245,243,242,240,239,237,236,234,232,
  230,228,226,224,222,220,218,215,213,211,208,206,203,201,198,195,193,190,
  187,184,181,179,176,173,170,167,164,161,158,155,152,148,145,142,139,136,
  133,130,126,123,120,117,114,111,108,104,101,98,95,92,89,86,83,80,77,75,
  72,69,66,63,61,58,55,53,50,48,45,43,41,38,36,34,32,30,28,26,24,22,20,19,
  17,16,14,13,11,10,9,8,7,6,5,4,3,3,2,2,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,4,
  5,6,7,8,10,11,12,13,15,16,18,20,21,23,25,27,29,31,33,35,37,39,42,44,47,
  49,51,54,57,59,62,65,67,70,73,76,79,82,85,88,91,94,97,100,103,106,109,
  112,115,119,122,125
};

Now since a three phase motor requires 120° phase shift, i did the following:
Define three counters (a, b, c) with a = 0, b = 85 and c = 170.
360° / 3 = 120°. Therefore 255 / 3 = 85. So if a looks at array position 0, b looks at 85 and c at 170, they are exactly 120° phase shifted.

Now you need a loop like

void loop()
{
analogWrite(PhaseU, sinusTabelle[a]);
analogWrite(PhaseV, sinusTabelle[b]);
analogWrite(PhaseW, sinusTabelle[c]);
a++;
b++;
c++;
if (a == 255), a = 0; //255 can be changed for whatever amount of samples is used in the sine look-up table
if (b == 255), b = 0;
if (c == 255), c = 0;
delayMicroseconds(X); //Factor X can be used to change frequency of the sine wave
}

/*If you also want to adjust the amplitude, you can add a divider in the analogWrite function.
For example */
analogWrite(PhaseU, sinusTabelle[a] / 2); //This would half your amplitude

Maybe someone with better programming skills could make it a bit neater. I am not really a good programmer, but the code works.

1 Like

@Ewoodster
This guy is a bit of a time waster, he keeps asking the same questions and never ever states what his requirements are.
Over in this other thread we got to that sort of code you posted.
http://forum.arduino.cc/index.php?topic=225600.0
He wants to do the same thing but we managed to squeeze out of him that he wants an 80KHz sin wave made from PWM signals. He refuses to believe it can't be done.
You will also note that he refuses to believe that the only way of changing the frequency is to change the number of samples or change the sample rate, he insists there is another way but unsurprisingly does not know what it is.

This guy is a bit of a time waster, he keeps asking the same questions and never ever states what his requirements are.

Yes, you're absolutely right. Many people ask questions in this very ineffective style, but this guy is particularly bad.

I keep hoping to find a way to impress on people like this how to better ask their questions. Sticky threads help a bit, but obviously some people do not read them. On the PJRC forum, I've been intending to look into how to edit the HTML templates for the forum, so posting guidelines can appear right above the text entry box when starting a new thread, and perhaps a reminder can appear right next to the button to submit the message.

These people not only ruin their own chances for getting useful help, but they waste a LOT of time from everyone else... time that could be better spent actually helping people who've posted sincere questions with adequate detail.

@so3ody - If you're still reading this, why have you posted so many questions with so little detail? You seem to want help with your PWM waveforms. Can you see how your poor writing has ruined your opportunity for good answers from knowledgeable and helpful people? What words or messages might have inspired you to write better questions?

If anyone has any other ideas about what arrangements of pixels might somehow inspire people like this to write better questions, I'm really, really interested to hear. It's a tough problem, and perhaps can never be fully solved, but even small improvements could really help a lot of people in the long run.

Why Im a time waster..
I asked questions and I posted the results but I still have some problem...And my post here wast older than the other post..As I didnt get any answer here I just asked a new question..I posted last week some results ! Thanks anyway..Im just new in dealing with arduino !

@so3ody - If you're still reading this, why have you posted so many questions with so little detail? You seem to want help with your PWM waveforms. Can you see how your poor writing has ruined your opportunity for good answers from knowledgeable and helpful people? What words or messages might have inspired you to write better questions?

Well first of all english is not my mother tongue..

And secondly I just wanted to solve my problems in small steps this is why I didnt say from the bgegninning all what I need.. I thought that It could be a good way solving my problem..

so3ody:
.... wanted to solve my problems in small steps this is why I didnt say from the bgegninning all what I need.. I thought that It could be a good way solving my problem..

What words or images could have shown you the error of this assumption?

When someone else comes here with questions and again would make this same mistake, how could this forum (or similar technical forums on other sites) instruct people to ask their questions in a more effective manner.

Obviously, this lengthy "How to use this forum" page did not help. Did you ever see that page, or read it, before posting? What could have helped you to understand the proper way to ask technical project questions?

Ok I will ask my questions with more details esc.. Sorry for bothering anyone

You did not answer my question.

I want to improve how new people, like you (only days ago), see forums before posting their first question. If you had known this before writing all those questions, you could have received better answers and avoided wasting so much time from the most knowledgeable and helpful people.

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

Please, consider you have consumed so much time from so many people. The least you could do is offer a little help to improve the process for everyone in the future.

What could have shown you, only a few days ago, how to ask a better question before you wasted everyone's time?

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

io53:
Had a quick look at it and I can confirm that changing PWM_FREQUENCY in
Arduino/variant.h at ide-1.5.x · arduino/Arduino · GitHub 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()
{
analogWriteResolution(Resolution);
analogWrite(Pin, PosPulse*2.05); //always multiply by 2 something
delay(5);
}

@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.

http://forum.arduino.cc/index.php?topic=144446.msg1149044#msg1149044

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 `` attributes which would be a major improvement!

Ray