How to generate 3 PWM signal output

I need to use arduino uno to trigger 3 mosfet for my 3-phase interleaved boost converter. Can anyone help me for the code please?? :sweat_smile:

(deleted)

What do you want the waveforms to be? What frequency?

johnwasser:
What do you want the waveforms to be? What frequency?

i want 3 pwm waveform signal with 120 degree phase shift

azeyzoul:
i want 3 pwm waveform signal with 120 degree phase shift

You don't care about the frequency at all?

johnwasser:
You don't care about the frequency at all?

how about the 100hz? can u show the code for 3 pwm with 120 degree phase shift?

byte phaseCount;
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedMicrors;
void setup(){
pinMode (8, output);
pinMode (9, output);
pinMode (10, output);
currentMicros = micros();
previousMicros = currentMicros;
// 3phase, 100 Hz, rising or falling edge every 1664uS ( multiple of 4uS).
void loop(){
currentMicros = micros();
elapsedMicros = currentMicros - previousMicros;
if (elapsedMicros >= 1664){
phaseCount = phaseCount +1;
if (phaseCount == 6){ phaseCount = 0; }
switch (phaseCount):
case 0:
PORTB = 0b00000001;
break;
case 1:
PORTB = 0b00000011;
break;
case 2:
PORTB = 0b00000111;
break;
case 3:
PORTB = 0b00000110;
break;
case 4:
PORTB = 0b00000100;
break;
case 5:
PORTB = 0b00000000;
break;
}
}

Try that.

CrossRoads:

byte phaseCount;

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedMicrors;
void setup(){
pinMode (8, output);
pinMode (9, output);
pinMode (10, output);
currentMicros = micros();
previousMicros = currentMicros;
// 3phase, 100 Hz, rising or falling edge every 1664uS ( multiple of 4uS).
void loop(){
currentMicros = micros();
elapsedMicros = currentMicros - previousMicros;
if (elapsedMicros >= 1664){
phaseCount = phaseCount +1;
if (phaseCount == 6){ phaseCount = 0; }
switch (phaseCount):
case 0:
PORTB = 0b00000001;
break;
case 1:
PORTB = 0b00000011;
break;
case 2:
PORTB = 0b00000111;
break;
case 3:
PORTB = 0b00000110;
break;
case 4:
PORTB = 0b00000100;
break;
case 5:
PORTB = 0b00000000;
break;
}
}



Try that.

when i verifying the code, this error come up:

_1:24: error: break statement not within loop or switch

break;

^

_1:25: error: case label '1' not within a switch statement

case 1:

^

_1:27: error: break statement not within loop or switch

break;

^

_1:28: error: case label '2' not within a switch statement

case 2:

^

_1:30: error: break statement not within loop or switch

break;

^

_1:31: error: case label '3' not within a switch statement

case 3:

^

_1:33: error: break statement not within loop or switch

break;

^

_1:34: error: case label '4' not within a switch statement

case 4:

^

_1:36: error: break statement not within loop or switch

break;

^

_1:37: error: case label '5' not within a switch statement

case 5:

^

_1:39: error: break statement not within loop or switch

break;

^

exit status 1
break statement not within loop or switch

Ah, I see the problem:

switch (phaseCount):

should be

switch (phaseCount){

Little typo.

CrossRoads:
Ah, I see the problem:

switch (phaseCount):

should be

switch (phaseCount){

Little typo.

thank you very much CrossRoads.. it works! :grin:
but how to change the frequency?

Change 1664 here:

// 3phase, 100 Hz, rising or falling edge every 1664uS ( multiple of 4uS).

if (elapsedMicros >= 1664){

1/100 Hz = 10000uS / 6 = 1667uS, round to a multiple of 4

CrossRoads:
Change 1664 here:

// 3phase, 100 Hz, rising or falling edge every 1664uS ( multiple of 4uS).

if (elapsedMicros >= 1664){

1/100 Hz = 10000uS / 6 = 1667uS, round to a multiple of 4

sorry i not get it. how about if i want to change the frequency to 20khz?

1/20000 = 50uS, 50/6 = 8uS.
So change 1664 above to 8.
Code might work, might not. 128 clocks between output changes, might be enough time.
Can change loop() a little too, this might help:

void loop(){
while (1){ // bypass main() and loop() stuff, should help run faster and with smoother output. Maybe.
currentMicros = micros();
elapsedMicros = currentMicros - previousMicros;
if (elapsedMicros >= 8){
phaseCount = phaseCount +1;
if (phaseCount == 6){ phaseCount = 0; }
switch (phaseCount){
case 0:
PORTB = 0b00000001;
break;
case 1:
PORTB = 0b00000011;
break;
case 2:
PORTB = 0b00000111;
break;
case 3:
PORTB = 0b00000110;
break;
case 4:
PORTB = 0b00000100;
break;
case 5:
PORTB = 0b00000000;
break;
} // end of switch
} // end of while
} // end of loop

after i try the coding in proteus software, the 3 signal is only separate with 60 degree phase shift (see the attachment) . how to get 120 degree phase shift different between each signal like this picture?

hi buddies

how to generate 3 pwm with 120 degree phase shift with 20khz frequency in arduino uno/Mega2560 ?

like this

azeyzoul:
hi buddies

how to generate 3 pwm with 120 degree phase shift with 20khz frequency in arduino uno/Mega2560 ?

like this

Does the Arduino have to do anything Else?

What is the PWM resolution are you need? 4bit, 8bit?

I don't see any way that Arduinos could support a 3 phase PWM at 20khz. Using Timer1, you could generate a single phase PWM.

They only way I could see this 3 phase being accomplished is to create external hardware that acts as a programmable delay line that is keyed off timer1.

Chuck.

@azeyzoul, do not cross-post. Threads merged.

chucktodd:
Does the Arduino have to do anything Else?

What is the PWM resolution are you need? 4bit, 8bit?

I don't see any way that Arduinos could support a 3 phase PWM at 20khz. Using Timer1, you could generate a single phase PWM.

They only way I could see this 3 phase being accomplished is to create external hardware that acts as a programmable delay line that is keyed off timer1.

Chuck.

i already have the output signal of 3 phase by using arduino uno with the help from @CrossRoads, i just need to modify it. i hope @CrossRoads will help me.

azeyzoul:
i already have the output signal of 3 phase by using arduino uno with the help from @CrossRoads, i just need to modify it. i hope @CrossRoads will help me.

I don't see you ever getting a 20khz three phase output.

A 20khz wave has a period of 50us. PWM a 50us signal with 8bit would need a resolution of 0.195us.

A 16mhz Arduino could execute 3 opcodes? hardly enough time to generate a single phase, let alone three phases.

20hz is possible, a thousand times slower.

If your khz was meant as hz, then it is possible.

Chuck.