How to make 6 independent PWM on 6 timer using Arduino Mega?

I am a student of BS in electrical engineering and working on a project.

I can not create switching patterns as shown in diagram. I read the datasheet of (ATmega640/1280/1281/2560/2561) and i know which bit I want High to create (phase_correct_PWM) and inverse it by COM bit in timer.

But i am having difficulty in Coding it for 6 PWM.
Can you help me in coding Please? the diagram is given.

Image from Original Post so we don’t have to download it. See this Image Guide



Your diagram seems to show a constant pulse width and a varying voltage. An Atmega microprocessor can only produce a variable (or constant) pulse width at a constant voltage.

Maybe a better explanation of what you are trying to achieve would help?


Sorry for incomplete information…

My project is Single Phase Z-source Buck Boost Matrix Converter (IEEE transaction).it buck and boost voltage as well as change frequency at output .

The input is AC wave, we divide AC_wave_one_complete_cycle into 4 equal parts by using opto coupler zero crossing detection system.
10 (ten) switches(IGBTs) are used by the author in project which is controlled by Ardino Mega.

Now if the Arduino receive (for example 10 in binary) at input pins meaning it is in one fourth of the input cycle now arduino will operate 10 switches like shown in above diagram’s.

S1a, S2b, S4a, Ssb get on. (simple, can be done)

S2a, S3a get off. (simple, can be done)

S1b, S4b, Ssb produce PWM (dont know how to using separate timer for separate switch in

S3b Produce Inverse PWM (dont know how to using separate timer for separate switch in coding)

and again after the quarter of the input cycle, it changes switching patter again as shown in diagram…

after changing switching patter 4 times in one cycle it repeat it and follows same pattern again and again.

Transaction link :
PWM on 8-bit timer link:

the file is also attached.
Please Help me i am in big trouble now.Untitled.png

I am completely out of my depth as far as your electronics is concerned.

I suspect I would be able to deal with the piece I have quoted if I understood it properly. Can you express all those Sna names in terms of Arduino pin numbers?

What value (or range of values) do you want for your PWM outputs?

What do you mean by "inverse PWM" - if you just mean a value that is (255 - pwmVal) then there should be no problem.


the Arduino Mega has 6 timer as shown below..

now the purposed switching strategy is given below.

Now the arduino pins is assigned to switches... as shown below..

now 8 bit register is given below

to creat PWM we have this bits to control as shown in below

we get output as shown below..


the complete link video about PWM and Inverse of PWM is shown below

8. Arduino Timers and Counters - YouTube

now in coding by using four if statements, we can control the switching strategy.
in each if statement the 10 switches can be controlled in which 6 pins are PWM ...

I know only that further about programming...

a simple body is shown below..



6 out of 10 switches we operate 5 timers pin 13, 12, 10, 5, 6, 7;
and other switches are simply turned on or off;


6 out of 10 switches we operate 5 timers pin 13, 12, 10, 5, 6, 7;
and other switches are simply turned on or off;


6 out of 10 switches we operate 5 timers pin 13, 12, 10, 5, 6, 7;
and other switches are simply turned on or off;


6 out of 10 switches we operate 5 timers pin 13, 12, 10, 5, 6, 7;
and other switches are simply turned on or off;

please i know that far.. if i missed any information.. please ask..


base on what I can understand from your post, i'm interpreting it as operating a stepper motor operation. switches are operated synchrounously not asynchrounously

I coded my 4 wires stepper motor for continuous rotation using only one timer as follows :

const uint8_t stepper_out[4]={0x9F,0x5F,0x6F,0xAF}; //each array cell contains the respective output states of the 4 wires for one step.  

void timer1_intr(){
     PORTD=stepper_out[step_cnt]; //used direct port manipulation for simultaneous output (using a Uno board)
     step_cnt = (step_cnt+1)%4; //increment step value in a circular manner
     //cycling through all 4 values consecutively results in one revolution.

you could potentially implement something similar using the different opto_coupler value to change the switch states.

hope that helps.

well thanks for your time. Hopefully i will extract some information as you advised me.

Can you send me 16-bit timer1 PWM coding of arduino IDE? with a little explainations?

thanks for your coordination...

There are already some libraries available online to help using Timer1.
you can file one here:

some explanation about using timer on AVR here:

if you need more help you can still post your questions on this thread! :slight_smile:

Does this mean that you are trying to control a stepper motor but did not bother to tell us in your Original Post?


no i m not using stepper motor control.the problem is still on… i have sent you the link of my project my project is not stepper motor i just need 6 PWM pins only by using different timers… i am just having idea that i will figure it out my trying harder…

That's what I thought so I could not understand the purpose of Reply #6.

I still don't understand what is in your mind when you say "6 PWM pins only by using different timers". Each of the hardware Timers can manage two PWM outputs and on the Mega IIRC some can manage three.

I cannot make sense of all that is in your very long Reply #5. It reads like you have everything figured out - maybe I just don't have the stamina to read it carefully? Or maybe it needs to be read by someone who understands how to make a "Single Phase Z-source Buck Boost Matrix Converter (IEEE transaction)"


so I could not understand the purpose of Reply #6.

He is "a student of BS in electrical engineering and working on a project". So I was giving him pointers not the actual solution with an example where the state multiple outputs(4) was controlled with a single timer.

I personally think that he does not really need 6 timers to operate the converter...also if he needs to inverted PWM, I would rather use an inverting buffer at the output of the non-inverted PWM as a simpler solution.

Robin2 i can not use 2 PWM channel of a same timer... because both Pins of timer will give me ether inverting or non-inverting PWM but i need PWM and inverting-PWM both and i can not change pins physically.
lets have an example of switch S1b, i assign channel A pin 13 on timer0, and S3b i assign channel B
pin 4 of same timer 0. now even at first the one switch need PWM and other switch need inverting PWM. now the timer can changes both channels to PWM or both to inverting PWM.... but can not change one of them as according to my knowledge.
Each switch for some time duration differs from other switch.

i can convert PWM to complete on state by changing duty cycle to 100% or off state by duty cycle to 0% as at some places switchs behave like according to diagram. but can not change PWM to Inverting PWM on different channel of a same timer...

Sherzaad Thanks for your advise but i can not use not gate because after some time my switching pattern will change like PWM to inverting PWM,, than what will i do?. the best answer to a solution is to use different timer for different PWM-switches(S1b, S2b, S3b, S4b, Ssa, Ssb). this way the each timer will control only one switch and convert PWM into inverting PWM by changing COM bits or ON state by 100% duty cycle and OFF state by 0% duty cycle..

i have found the 8-bit timer PWM program.

void setup()
DDRD= (1<<5)|(1<<6);

OCR0A=128;//duty cycle for A channel.


16 bit timer PWM

DDRB = 0x20; //set port B bit five to output
//fast pwm, set on match, clear at top, ICR1 holds TOP
TCCR1A |= (1<<COM1A1) | (1<<COM1A0) | (1<<WGM11);
//use ICR1 as source for TOP, use clk/1
TCCR1B |= (1<<WGM13) | (1<< WGM12) | (1<<CS10);
//no forced compare
TCCR1C = 0x00;
//20% duty cycle, LED is a bit dimmer
OCR1A = 0xC000; //set at 0xC000
ICR1 = 0xF000; //clear at 0xF000

now i need to combine them in one program for further proceed.

i will assign pins to switches... dont know how yet but i will try my best...

That is straightforward but I find the rest of your explanation almost incomprehensible. It may make sense to you - but as an engineer you will have to be able to write stuff that makes sense for your clients.

One possible solution might be not to use the Timers to trigger the PWM pins directly but rather to trigger interrupts and then you can have a few lines of code in the ISR that actually toggles an I/O pin any way you want.


Well i tried my best to explain it but it was to much cloudy that it almost made me confuse how to elaborate it. sorry me bad.

Robin2 as you advised me about trigger interrupts, i don’t know programming much. Can you help me how trigger interrupts works with example if you may please?

Now can you tell me is this a to OFF the timer can i do simply?
can i off the timer by lowering all bits to 0.?

as i have in my mind that i will use 5 timer in my first if conditions out of 4. than i can turn off all timers and than in next if statement i can change timers PWM Patterns

like if one pin was producing PWM by timer 0 than i will turn off the timer and in next if, i will assign bits of my choice.


 now i use timer for PWM
DDRD= (1<<5)|(1<<6);

and other 4 timers for other switches which i will learn.

now outside the loop i turn off all timers 


and now i will turn ON all 5 timers which i am using above with different choice of bit as i please.

now outside the loop i turn off all timers 


and now i will turn ON all 5 timers which i am using above with different choice of bit as i please.

now outside the loop i turn off all timers


same here 

}\\loop ends
program ends

if you are thinking of delays i am producing, that arduino will run serial data (meaning code will execute first at first line and last switch instruction will execute at last) and there will be micro seconds delay between first and last switch operation...

i have a solution for that, i will use Hex-Buffer at the output of arduino. The buffer IC will only active when Vcc is apply and Vcc will be controlled by arduino it self. so 1st switch instruction will run but not go to output device than 2nd and 3rd. When all 10 switches instruction will execute, than Buffer Vcc will high.Than all pins will send parallel data.
and in our project output signal, there will be no current spikes to distort the signal.

In this way the output device will receive Parallel data transfer. otherwise i have to use PLCs for my project which is expensive than arduino Mega.

This practices is performed in some other type project.

I'm not sure I can help any further. I don't really understand what you do and don't know about programming.

Figuring out what all the Timer registers do is (for me, at least) a very time consuming business needing constant reference to the Atmega datasheet and frequent testing. I am only prepared to devote that effort to my own projects.

You have never posted a complete program (even one with errors) so it is impossible to understand the context of the various snippets you are posting.

If you are planning a complex program I suspect WHILE has no place in it and especially an infinite while(1).

EVERY successful program is a collection of small pieces that work together. I wonder if you are trying to eat your buffalo in one bite. Write a short program that makes one part work. Then write another short program to figure out how to do another part. When you can do all the parts separately then start combining them. One advantage of doing it piece by piece is that it will be easier to get help here.

Planning and Implementing a Program

thanks i will do as you advised my...

Programming Code for 16-bit register 3 and 4 with freedom to access any channel for invert or non invert PWM... but i needed the timer for 5 millisecond.
After 5 millisecond it turn off and after 5 millisecond it again turn on..
is there any interrupt command to to off timer and than on it?


void setup() 
  TCCR3A = 0;
  TCCR3B = 0;
  TCCR3C = 0;
  TCCR4A = 0;
  TCCR4B = 0;
  TCCR4C = 0;  

     /* Set Timer3 pins as output :

  Channel A : Arduino mega Digital Pin 5 / Chip Pin 5 (PE3/OC3A)
  Channel B : Arduino mega Digital Pin 2 / Chip Pin 6 (PE4/OC3B)
  Channel C : Arduino mega Digital Pin 3 / Chip Pin 7 (PE5/OC3C)*/ 

  DDRE = DDRE|(1<<DDE3)|(1<<DDE4)|(1<<DDE5);    
/* Set Timer4 pins as output :

  Channel A : Arduino mega Digital Pin 6 / Chip Pin 15 (PH3/OC4A)
  Channel B : Arduino mega Digital Pin 7 / Chip Pin 16(PH4/OC4B)
  Channel C : Arduino mega Digital Pin 8 / Chip Pin 17 (PH5/OC4C)*/ 
  DDRH = DDRH|(1<<DDH3)|(1<<DDH4)|(1<<DDH5);


void loop()

      /* Waveform Generation Mode
  mode 1   Phase Corrent PWM, 8-bit, TOP = OCRxN, set = bottom ,
  => WGM33|WGM32|WGM31|WGM30 = 0|0|0|1 

      Fast PWM, non-inverting mode
  => COMxA1|COMx0|COMxB1|COMxB0|COMxC1|COMxC0 = 1|0|1|0|1|0*/

/*I can get inverting or non inverting PWM of any channel by Cnanging COM bits.*/

  TCCR3A = TCCR3A|(1<<COM3A1)|(1<<COM3A0)|(1<<COM3B1)|(0<<COM3B0)|(1<<COM3C1)|

  TCCR3B = TCCR3B|(0<<WGM33)|(0<<WGM32)|(1<<CS32)|(0<<CS31)|(1<<CS30);

  TCCR4A = TCCR4A|(1<<COM4A1)|(0<<COM4A0)|(1<<COM4B1)|(0<<COM4B0)|(1<<COM4C1)|(1<<COM4C0)|(0<<WGM41)|(1<<WGM40);

  TCCR4B = TCCR4B|(0<<WGM43)|(0<<WGM42)|(1<<CS42)|(0<<CS41)|(1<<CS40);

      /* Set PWM Duty Cycles. For example you may define any number in OCRxAL*/

//timer 3
  OCR3AH = 0;
  OCR3AL = 200;

  OCR3BH = 0;
  OCR3BL = 200;

  OCR3CH = 0;
  OCR3CL = 200;

//timer 4
  OCR4AH = 0;
  OCR4AL = 1;

  OCR4BH = 0;
  OCR4BL = 200;

  OCR4CH = 0;
  OCR4CL = 200;



on simulation it satisfy my need...

Is there any method that i can OFF timer by using any interrupt command??? or some else?