Skip to main content
2 of 2
deleted 3 characters in body
sa_leinad
  • 3.2k
  • 2
  • 24
  • 53

The answer is NO, changing the PWM divider for pin 11 will not affect the millis() function (nor delay()).


Explanation:
The Arduino Nano uses the ATMega328P microcontroller, same as the Arduino Uno.

Pin 11's PWM is controlled by Timer 2. By default Timer 2 has a prescale of 64 (ie. setting 0x04). The base frequency of Timer 2 is 31372.55 Hz resulting in a frequency of 490.2 Hz (ie. 31372.55/64 = 490.1961).

On the other hand, the microsecond timer (ie. the one used by millis(), micros(), delay() and delayMicroseconds()) is controlled by Timer 0.

So changing the prescaler of Timer 2 will not affect millis().

It will however change the PWM frequency of Pin 3.


Back to your question:
The possible PWM frequencies of Pin 11 (higher than 488 Hz) are:

31373 / 32 = 980.4 Hz
31373 / 8 = 3921.6 Hz
31373 / 1 = 31373 Hz

Where 32, 8, 1 are the prescaler.
Which equates to a setting value of 0x03, 0x02 & 0x01 respectively.

Add this line of code:

TCCR2B = TCCR2B & 0b11111000 | setting;

Where setting is the value of the setting for the respective prescaler.

============================================  
|| Frequency [Hz] || Prescaler || Setting ||  
============================================  
|| 31373.55       || 1         || 0x01    ||  
|| 3921.57        || 8         || 0x02    ||  
|| 980.39         || 32        || 0x03    ||  
|| 490.20         || 64        || 0x04    ||  
|| 245.10         || 128       || 0x05    ||  
|| 122.55         || 256       || 0x06    ||  
|| 30.64          || 1024      || 0x07    ||  
============================================  

Source code: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring.c

sa_leinad
  • 3.2k
  • 2
  • 24
  • 53