PWM Pin Frequency

Hi , it might help if I first say what I am trying to achieve
I want to slow down a servo on a R/C model
so my idea is to lower the repetition speed of the PWM signal but keep the duty cycle the same.

My plan is to first find what duty cycle the receiver is requesting using pulsein() so I wrote a test

int width ; // variable to hold the width of the pulse

void setup() {
pinMode(5,INPUT);
pinMode (3,OUTPUT);
Serial.begin(9600);
}

void loop() {
width = pulseIn (5,HIGH); // read the pulse width from when it goes high till when it goes back low
Serial.println(width); // and print it out
if (width >1500) // 1500 uS is mid point of possible widths ( 1000 uS - 2000 uS)
{ Serial.println ("ON"); // if its above 1500 turn the output on and tell me its on
digitalWrite(3,HIGH);
}
else
{
Serial.println("off"); // or if below 1500 turn it off
digitalWrite(3,LOW);
}
}

This worked fine ,
so my next plan is to output a PWM pulse that is the same as the pulse I read with pulsein()
but to change the repetition frequency of the PWM pin to a slower speed so the servo will move to the same place but take longer to get there.
I read that to change the frequency I should use one of these lines

TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz

TCCR1B = TCCR1B & B11111000 | B00000011; // for PWM frequency of 490.20 Hz (The DEFAULT)

TCCR1B = TCCR1B & B11111000 | B00000100; // for PWM frequency of 122.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz

but my question is where do I put the line ?
Does it go in the setup ?
Does it go in the main loop ?
or should I forget this idea and do it a different way altogether ?
If I change the frequency does it hold true untill I change it again?

Regards
Don

Hi , it might help if I first say what I am trying to achieve
I want to slow down a servo on a R/C model
so my idea is to lower the repetition speed of the PWM signal but keep the duty cycle the same.

Won’t work

Slow down the rate of change of the “angle” you write using the Servo library.

Have a look at VarSpeedServo.h library in place of Servo.h. Saves a lot of messing about.

Or just use the technique from the Sweep example in the IDE.

Servos work on absolute pulse widths so changing the repetition rate of those pulses won't do anything useful.

Steve

 TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz
 TCCR1B = TCCR1B & B11111000 | B00000010; // for PWM frequency of 3921.16 Hz
 TCCR1B = TCCR1B & B11111000 | B00000011; // for PWM frequency of 490.20 Hz (The DEFAULT)
 TCCR1B = TCCR1B & B11111000 | B00000100; // for PWM frequency of 122.55 Hz
 TCCR1B = TCCR1B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz

Uh… Servo pulses are typically repeated at 50 Hz (20 milliseconds). I don’t think slowing down the rate of sending will ‘slow down’ the servo motion. The servo mostly cares about how long the pulse is, not how often it gets a pulse.

You can experiment by flipping the servo back and forth, slowly increasing the delay between pulses. Using Pin 13 for the servo will let you see the pulses on the LED.

const byte ServoPin = LED_BUILTIN;  // 13


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


void loop()
{
  static unsigned interPulseDelay = 20;  // Start at about 50 Hz.


  // Move to angle 0 for about a second
  for (unsigned  i = 0; i < 1000 / (interPulseDelay + 1); i++)
  {
    // Angle 0
    digitalWrite(ServoPin, HIGH);
    delay(1);
    digitalWrite(ServoPin, LOW);


    delay(interPulseDelay);
  }


  // Move to angle 180 for about a second
  for (unsigned i = 0; i < 1000 / (interPulseDelay + 2); i++)
  {
    // Angle 180
    digitalWrite(ServoPin, HIGH);
    delay(2);
    digitalWrite(ServoPin, LOW);


    delay(interPulseDelay);
  }


  // Increase delay
  interPulseDelay += 5;
}