NANO pwm frequencies

In a project I use an RGB LED strip. I control this strip with an Arduino Nano using pins D6, D9 and D10 via MOSFETS. I noticed that the frequency of D10 is about 960Hz while D6 and D9 are about 490Hz.

I've been browsing the forum and the web to find how to set D6, D9 and D10 to the same frequency, but I only find snippets here and there or very complicated sketches impossible to understand for a simple soul like me...
It doesn't matter WHAT frequency they're set to if it's more than, let's say, 100Hz and less than 2000Hz. But I want them to be THE SAME.

Anyone has a clear and simple "straight to the point"-solution for this ?
Your answer could be helpful and save hours of searching/browsing for many Arduino fans.

Thanks in advance ! :slight_smile:

Your answer could be helpful and save hours of searching/browsing for many Arduino fans.

Changing the frequency on pin 10 to 490 (the same as the other PWM pins) isn't that difficult. You could peruse the data sheet for the AtMega328, or you could use the code you don't understand.

It is not necessary to know how video cameras, the internet, or your computer works in order to watch a you tube video.

It is no more necessary that you understand how the frequency is set, if you get the results you desire.

It IS necessary to pay attention to WHERE you post. Your post has nothing to do with the Arduino web site or the forum. So, this is NOT the place for this post. You should ask the moderators to move it.

Thanks Brattain Member,

I'm so sorry that I posted my question in the wrong section. Because you seem to know the answer, but don't want to reveal it here : where should I post this issue and get an answer (maybe from somebody else ?)

where should I post this issue

If you ask the moderators to move the post, they are usually pretty bright, and can figure out the proper place.

Have you tested all 6 PWM pins? There are 3 timers used for PWM, some using 8 bits and some using 16 bits, so there might already be 3 pins using the same frequency.

Meanwhile I kept searching and stumbled upon this :
For Arduino Uno, Nano, Micro Magician, Mini Driver, Lilly Pad and any other board using ATmega 8, 168 or 328

//---------------------------------------------- Set PWM frequency for D5 & D6 -------------------------------

//TCCR0B = TCCR0B & B11111000 | B00000001; // set timer 0 divisor to 1 for PWM frequency of 62500.00 Hz
//TCCR0B = TCCR0B & B11111000 | B00000010; // set timer 0 divisor to 8 for PWM frequency of 7812.50 Hz
TCCR0B = TCCR0B & B11111000 | B00000011; // set timer 0 divisor to 64 for PWM frequency of 976.56 Hz
//TCCR0B = TCCR0B & B11111000 | B00000100; // set timer 0 divisor to 256 for PWM frequency of 244.14 Hz
//TCCR0B = TCCR0B & B11111000 | B00000101; // set timer 0 divisor to 1024 for PWM frequency of 61.04 Hz

//---------------------------------------------- Set PWM frequency for D9 & D10 ------------------------------

//TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz
//TCCR1B = TCCR1B & B11111000 | B00000010; // set timer 1 divisor to 8 for PWM frequency of 3921.16 Hz
TCCR1B = TCCR1B & B11111000 | B00000011; // set timer 1 divisor to 64 for PWM frequency of 490.20 Hz
//TCCR1B = TCCR1B & B11111000 | B00000100; // set timer 1 divisor to 256 for PWM frequency of 122.55 Hz
//TCCR1B = TCCR1B & B11111000 | B00000101; // set timer 1 divisor to 1024 for PWM frequency of 30.64 Hz

//---------------------------------------------- Set PWM frequency for D3 & D11 ------------------------------

//TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz
//TCCR2B = TCCR2B & B11111000 | B00000010; // set timer 2 divisor to 8 for PWM frequency of 3921.16 Hz
//TCCR2B = TCCR2B & B11111000 | B00000011; // set timer 2 divisor to 32 for PWM frequency of 980.39 Hz
TCCR2B = TCCR2B & B11111000 | B00000100; // set timer 2 divisor to 64 for PWM frequency of 490.20 Hz
//TCCR2B = TCCR2B & B11111000 | B00000101; // set timer 2 divisor to 128 for PWM frequency of 245.10 Hz
//TCCR2B = TCCR2B & B11111000 | B00000110; // set timer 2 divisor to 256 for PWM frequency of 122.55 Hz
//TCCR2B = TCCR2B & B11111000 | B00000111; // set timer 2 divisor to 1024 for PWM frequency of 30.64 Hz

So it is IMPOSSIBLE to have D6,9 and 10 on the same frequency.

I hope that the moderators copy this whole thread to the right place.

Thanks.

Use a PCA9685 breakout board (Adafruit, Ebay).
Then you have 16 pins with the same PWM frequency.
And smoother 12-bit dimming.
Leo..

PaulVdB:
......I noticed that the frequency of D10 is about 960Hz while D6 and D9 are about 490Hz.

It doesn't matter WHAT frequency they're set to if it's more than, let's say, 100Hz and less than 2000Hz. But I want them to be THE SAME.

Does it matter ?

Since you are controlling an LED strip light, which is a visual output device, the human eye cannot distinguish the two frequencies. They are both way above the human eye persistence of vision threshold.

Remember that it is only in recent years that televisions have upgraded to 100Hz from 50Hz, and that normal mains-driven lighting is still 50Hz

Thanks for your reply, daba and Wawa.
I know about "visible" frequencies. As far as I remember, a human eye does not see frequencies above (about) 16 Hz. All our (AC) lights DO "blink" with 50Hz (60 in US) and we are not aware of this.
But that's not my issue.
The reason why I want all the frequencies the same is to make it easy for the power supply. Even with a "good" PSU, (I have a 12V/8A PSU and only draw 4 Amps from it) I can see on my oscilloscope that the voltage is not nice and flush when using it for three strings that vary constantly in intensity. If I disconnect the 960 Hz output, the supply voltage (as well as the light intensity) is MUCH smoother.
The LED's will be looked at from relatively close distance, so there IS (soft) flickering visible. I think I can avoid this (or at least reduce it) when all the PWM frequencies are the same.
I didn't know that (all) Arduino's use different frequencies on different PWM outputs. I should have (double)checked it before making prints etc, building the final project. Instead of using pin 6,9 and 10, I should have used 9,10 and 11 which can all be set to the SAME frequency.
Using a PCA9685 board could have been a good solution, but I only need 3 PWM's. A board with 16 outputs might be a little "overkill" and would make the project more complex but I'll experiment with it as soon it arrives from China (for the price of $5...)
Another solution might be to use 3 separate (smaller) PSU's
But the best thing would have been that instead of using pin 6,9 and 10, I should have used 9,10 and 11 which can all be set to the SAME frequency. (good for version 2.0 of my project! :slight_smile: )

Nevertheless : thanks to all who joined this issue.
I hope that more Arduino fans will benefit from this thread !

[Mod edit: Suspicious link removed]

The LED's will be looked at from relatively close distance, so there IS (soft) flickering visible. I think I can avoid this (or at least reduce it) when all the PWM frequencies are the same.

This sounds like :-

  1. a problem with your code

  2. Just an optical illusion

I can see on my oscilloscope that the voltage is not nice and flush when using it for three strings that vary constantly in intensity.

So what? This is normal, if it really is a problem then it is fixed by providing additional decoupling caps.

Mark

Oh - by the way using the same freq for all the PWMs may well make the problem VERY much worse!

M

Thanks, Holmes4 !

Not sure it's the code that makes the LED's not "stable" all the time.
I noticed that the LED's only "flicker" at certain levels of the individual colors...

  ///////////////////////////////////////
  R = A1 => D6
  G = A2 => D9
  B = A3 => D10
  W = A4 => D11
*/
void setup()
{
  // pinMode(6, OUTPUT);
  // pinMode(9, OUTPUT);
  // pinMode(10, OUTPUT);
  // pinMode(A1, INPUT);
  // pinMode(A2, INPUT);
  // pinMode(A3, INPUT);
  // pinMode(A4, INPUT);
  // Serial.begin(9600);
}

void loop()
{
  analogWrite(6, 4 + map(analogRead(A1), 0, 1023, 0, map(analogRead(A4), 0, 1023, 0, 246)));
  delay(5);
  analogWrite(9, 4 + map(analogRead(A2), 0, 1023, 0, map(analogRead(A4), 0, 1023, 0, 246)));
  delay(5);
  analogWrite(10, 4 + map(analogRead(A3), 0, 1023, 0, map(analogRead(A4), 0, 1023, 0, 246)));
  delay(5);
}

There's no difference if I enable the "Setup" or not...
D11 is not used in this project.
A4 is used to regulate the global intensity.

and here's the diagram :

Option 1 and 2 are not used in this project.

Thanks for your time !

hmmm ... seems like the picture of the diagram doesn't show up in my previous post. So I posted it here as an attachment.

PaulVdB:
Meanwhile I kept searching and stumbled upon this :
For Arduino Uno, Nano, Micro Magician, Mini Driver, Lilly Pad and any other board using ATmega 8, 168 or 328
....

So it is IMPOSSIBLE to have D6,9 and 10 on the same frequency.

Not impossible at all. That is just the prescaler. You can set PWM to whatever you want.

With chip registries you can set PWM style with TCCRA, prescale the timer with TCCRB, set your PWM frequency with ICR (this is the counter that is denominator vs chip clock speed and prescaler), and then set your duty cycle with OCR.

Just stay aware which pins are tied to which timer which it looks like you realized. Also that some other things are affected like servo motors drivers depending on which timer you modify.

This linked article got me squared away.