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

**URL:** <https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704>\
**Category:** Programming\
**Created:** [November 27, 2022, 7:39pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704 "2022-11-27T19:39:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinaykumar\_b](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vinaykumar\_b](https://forum.arduino.cc/u/vinaykumar_b)\
**Post date:** [November 27, 2022, 7:39pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/1 "2022-11-27T19:39:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![herbschwarz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/herbschwarz/32/239314_2.png) [@herbschwarz](https://forum.arduino.cc/u/herbschwarz)\
**Post date:** [November 27, 2022, 7:49pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/2 "2022-11-27T19:49:28Z")

</div>

How exact must the 30 degree phase shift be?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 27, 2022, 7:54pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/3 "2022-11-27T19:54:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [November 27, 2022, 11:16pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/4 "2022-11-27T23:16:41Z")

</div>

> [@vinaykumar\_b](#):
>
> variable frequency

What range of frequencies?

---

<div class="post-metadata">

**Author:** ![vinaykumar\_b](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vinaykumar\_b](https://forum.arduino.cc/u/vinaykumar_b)\
**Post date:** [November 28, 2022, 3:02am UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/5 "2022-11-28T03:02:19Z")

</div>

upto 4khz

---

<div class="post-metadata">

**Author:** ![sherzaad](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sherzaad/32/226173_2.png) [@sherzaad](https://forum.arduino.cc/u/sherzaad)\
**Post date:** [November 28, 2022, 7:47am UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/6 "2022-11-28T07:47:04Z")

</div>

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

> **[Timer and PWM - Part 1 (8-Bit Timer0/2) Timer and PWM - Part 1 (8-Bit...](https://wolles-elektronikkiste.de/en/timer-and-pwm-part-1-8-bit-timer0-2)**
>
> Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.

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

```cpp
            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....

---

<div class="post-metadata">

**Author:** ![vinaykumar\_b](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vinaykumar\_b](https://forum.arduino.cc/u/vinaykumar_b)\
**Post date:** [November 28, 2022, 2:25pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/7 "2022-11-28T14:25:34Z")

</div>

sorry but that is full of confusion.

---

<div class="post-metadata">

**Author:** ![Beminetonight](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/beminetonight/32/1013044_2.png) [@Beminetonight](https://forum.arduino.cc/u/Beminetonight)\
**Post date:** [November 28, 2022, 3:02pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/8 "2022-11-28T15:02:17Z")

</div>

> [@vinaykumar\_b](#):
>
> upto 4khz

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.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [November 28, 2022, 3:19pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/9 "2022-11-28T15:19:45Z")

</div>

[Arduino Timer Phase-Shifted Square Waves](https://runtimemicro.com/Forums/RTM_TimerCalc/Examples-and-Tips/Arduino-Timer-Phase-Shifted-Square-Waves)

---

<div class="post-metadata">

**Author:** ![jhaine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jhaine/32/707424_2.png) [@jhaine](https://forum.arduino.cc/u/jhaine)\
**Post date:** [November 28, 2022, 4:09pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/10 "2022-11-28T16:09:37Z")

</div>

> [@vinaykumar\_b](#):
>
> how to produce two PWM signals with 50% duty cycle

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"!

---

<div class="post-metadata">

**Author:** ![vinaykumar\_b](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vinaykumar\_b](https://forum.arduino.cc/u/vinaykumar_b)\
**Post date:** [November 28, 2022, 5:23pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/11 "2022-11-28T17:23:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jhaine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jhaine/32/707424_2.png) [@jhaine](https://forum.arduino.cc/u/jhaine)\
**Post date:** [November 28, 2022, 5:25pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/12 "2022-11-28T17:25:48Z")

</div>

But you said 50% duty cycle?

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [November 28, 2022, 5:42pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/13 "2022-11-28T17:42:17Z")

</div>

I think I see your solution here ...  
[Working with Atmel AVR Microcontroller Basic Pulse Width Modulation (PWM) Peripheral](http://www.ermicro.com/blog/?p=1971)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [November 28, 2022, 7:30pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/14 "2022-11-28T19:30:14Z")

</div>

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

```cpp
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()
{
}
```

---

<div class="post-metadata">

**Author:** ![vinaykumar\_b](https://avatars.discourse-cdn.com/v4/letter/v/b5ac83/32.png) [@vinaykumar\_b](https://forum.arduino.cc/u/vinaykumar_b)\
**Post date:** [November 29, 2022, 7:59pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/15 "2022-11-29T19:59:10Z")

</div>

sir I am unable to understand above code . but

```nohighlight
{
  // 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

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [November 29, 2022, 8:32pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/16 "2022-11-29T20:32:19Z")

</div>

@

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

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behavior and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frus…

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

Thank you.

---

<div class="post-metadata">

**Author:** ![jremington](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jremington](https://forum.arduino.cc/u/jremington)\
**Post date:** [November 29, 2022, 9:04pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/17 "2022-11-29T21:04:31Z")

</div>

> [@vinaykumar\_b](#):
>
> I am unable to understand above code

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

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [December 2, 2022, 5:25pm UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/18 "2022-12-02T17:25:40Z")

</div>

> [@jremington](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [December 3, 2022, 7:29am UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/19 "2022-12-03T07:29:19Z")

</div>

> [@vinaykumar\_b](#):
>
> I am unable to understand above code

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 1, 2023, 7:30am UTC](https://forum.arduino.cc/t/generating-two-pwm-signals-with-50-duty-cycle-variable-frequency-and-one-signal-is-30-degrees-phase-shifted-with-another/1058704/20 "2023-06-01T07:30:03Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
