Changing the PWM frequency on a MEGA2560

Hello, I apologize in advance as I know this is already covered in different posts but I am having trouble understanding how to do this for my application.

I am creating a 24v CCT LED dimmer, one potentiometer controls brightness and one controls the balance of warm to cool light. The code works fine but I want to increase the PWM frequency to reduce flicker in images and videos. I think I need it above 1khz.

What would I add to my code to make this modification to the standard frequency of the controller? I know that different pins use different timers, I am open to switching to different pins if needed. Thanks for the help!

// Pin Definitions
const int brightnessPotPin = A0;  // Potentiometer for overall brightness
const int balancePotPin = A1;     // Potentiometer for warm/cool balance
const int warmLEDPin = 3;         // PWM output pin for warm LEDs
const int coolLEDPin = 5;         // PWM output pin for cool LEDs

void setup() {
  // Set LED pins as outputs
  pinMode(warmLEDPin, OUTPUT);
  pinMode(coolLEDPin, OUTPUT);
}

void loop() {
  // Read the overall brightness potentiometer (0 to 1023)
  int brightnessValue = analogRead(brightnessPotPin);
  // Map the brightness value to the PWM range (0 to 255)
  int brightnessPWM = map(brightnessValue, 0, 1023, 0, 255);

  // Read the warm/cool balance potentiometer (0 to 1023)
  int balanceValue = analogRead(balancePotPin);
  // Map the balance value to percentages for warm and cool LEDs
  int warmRatio = map(balanceValue, 0, 1023, 255, 0);  // Warm proportion
  int coolRatio = map(balanceValue, 0, 1023, 0, 255);  // Cool proportion

  // Scale the brightness for warm and cool LEDs
  int warmPWM = (brightnessPWM * warmRatio) / 255;  // Brightness scaled by warm proportion
  int coolPWM = (brightnessPWM * coolRatio) / 255;  // Brightness scaled by cool proportion

  // Set the PWM output for warm and cool LEDs
  analogWrite(warmLEDPin, warmPWM);
  analogWrite(coolLEDPin, coolPWM);
}

analogWrite() generates a PWM signal with about 700 Hz. With 700 Hz you will not see any flickering. But in a video with high frame frequency there might be stroboscopic effects (like a wheel turning backwards).

Thanks for the response, are you sure? When I looked it up I read that the analogwrite() generates 490hz.

Hi @nonameuno ,

here is a webpage that might be of assistance:

https://arduinoinfo.mywikis.net/wiki/Arduino-PWM-Frequenc

and it links to this page also

https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm/

Good luck!
ec2021

1 Like

I don't personally know how to do it, but a description appears to be on page 121-122 of the datasheet, which can be downloaded here. It looks like frequencies of up to IO_freq/256 can be generated.

1 Like

Thank you so much @ec2021 . This is very informative. Really appreciate it

Perfect, thank you @jaguilar84 !

When I looked it up I read that the analogwrite() generates 490hz.

OK, I guess 700 Hz is for another controller. But nevertheless analogWrite() generates PWM with very low frequency, sufficient for dimming LEDs, etc..
Much higher frequencies can be achieved by direct programming timer registers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.