Frequency and Duty Cycle quesitons

Hello,

I'm wondering if there's an easy way to set the output of the Arduino's frequency to 50Hz. I understand that the output of the PWM pins is around 500Hz? Am I correct? I'm trying to do this to drive a couple servo motors. I also need to vary the duty cycle. I'm not sure if I should be using digitalWrite() or AnalogWrite().

Right now I'm just using this:

void setup()
{
  pinMode(10, OUTPUT);
}

void loop()
{
  
  digitalWrite(10, HIGH);
  delay(10); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(10, LOW);
  delay(10);
}

To output my 50Hz square wave. Whenever I try to use pushbuttons to simulate a signal to make the duty cycle change, the duty cycle gets all messed up.

I need to use a 5%, 7.5%, and 10% duty cycle. Any help would be appreciated.

Thank you.

I’m trying to do this to drive a couple servo motors.

Why? There is a perfectly good Servo library.

delay(10); // Approximately 10% duty cycle @ 1KHz

Not even close.

Helghast-Engineer: Any help would be appreciated.

The output timing you've described sound like the standard timing for an ordinary hobby servo. There's a standard library to drive those for you - no need to code this yourself.

If for some reason you do want to program it yourself, you could start by reading the code for the Servo library and the datasheet for the ATmega328 (sections on timers).