PWM library that handle multiple PWMs on single timer

Is there a library that allows to use PWM on multiple pin that shares the same timer and set custom frequency?
I want to use pin 2, 3 and 5. I accept, that pins that shares same timer must operate at the same frequency. I just need different duty cycles for them.

I tried AVR_PWM library, and everything works perfect when I use pins on different timers. Here of course I can even set different frequencies, but I don't want to use many timers when it can be done on one timer. Am I doing something wrong with library usage, there is bug in the library, or this library is not intended to use pany pins on single timer?

Is the only option to achieve above is to directly use timer registers to set Timer3 frequency and use built-in analogWrite? Or maybe there is library to only set timer frequency and use analogWrite? The default frequencies are too low for my application.

#include "AVR_PWM.h"

//#define pinToUse      12            // Timer1B on Mega
//#define pinToUse      11            // Timer1A on Mega
//#define pinToUse       9            // Timer2B on Mega
//#define pinToUse       2            // Timer3B on Mega
//#define pinToUse       3            // Timer3C on Mega
//#define pinToUse       5            // Timer3A on Mega
//#define pinToUse       6            // Timer4A on Mega
//#define pinToUse       7            // Timer4B on Mega
//#define pinToUse       8            // Timer4C on Mega
//#define pinToUse      46            // Timer5A on Mega
//#define pinToUse      45            // Timer5B on Mega
//#define pinToUse      44            // Timer5C on Mega

void setup() {
  AVR_PWM* PWM_Instance = new AVR_PWM(2, 5000, 0);
  AVR_PWM* PWM_Instance2 = new AVR_PWM(3, 5000, 0); // Tried with only one instance per timer, setting different pins on the same instance.

  PWM_Instance->setPWM(2, 5000, 50);
  //delay(4000); //to be able see on oscilloscope.
  PWM_Instance2->setPWM(3, 5000, 50); //That disables PWM on pin2 and enables PWM on pin3. I just want to have both
}

That's what analogWrite() does, without any special library. Setting the frequency may require a library or low level code.

Which Arduino are you using?

I'm using Ardunno Mega.
Libraries TimerOne, TimerThree etc. is the answer.

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