2 shifted PWM using Arduino programming

Hi there. I am working in a project that need specific requirements, so I need some help:
I need to make 2 PWM with variable duty cycle which are controlled by means of potentiometer, The PWM frequency should be 30 khz. I have done all of that but I can't do it with the following challenge. I need those 2 PWM to be shifted from each other half cycle. And running simultaneously with each other. This is my code:

#define PWM_1 9
#define PWM_2 10
#define POT A0

void setup() {
  TCCR1B = TCCR1B & B11111000 | B00000001;
  pinMode(PWM_1, OUTPUT);
  pinMode(PWM_2, OUTPUT);
  pinMode(POT, INPUT);
}

void loop() {
  long dutyCycle = map(analogRead(POT), 0, 1023, 0, 255);
  analogWrite(PWM_1, dutyCycle);
  analogWrite(PWM_2, dutyCycle);
}

I am using Arduino nano ATmega328
As you see, and when I run it on an oscilloscope simulation, the observation was like this:

And this is the simple connection:

I need to make one of the PWM shifted from the other half cycle. After days of research, I couldn't find it. I hope you can help me theoretically and with code modifications. Thanks a lot

Are you looking for a quadrature generator?

Invert the second channel for 180° phase shift.

No, My project is about controlling 2 parallel buck chopper, each is controlled by means of MOSFETS, these MOSFETS are switching On or Off according to the high or low of the Pulse came from my PWM. And The task is to phase shift between the 2 PWM while controlling their duty cycle by means of Potentiometer.

Okay, How to do it in coding? I know the theoretical rule, But How to do it in coding?

Read The Fine Manual and/or read the Related Topics down here.

You'll have to know more about coding for timers when you want a variable duty cycle.

I am about a week of reading, watching and writing, and the using some oscilloscope simulation on my laptop to help, but in vain. Can you help with a code modification?

What do you want the waveform phasing to look like with duty cycle 90%? Wouldn't overlapping be a problem?

No absolutely not, According to my project, the overlapping is a mandatory when I increase the PWM Duty cycle of both, as it will offer higher voltage for the load. So there is no problem with the overlapping.

I really need help to achieve this

For inverse output in Fast PWM Mode you have to set COM1A0 etc. as desired, see Table 16-2. Or Table 16-3 for the other PWM modes.

A timing diagram to illustrate what you are trying to achieve would avoid a hell lot of confusions!

"A picture is worth a thousand words..."

if you share a couple of scenarios, it would help us help you find a suitable solution! :wink:

1 Like

I am sorry, But I don't understand What should I do...
Sorry for that, I need more details

it really all depends WHERE OP want to phase shift the pulse... and that is why I asked for timing diagram as it may not be that straight forward as much as it could be! :slight_smile:

Maybe OP should study this a little:
https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm/

I searched and searched, And now by the help of a software, I could make a shift, but then couldn't control the duty cycle effectively, here is an update if you can help.

#define PWM_1 9
#define PWM_2 10

void setup() {
  TCCR1B = 0x18; // 0001 1000, Disable Timer 
  TCCR1A = 0x50; // 0101 0000

  ICR1 = 267-1;
  OCR1A = (int) (ICR1 * 0.01);
  OCR1B = (int) (ICR1 * 0.09);
  TCNT1=0x0;

  TCCR1A = 0xA0; // FOC setup 
  TCCR1C = 0xC0; // FOC strobe 
  TCCR1A = 0x50; // 0101 0000

  // UnComment following lines for UNO-NANO Timer-1 Pins 
  pinMode(9, OUTPUT);  // OC1a
  pinMode(10, OUTPUT); // OC1b

  TCCR1B |= 1; // Prescale=1, Enable Timer 
}

void loop() {
  analogWrite(PWM_1, 255);
  analogWrite(PWM_2, 255);
}

And here is the output:

The phase shift is almost done (I am not sure) But analogWrite is not effective in duty cycle controlling. as with any modification instead of 255, make no output at all.

Please help me

You have to set OCR1x in code.

Which PWM mode are you using now?

For your intended phase shift you should use phase (and frequency) correct PWM mode.

1 Like

Sounds tricky. You need 4 transitions at different times, all in sync with each other, and with the 180° shifted transitions overlapping the rollover, so most of the PWM modes won't work.

Maybe you could use timer1 in CTC mode with ICR1 (WGM=12) to set the frequency, toggle OC1A and OC1B on match, and also do some awkward interrupt trickery to update OCR1A and OCR1B when the matches happen to match the duty cycles.

How much PWM resolution do you need? Would a software PWM based on micros() or a counter be good enough?

1 Like

Mode, As a newbie in Arduino development, I don't know what PWM mode is, But I will describe what I want do.

I want 2 PWM to operate at same time, one on pin 9, and the other on pin 10, that's for using Timer1 which is 16-bit timer and can generate higher frequency about 30 khz. The controlling over the PWM duty cycle will be done by using potentiometer, from 0 to 5v. I hope it well be the answer, and for that, I supplied my 2 written codes.

In the 25% case, trace#2 looks like 50%.

RTFM

1 Like

The PWM frequency should be 30 khz (high freq, I know) But is a mandatory, so I thinks it will be 16-bit for Timer 1, as timer 0 can't get this higher freq.

Note: I am using Arduino nano.

Sorry for that I am a beginner in that