Question about 6 Pin PWM on Arduino Mega

I’ve been trying 6 Pin PWM on the Arduino Mega. Didn’t see anywhere if this was officially supported, but I’ve been having mild success. Currently, I’m running open-loop because I don’t have any sensing method possible yet.

I have been trying to figure out if the 6 pin mode inverts the PWM signal in the micro controller or not. I have crappy oscilloscope skills and cant seem to tune mine in at the moment… Browsing the code, it looks like not. In src/drivers/hardware_specific/atmega2560_mcu.cpp I see that _writeDutyCycle6PWM calls _setPwmPair. In _setPwmPair I see that both high and low PWM get set the same val duty cycle, with opposing adjustments of dead_time. This leads me to believe that the signal is not inverted.

As a side note, (and I don’t fully understand how the 3-phase transition works) the line 143 does not seem correct to me. Doesn’t digitalWrite(pinL, pwm_l ? LOW : HIGH) turn the pin off when the duty cycle is 255? I ask about this segment because I am unsure if the Mega is supported, and this code could possibly be out of date or something? Just checking. Any explanation is hugely appreciated and only for my better understanding.

Also, am I correct in that hardware inverting (NOT gate) the high PWM signal is not an acceptable solution? Won’t this mess up dead time?

Lastly, are all PWM pins supported here? I am using these:

  • Pin 12 as High A
  • Pin 10 as High B
  • Pin 8 as High C
  • Pin 6 as Low A
  • Pin 4 as Low B
  • Pin 3 as Low C

Thanks for the help. I’m so excited by this project, and I’m trying to learn a lot at the same time. It’s all mixing together :confused:

Hey @azocolo,

Arduino mega as well as most of atmega chips support hardware invertion of pwm output.
So when we’re setting the pwm we dont have to do any additional invertion.

However, in some cases setting pwm to 255 or 0 does not use analogWrite internally but a digitalWrite and this digital write is not inverted.
This is a problem of arduino implementation only, so the line 143 is the fix for these spetial cases.

Now to be able to use the 6 pwm on arduino mega you need to use the high and low pair pins which belong to the same timer. Otherwise they will be out of sync.

As the mega support is still a bit undertested, there might be some issues and one of them that I see now is that there is only one pwm pin combinations supported for 6 pwm.

That would be:

  • pair 1 High: 13
  • pair 1 Low: 4
  • pair 2 High: 11
  • pair 2 Low: 12
  • pair 3 High: 10
  • pair 3 Low: 9

The number of possible cases can be extended and if you would be to do some testing I’d be happy to extend the 6PWM support to all pwm pins. :smiley:
(Because I dont have one mega2560 at the moment)