Generating two PWM signals with 50% duty cycle, variable frequency and one signal is 30 degrees phase shifted with another

how to produce two PWM signals with 50% duty cycle and variable frequency
second signal is 30 degree phase shifted with first signal

board: Arduino uno/Arduino Mega

I am pretty new to Arduino , to be frank I don't know how to use timers
please help me with the code

How exact must the 30 degree phase shift be?

Do not post a question in the tutorials section. This is designed for posting, guess what, tutorials. This is not a tutorial so I have moved it to a better place.

What range of frequencies?

upto 4khz

this looks like good entry level for what you are trying to do:

have a look at the " Setting an exact frequency" section.

also did some math and it you plan to use only one timer to run both PWMs then your timer period should be a multiple of (6 x target output frequency)
for example

            sig1            sig2
t=0         HIGH            LOW
t=T/6       HIGH            HIGH
t = 2*(T/6) HIGH            HIGH
t = 3*(T/6) LOW             HIGH
t = 4*(T/6) LOW             LOW
t = 5*(T/6) LOW             LOW
t = 6*(T/6) HIGH            LOW <------ or back to t=0 ;)

have a try with the code at that link I shared.

hope that helps....

sorry but that is full of confusion.

FINALLY some useful information!

If it's that slow, you can generate the two output signals entirely in software. Have a cycle of 4 points in time to be calculated repeatedly, which will each generate an interrupt when its turn comes around. So you'd have A going high, then at 30 degrees B goes high, at 180 A goes low, at 210 degrees B goes low.

You do have to decide how much resolution in frequency you need, but I'm sure you won't want to share that. Or the minimum frequency either.

Arduino Timer Phase-Shifted Square Waves

So in fact they are square waves 30* out of phase and variable frequency. If the duty cycle is always 50% then by definition they aren't "pulse width modulated"!

Thank you sir , I've manipulated OCR1B and got 30 degrees phase shift but how to change duty cycle

But you said 50% duty cycle?

I think I see your solution here ...
Working with Atmel AVR Microcontroller Basic Pulse Width Modulation (PWM) Peripheral

Try this. It uses the technique suggested by @sherzaad above.

const byte PhaseAPin = 4;
const byte PhaseBPin = 5;

unsigned Frequency = 4000;  // Should work from 1500 Hz to over 4000 Hz.


void setup()
{
  Serial.begin(115200);
  while (!Serial);

  noInterrupts ();  // protected code
  // reset Timer 1
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  TIMSK1 = 0;
  
  TIFR1 |= _BV(TOV1); // clear Overflow Flag so we don't get a bogus interrupt
  TIMSK1 |= _BV(TOIE1); // Enable Timer 1 Overflow Interrupt

  // Set TOP for desired frequency
  OCR1A = F_CPU / (Frequency * 6) - 1;
  
  TCCR1B = _BV(WGM12) | _BV(CS10); // WGM=4, start Timer 1, no prescaler

  interrupts ();
}


ISR(TIMER1_OVF_vect)
{
  static byte phase = 0;
  switch (phase++)
  {
    case 0: digitalWrite(PhaseAPin, HIGH); break;
    case 1: digitalWrite(PhaseBPin, HIGH); break;
    case 2: break;
    case 3: digitalWrite(PhaseAPin, LOW); break;
    case 4: digitalWrite(PhaseBPin, LOW); break;
    case 5: 
    default: phase = 0; break;
  }
}


void loop()
{
}

sir I am unable to understand above code . but

{
  // PUSH BUTTON PINS -- Pins need Pulled-Up Inputs

  // Timer-1 16-bit, Mode-12 CTC, Top=ICR
  // 1,000 Hz Frequency, Clock is 16 MHz

  TCCR1A=0;
  TCCR1B=0;
  
  TCCR1B = 0x18; // 0001 1000, Disable Timer
//  TCCR1B |= (1<<WGM13) | (1<<WGM12);
  TCCR1A = 0x50; // 0101 0000

  ICR1 = 2000-1;
  OCR1A = 1; // never less than 1, never more than ICR1
  OCR1B = 1001; //  using for phase shifting the signal
  // angle/180 * 2000
  TCNT1=0x0;

  TCCR1A = 0xA0;// // FOC setup to clear Wavegen output flops
  TCCR1C = 0xC0; // FOC strobe
  TCCR1A = 0x50; // 0101 0000

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

  // 2560 Timer-1 Pins
  // pinMode(11, OUTPUT); // OC1a
  // pinMode(12, OUTPUT); // OC1b

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

void loop(){}

with this code I am getting variable frequency and variable phase shift but unable to get variable duty cycle

@

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Keep working at it, and you will learn useful techniques.

Is CTC-12 Mode operation of TC1 a PWM-mode? If not, then we will not be able to change the duty cycle of the generated waveforms. The duty cycle will remain at 50% as the waveform toggles at every compare match event.

What specifically do you not understand about this code in post #14?