how to set PWM at 120Hz

Hi to all,

I have read that standard frequency for the PWM is about 488 Hz, but I need to lower it to 120 Hz for my project.

What us the easiest and most robust way to do this?

I’ve read about a solution based on adjusting the timers.
Is this the only way?

Do you have any sample to study/test?

Thank you!

Look at the TimerOne library.

.

So, I should set the period to 8333.3333333333 microseconds if I need a pwm at 120 hz?

For example, pwm(9, 512, 8333.3)?

Since I need to change the duty cycle, can I pass the duty cycle as a parameter to thus function?

This would give you ~120HZ

#include <TimerOne.h>


void setup() 
{
  pinMode(9, OUTPUT);   
  Timer1.initialize(8333);
  Timer1.pwm(9, 512); 
}
 
void loop()
{
}

Yes you can change the duty cycle as needed:

Timer1.pwm(9, myDutyCycle);

LarryD: Yes you can change the duty cycle as needed:

Timer1.pwm(9, myDutyCycle);

Can I put his even in the main loop? Or only in the setup?

Timer1.pwm(9, myDutyCycle);

Can I put his even in the main loop?

Yes.