PWM library and Pin 6 problem

I have IDE 1.8.2 and PWM library (Arduino PWM Frequency Library v_05.zip) running on a Nano.

I can vary the PWM frequency on pins 3, 5, 9 and 10 but pin 6 is either HIGH or LOW with the ‘…Safe’ function calls, as observed on my oscilloscope.

With the ‘normal’ calls I also lose control of pin 5.

Any advice as to how to get pin 6 to work as PWM?

Thank you
Alan

‘Safe’ calls…

#include <PWM.h>

void setup() {
  Serial.begin(250000);
  
  InitTimersSafe();

  long lFrequency = 4000;

  SetPinFrequencySafe(3, lFrequency);
  SetPinFrequencySafe(5, lFrequency);
  SetPinFrequencySafe(6, lFrequency);
  SetPinFrequencySafe(9, lFrequency);
  SetPinFrequencySafe(10, lFrequency);

  pwmWrite(3, 96);
  pwmWrite(5, 96);
  pwmWrite(6, 96);
  pwmWrite(9, 96);
  pwmWrite(10, 96);

}

void loop() {
}

and ‘normal’ calls…

#include <PWM.h>

void setup() {
  Serial.begin(250000);
  
  InitTimers();

  long lFrequency = 4000;

  SetPinFrequency(3, lFrequency);
  SetPinFrequency(5, lFrequency);
  SetPinFrequency(6, lFrequency);
  SetPinFrequency(9, lFrequency);
  SetPinFrequency(10, lFrequency);

  pwmWrite(3, 96);
  pwmWrite(5, 96);
  pwmWrite(6, 96);
  pwmWrite(9, 96);
  pwmWrite(10, 96);

}

void loop() {
}

Can't help with the problem, but for anyone interested here are links to the library download and the original forum post about it.

Pins 5 and 6 are controlled by Timer0.

The library has five global functions:

InitTimers()

Initializes all timers. Needs to be called before changing the timers frequency or setting the duty on a pin

InitTimersSafe()

Same as InitTimers() except timer 0 is not initialized in order to preserve time keeping functions

pwmWrite(uint8_t pin, uint8_t val)

Same as ‘analogWrite()’, but it only works with initialized timers. Continue to use analogWrite() on uninitialized timers

SetPinFrequency(int8_t pin, int32_t frequency)

Sets the pin’s frequency (in Hz) and returns a bool for success

SetPinFrequencySafe(int8_t pin, int32_t frequency)

Same as SetPinFrequency except it does not affect timer 0

Does this explain why pin 6 isn't working?

You are not using the library correctly.

Do not use SetPinFrequencySafe() or pwm.Write() on pins 5 or 6 when using InitTimersSafe() mode. Use analogWrite() on these pins. The pwm frequency will 980 Hz and won't be adjustable.

InitTimers() will let you adjust the frequency of pwm on pins 5 and 6, with SetPinFrequency() and pwm.Write() but the millis()and micros() functions will not work normally.

I wish I hadn't posted the ...Safe code. I did it to show I had actually experimented with both.

My problem lies with the non...Safe version which to my mind should allow pin 6 to be set. Which it isn't

To vary the frequency on the 8 bit timers (Timer 0 and Timer 2) the output A pin can not be used for pwm because the output compare values (OCR0A,OCR2A) are used to set the frequency. In this mode you can't get pwm output on pin D6 (Timer 0) or pin D11 (Timer 2).

cattledog:
To vary the frequency on the 8 bit timers (Timer 0 and Timer 2) the output A pin can not be used for pwm because the output compare values (OCR0A,OCR2A) are used to set the frequency. In this mode you can't get pwm output on pin D6 (Timer 0) or pin D11 (Timer 2).

In which mode is that? Which mode can you set 6 and 11... None?

In which mode is that?

The "not Safe" mode using

InitTimers(), SetPinFrequency(), pwmWrite()

You can not get variable frequencies on pin 6 or pin 11 using that library.

You can get some different frequencies using the timer prescalers, or get 50% duty cycle at half frequency using direct register commands, but that's not what the library is doing.

A good reference is https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM