Independent PWM outputs with variable frequency (Arduino MEGA)

Hello!

I'm currently working on a project that requires 7 independent PWM outputs with a variable frequency from approximately 0 - 4096Hz with a Arduino MEGA.

As I write this I manage to get 4 independent outputs and understand that this is the maximum amount since the MEGA has 5 different timers that are linked between the different PWM- pins on the Arduino and that timer0 "shouldn't" be used. As you understand, this is a problem since I need 7 of these bad boys.

I'm using the PWM.h library and the frequency value is controlled by a CAN interface that sends a message to the arduino with a ID and a value of 0-100 together with some Arduino code.

#include <PWM.h>
#define FI_factor 40.96

int32_t frequency = 100;                          //Start frequency (in Hz)

int FI_1 = 3;
int FI_2 = 4;
int FI_3 = 5;
int FI_4 = 6;
int FI_5 = 7;
int FI_6 = 8;
int FI_7 = 44;

void setup() {
InitTimersSafe();                      //initialize all timers except timer0

  for (int i = 3; i <= 8; i++) {
    SetPinFrequencySafe(i, frequency);   //sets the start frequency for the specified pin
  }
  SetPinFrequencySafe(44, frequency);
}

void loop() {

if (rxId == 0x90) {                                         //rxId is the message id from CAN and checks if a message is recieved
    frequency = (rxBuf[0] * FI_factor);          //rxBuf[0] is the massage from CAN, a value between 0 - 100.
    SetPinFrequencySafe(FI_1, frequency);    //Sets the desired frequency to the chosen pin 
  }
    else if (rxId == 0x91) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_2, frequency);
  }
      else if (rxId == 0x92) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_3, frequency);
  }
      else if (rxId == 0x93) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_4, frequency);
  }
      else if (rxId == 0x94) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_5, frequency);
  }
      else if (rxId == 0x95) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_6, frequency);
  }
      else if (rxId == 0x96) {
    frequency = (rxBuf[0] * FI_factor);
    SetPinFrequencySafe(FI_7, frequency);
  }

  for (int i = 3; i <= 8; i++) {
    pwmWrite(i, 127);                          //Sets the duty cycle for a PWMs to 50%
  }
   pwmWrite(44, 127);
}

As i said, it works... but only 4 of them is independent. (As an example, pin 6 and 7 always get the same frequency value).

Is there some component or "trick" to bypass the timers or get these independent PWM outputs?
I hope you understand what i want to achieve.
Sincerely, Philip

If 5 timers is all the chip has, that's all you get. You can't magically change the hardware with code.

gfvalvo:
If 5 timers is all the chip has, that's all you get. You can't magically change the hardware with code.

Ok thank you. Then i have a follow-up question:

If I use 2 MEGAs as a master/slave. Does the timers on the MEGAs act separately?

Each will operate as determined by its own on-board crystal.

Where does the pwm signal go? Depending on the application you could use an external 8-channel pwm ic.

akerblom5356:
I'm currently working on a project that requires 7 independent PWM outputs with a variable frequency from approximately 0 - 4096Hz with a Arduino MEGA.

4096 Hz is one per 244 microsecs. Maybe you could bit-bang some of the PWMs. Maybe you could bit-bang all of them.

What is the project that requires the PWM freguency to vary over such a wide range? Usually it is sufficient to vary the duty cycle with a fixed frequency.

It is much easier to give useful advice when you describe the project so that we can understand the background to your question.

...R

gfvalvo:
Each will operate as determined by its own on-board crystal.

Wonderful, thank you for your reply!

LightuC:
Where does the pwm signal go? Depending on the application you could use an external 8-channel pwm ic.

Its going to be used to symbolize various inputs on a machine that are connected to different sensors that are dependent on frequency.

Ive looked into that and I dont think that will work without getting the same frequency.

akerblom5356:
Its going to be used to symbolize various inputs on a machine that are connected to different sensors that are dependent on frequency.

You need to provide a much more comprehensive description of your project.

If things depend on frequency, why is there a need for PWM ?

...R