# Using ServoTimer2 library

**URL:** <https://forum.arduino.cc/t/using-servotimer2-library/1011060>\
**Category:** Programming\
**Created:** [July 10, 2022, 9:31am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060 "2022-07-10T09:31:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 9:31am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/1 "2022-07-10T09:31:29Z")

</div>

I am using ServoTimer2 library since the other libraries in my code were using same interrupt as Servo library. However, I observe that when I use the code below, the servo motor rotates smoothly

```cpp
servo1.write(750); //min pulse width for 0 degree
delay(1000);
servo1.write(1500); //pulse width for 90 degree
delay(1000);
servo1.write(2250); //max pulse width for around 180 degree
delay(1000);

```

but when I try to do it as shown below, it works really slow although the delay is small

```cpp
for (int i=750; i<1500; i++){
      servo1.write(i);
      delay(5);
  }

  for (int i=1500; i>750; i--){
      servo1.write(i);
      delay(5);
 }

```

Any idea why is it so?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 9:34am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/2 "2022-07-10T09:34:59Z")

</div>

You say that the delay is small but 750 \* 5 milliseconds is 3.75 seconds. Does that correspond to how long the servo takes to move or is it longer ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 9:48am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/3 "2022-07-10T09:48:04Z")

</div>

I reduced the delay to 1 sec. It works fine but i don't know why it is not going smoothly.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 10, 2022, 9:48am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/4 "2022-07-10T09:48:15Z")

</div>

You should probably use [the writeMicroseconds() function](https://www.arduino.cc/reference/en/libraries/servo/writemicroseconds/) to write those type of values, not write. It makes it more clear what you are doing.  
**Not** with ServoTimer2.

Will your servos actually go to the position referenced by 750us? If not you could be stalling the servo against its stops and that can damage the servo. That applies to the high end, too. Not all servos will actually go to 0 or 180 without stalling.

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 9:57am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/5 "2022-07-10T09:57:00Z")

</div>

Thanks for the suggestion!  
Yes, the servo does not go to the position referenced.

When I use the writeMicroseconds function, I get the following error:

backservo:42:14: error: 'class ServoTimer2' has no member named 'writeMicroseconds'  
servo1.writeMicroseconds(i);

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [July 10, 2022, 9:59am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/6 "2022-07-10T09:59:55Z")

</div>

> [@simdick](#):
>
> backservo:42:14: error: 'class ServoTimer2' has no member named 'writeMicroseconds'  
> servo1.writeMicroseconds(i);

**My apologies.** Somewhere between my eyes and brain the reference to ServoTimer2 got lost. I will wait for the coffee to be done before I post any more.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 10:26am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/7 "2022-07-10T10:26:38Z")

</div>

> [@simdick](#):
>
> I reduced the delay to 1 sec.

Do you mean that you reduced the delay to1 millisecond ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 10:45am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/8 "2022-07-10T10:45:46Z")

</div>

Oops!!  
Yes, it's 1 ms

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 11:54am UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/9 "2022-07-10T11:54:27Z")

</div>

The motor gives sudden jerks while rotating which is not the case when the first sketch of code is used

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 12:12pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/10 "2022-07-10T12:12:30Z")

</div>

Your first sketch moves the servo directly to its target position and it moves as fast as possible, hence no jerks

Your second sketch moves the servo in steps hence I would expect a jerkier movement as it moves, stops, moves, stops and so on

How bad is the jerkiness ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 1:00pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/11 "2022-07-10T13:00:08Z")

</div>

There are around 3 jerks from 0 to 180 degrees.

But this was not the case when I used Servo library...

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 1:02pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/12 "2022-07-10T13:02:32Z")

</div>

It sounds like there is something going on in the background that is causing the jerkiness. Do you actually need the servos to work in steps or could you just move them to their target positions ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 1:03pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/13 "2022-07-10T13:03:18Z")

</div>

Yes I need them in steps ☹

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 1:07pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/14 "2022-07-10T13:07:19Z")

</div>

Is that because you need the servo to move slowly ?

When you are testing ServoTimer2 have you got any other code in the sketch or just the 2 for loops, if so, what ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 1:27pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/15 "2022-07-10T13:27:13Z")

</div>

Hmm.. preferably fast

I am actually using servo motor with lidar to detect obstacles. Hence, I also call the lidar function within the for loop.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 1:30pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/16 "2022-07-10T13:30:50Z")

</div>

Try the slow for loops with no other code

It sounds like you have swapped one problem for another. My guess is that the lidar code is interfering with the servo code

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 1:41pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/17 "2022-07-10T13:41:22Z")

</div>

Hmm there are still many jerks ☹

When I am trying the same thing with Servo library it works fine..

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 1:44pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/18 "2022-07-10T13:44:38Z")

</div>

Which Arduino are you using and how is the servo powered ?

---

<div class="post-metadata">

**Author:** ![simdick](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@simdick](https://forum.arduino.cc/u/simdick)\
**Post date:** [July 10, 2022, 1:48pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/19 "2022-07-10T13:48:55Z")

</div>

I am using Arduino UNO. The servo motor is powered using power supply module connected to breadboard

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 10, 2022, 2:03pm UTC](https://forum.arduino.cc/t/using-servotimer2-library/1011060/20 "2022-07-10T14:03:01Z")

</div>

I have just tried this sketch using a Nano and the servo moves smoothly. Please try it

```cpp
#include <ServoTimer2.h> 

ServoTimer2 aServo;

void setup()
{
  aServo.attach(2);
}

void loop()
{
  for (int i = 750; i < 1500; i++)
  {
    aServo.write(i);
    delay(1);
  }
  for (int i = 1500; i > 750; i--)
  {
    aServo.write(i);
    delay(1);
  }
}
```

I presume that your servo and the Arduino have a common GND connection

[Next page](https://forum.arduino.cc/t/using-servotimer2-library/1011060.md?page=2)
