Go Down

Topic: Issue about use Servo library and Timer 2 Interrupt (Read 254 times) previous topic - next topic


Oct 20, 2018, 01:51 pm Last Edit: Oct 21, 2018, 06:04 am by Robo_mechatronics
Hi everyone,

My project have issue, I using Servo library (controll servo) and Interrupt Timer 2 (Sampling PD Controller, dt = 1ms).

I known Servo.h using Timer1, so I use Timer2 to sampling. But Servo motor have issue.

I writed code as below:

Code: [Select]

#include <TimerOne.h>
#include <Servo.h>

#define servoPin1 9
#define servoPin2 10

Servo Servo1;
Servo Servo2;

//Offset Setpoint [pixel].
float r = 0;
//Input, Output u(k - 1), Error e(k-1))
float y, u = 0, u0 = 0, e0 = 0;
//Sampling Time, [ms]
float dt = 1;
//Parameters of PD controller.
float Kp = 0.5, Kd = 0.2;
//Position of camera
unsigned char pitch = 100, yaw = 90;
void PDcontroller() {
  float e, P, D;
  e = r - y;
  P = Kp * e;
  D = Kd * (e - e0) / dt;
  u = u0 + P + D;
  u0 = u;
  e0 = e;

void CameraPos(unsigned char pitch, unsigned char yaw) {

unsigned char TimerValue() {
  float temp;
  temp = 256 - dt * 1000 / 4;
  return (unsigned char)temp;
void SetTimer2(){
  TCCR2A = 0;
  TCCR2B = 0;
  TIMSK2 = 0;
  TIMSK2 = (TCCR2B & B11111110) | 0x01;
  TCCR2B = (TCCR2B & B11111000) | 0x04;
  TCNT2 = TimerValue();
// **************************************************
void setup()

  CameraPos(pitch, yaw);
// **************************************************//
void loop(){}

// **************************************************//
ISR(TIMER2_OVF_vect) {
  TCNT2 = TimerValue();

Issue: ISR is run okay, but servo motor is run, after it not keep at setup position(pitch and yaw angle).

I would you like help me.

Thank you and best regard,


Please read the "How to use this forum" post and follow the directions.

Post the code, using code tags, and explain clearly what you mean by "I can't control ...", that is, explain what you expect to happen, and what happens instead.


Why have you a reference to a TimerOne library? Timer1 is used by the Servo library.

How often is your Timer2 ISR being triggered? If it is too often it may not allow time for other code to run.

Two or three hours spent thinking and reading documentation solves most programming problems.


Thank your reply.

(1) I will reference to a TimerOne library.
(2) Period: 1ms. Maybe you are right.


(1) I will reference to a TimerOne library.
That does not make sense. Maybe you mean that you are going to remove the reference

(2) Period: 1ms. Maybe you are right.
Interrupts at 1 millisec intervals should not be a problem.

However when I look more closely at the code in your ISR I suspect the problem is that it takes too long to complete. Interrupts are halted in an ISR so other code that relies on them can't work.

Rather than call  TimerValue() and PDcontroller() from within the ISR just set a flag and call those functions from loop() after the ISR has returned.

And if you just want something to happen every millisec I can't see any need for an ISR. Just use micros() to do the timing.

Two or three hours spent thinking and reading documentation solves most programming problems.


 :) Thank you so much. :)

I understand your solution!

I fixed it!

Thank you,


Go Up