# Servo library problem

**URL:** <https://forum.arduino.cc/t/servo-library-problem/1129603>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [May 22, 2023, 5:43am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603 "2023-05-22T05:43:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 22, 2023, 5:43am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/1 "2023-05-22T05:43:20Z")

</div>

I am using an Arduino Uno Wifi rev 2 with a CNC shield. One of the operations is to move a servo through 120 degrees and back. The problem I have, is that sometimes the servo works faultlessly. However, on other occasions I can hear a ticking sound and the servo doesn't move. By changing out the servo for one of the same brand and spec, I get a faultless movement. I change back and sometimes it works and sometimes it doesn't. Both servos are brand new. Can this be the Servo,h library causing the issue?

---

<div class="post-metadata">

**Author:** ![amitf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/amitf/32/1057288_2.png) [@amitf](https://forum.arduino.cc/u/amitf)\
**Post date:** [May 22, 2023, 5:47am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/2 "2023-05-22T05:47:21Z")

</div>

I think there is a connection problem. May be due to loose wiring.

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 22, 2023, 6:13am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/3 "2023-05-22T06:13:48Z")

</div>

I have checked multiple times, however you may be right. It could be inside the servo.  
Thank you.

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 22, 2023, 6:25am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/4 "2023-05-22T06:25:57Z")

</div>

I think you are right. I took the back off and fired it up and it works every time. Possibly a wet solder joint. Thank you very much

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 22, 2023, 7:24am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/6 "2023-05-22T07:24:49Z")

</div>

I thought it might be a connection inside the servo, however after running a soldering iron over the connections and putting the servo back together, I am still having this randomness. Is there another library I could use with the Arduino Uno Wifi that I can try which would eliminate a software issue?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 22, 2023, 8:17am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/7 "2023-05-22T08:17:10Z")

</div>

> [@paymentupfront](#):
>
> Is there another library I could use

> [@paymentupfront](#):
>
> By changing out the servo for one of the same brand and spec, I get a faultless movement. I change back and sometimes it works and sometimes it doesn't. Both servos are brand new.

You have proven that it is not the library, by swapping servos and one servo always has the fault.

What servo are you using?  
Can you please post a link to data/specs, where you purchased the servos?

I'm suspecting you have got the cheap SG90 servos with plastic gears.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/3/4/f347d5de20c9637b7a82ec461bd6e5429ff8ef7c.jpeg)

Tom... 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [May 22, 2023, 8:32am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/8 "2023-05-22T08:32:18Z")

</div>

> [@paymentupfront](#):
>
> is that sometimes the servo works faultlessly.

Hello [paymentupfront](https://forum.arduino.cc/u/paymentupfront)

We need some additional information.

Post your sketch, well formated, with well-tempered comments and in so called code tags "\< code \>" and a detailed circuit diagram to see how we can help.  
Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [May 22, 2023, 11:20am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/9 "2023-05-22T11:20:38Z")

</div>

> [@paymentupfront](#):
>
> Is there another library I could use with the Arduino Uno Wifi that I can try which would eliminate a software issue?

... or you could try

```arduino
servo.attach(pin, min, max);

```

Where min = 1000 and max = 2000 for a generic [SG90](http://www.ee.ic.ac.uk/pcheung/teaching/DE1_EE/stores/sg90_datasheet.pdf) servo.

If using

```arduino
servo.attach(pin);

```

Here, the default min = 544, max = 2400 could be beyond the mechanical limits, therefore ...

> [@paymentupfront](#):
>
> I can hear a ticking sound and the servo doesn't move.

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [May 22, 2023, 11:22am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/10 "2023-05-22T11:22:24Z")

</div>

How are you powering the servo?

---

<div class="post-metadata">

**Author:** ![crayton](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@crayton](https://forum.arduino.cc/u/crayton)\
**Post date:** [May 22, 2023, 9:00pm UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/11 "2023-05-22T21:00:01Z")

</div>

It could also be a gear issue, i had that with one of mine. The plastic gears were stripped by the metal ones. You would have to take apart the servo and then replace the broken geara with new ones from another motor.

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 23, 2023, 2:14am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/12 "2023-05-23T02:14:48Z")

</div>

excellent work. The servo works faultlessly. Here is the code I changed to have a min and max values. Thank you dlloyd `void moveServo(int fromPos, int toPos, int duration, int min, int max) {  
int increment = 1;  
int delayTime = duration / abs(toPos - fromPos);

if (toPos \< fromPos) {  
increment = -1;  
}

for (int pos = fromPos; pos != toPos; pos += increment) {  
pos = constrain(pos, min, max); // Constrain the position within the min and max values  
myservo.write(pos);  
delayMicroseconds(delayTime);  
}

toPos = constrain(toPos, min, max); // Constrain the final position within the min and max values  
myservo.write(toPos);  
delayMicroseconds(delayTime); // Add a small delay at the end for stability  
}  
`

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [May 23, 2023, 3:10am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/13 "2023-05-23T03:10:32Z")

</div>

Glad you've got it working.

Just to mention, all you needed to do is use `servo.attach(pin, min, max);` in setup. The library internally uses constrain for both the `write()` and `writeMicroseconds()` functions.

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 23, 2023, 4:43am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/14 "2023-05-23T04:43:47Z")

</div>

Thank you. I am a novice and any help is greatly appreciated. I will be needing assistance later on as the project matures. I hope to operate the arduino via blue tooth or wifi and run a printer program. Would you be interested in working on a project?  
Peter

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 23, 2023, 5:05am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/15 "2023-05-23T05:05:41Z")

</div>

Hi,  
What this points out is that the servos that you have, have the ability to physically move more or less than 0 to 180, even though being signaled to go 0 to 180.  
Not all the one brand/model are the same.

Glad you got them working properly rather than having to purchase more precise servos.

Tom... 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 23, 2023, 5:26am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/16 "2023-05-23T05:26:09Z")

</div>

Thank you everyone. Here are the specifications.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/5/b/95b6215694564c90aa70ed6cc6840400b76c354c.jpeg)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/e/a/dea506695e2c6385b4908ad5bf0568ea784b4450.png)

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 26, 2023, 5:20am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/17 "2023-05-26T05:20:04Z")

</div>

Hi dlloyd. seems like I might have jumped the gun. When I came in the following day [without changing anything] both servos did not work. I uploaded an earlier servo sweep code and both servos worked seamlessly. What am I doing wrong? I am hoping you can help. Power supply is 24V 15A. The servos are listed in the above post. Here is the code that works with the servo `/\* Sweep  
by BARRAGAN [http://barraganstudio.com](http://barraganstudio.com)  
This example code is in the public domain.

modified 8 Nov 2013  
by Scott Fitzgerald  
[https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep](https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep)  
\*/  
#include \<Servo.h\>

Servo myservo; // create servo object to control a servo  
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {  
myservo.attach(9); // attaches the servo on pin 9 to the servo object  
}

void loop() {  
delay(5); //stops the shuttering at the begining of the stroke.  
for (pos = 0; pos \<= 145; pos += 1) { // goes from 0 degrees to 180 degrees  
// in steps of 1 degree  
myservo.write(pos); // tell servo to go to position in variable 'pos'  
delay(5); // waits 15 ms for the servo to reach the position  
}  
for (pos = 145; pos \>= 0; pos -= 1) { // goes from 180 degrees to 0 degrees  
myservo.write(pos); // tell servo to go to position in variable 'pos'  
delay(5); // waits 15 ms for the servo to reach the position

}  
}  
`Here is the code that I am having trouble with`#include \<Servo.h\>  
// in this code the servo has a min and max because one servo did not perform correctly and now it does.  
// Define pin numbers  
#define EN\_PIN 8  
#define X\_DIR\_PIN 5  
#define X\_STP\_PIN 2  
#define Y\_DIR\_PIN 6  
#define Y\_STP\_PIN 3  
#define Z\_DIR\_PIN 7  
#define Z\_STP\_PIN 4  
#define BTN\_PIN 11  
#define SERVO\_PIN 9 // Change = connected Pin2 to Z+ on the CNC shield  
#define Y\_MS\_PIN 10

// TMC2208  
int delayTime = 160; // Delay between each pause (us)  
int zStps = 1000; // Sets the printer band width, i.e., width of ink cartridge  
bool isMoving = false; // Flag to keep track if the stepper is moving

Servo myservo;

void step(boolean dir, byte dirPin, byte stepperPin, int steps) {  
digitalWrite(dirPin, dir);  
delay(0.001); // Changes speed of motor

for (int i = 0; i \< steps; i++) {  
digitalWrite(stepperPin, HIGH);  
delayMicroseconds(delayTime);  
digitalWrite(stepperPin, LOW);  
delayMicroseconds(delayTime);  
}  
}  
void moveServo(int fromPos, int toPos, int duration, int min, int max) {  
int increment = 1;  
int delayTime = duration / abs(toPos - fromPos);

if (toPos \< fromPos) {  
increment = -1;  
}

for (int pos = fromPos; pos != toPos; pos += increment) {  
pos = constrain(pos, min, max); // Constrain the position within the min and max values  
myservo.write(pos);  
delayMicroseconds(delayTime);  
}

toPos = constrain(toPos, min, max); // Constrain the final position within the min and max values  
myservo.write(toPos);  
delayMicroseconds(delayTime); // Add a small delay at the end for stability  
}

void setup() {  
pinMode(X\_DIR\_PIN, OUTPUT);  
pinMode(X\_STP\_PIN, OUTPUT);  
pinMode(Y\_DIR\_PIN, OUTPUT);  
pinMode(Y\_STP\_PIN, OUTPUT);  
pinMode(Z\_DIR\_PIN, OUTPUT);  
pinMode(Z\_STP\_PIN, OUTPUT);  
pinMode(BTN\_PIN, INPUT\_PULLUP); // Set the button pin as input with pull-up resistor  
pinMode(EN\_PIN, OUTPUT);  
digitalWrite(EN\_PIN, LOW);  
pinMode(Y\_MS\_PIN, INPUT\_PULLUP);

myservo.attach(SERVO\_PIN); // Specify servo pin

// Home the machine  
while (digitalRead(Y\_MS\_PIN) == HIGH) {  
// Move Y stepper away from the microswitch (Normally Open)  
digitalWrite(Y\_DIR\_PIN, LOW); // Reverse direction  
digitalWrite(Y\_STP\_PIN, HIGH);  
delayMicroseconds(delayTime);  
digitalWrite(Y\_STP\_PIN, LOW);  
delayMicroseconds(delayTime);  
}

// Slowly move Y stepper until microswitch is Normally Closed  
while (digitalRead(Y\_MS\_PIN) == HIGH) {  
digitalWrite(Y\_DIR\_PIN, HIGH); // Move towards the microswitch  
digitalWrite(Y\_STP\_PIN, HIGH);  
delayMicroseconds(delayTime);  
digitalWrite(Y\_STP\_PIN, LOW);  
delayMicroseconds(delayTime);  
}

// Check that Servo is at 0 degrees  
if (myservo.read() != 0) {  
myservo.write(0);  
delay(1000);  
}

// Move Z stepper 6400 steps and stop  
for (int i = 0; i \< 6400; i++) {  
step(false, Z\_DIR\_PIN, Z\_STP\_PIN, 1);  
}  
step(false, Z\_DIR\_PIN, Z\_STP\_PIN, 0);

// Move servo to 115 degrees and back to 0 degrees  
myservo.write(115);//set the travel distance of the servo arm  
delay(1000);  
myservo.write(0);  
delay(1000);

// Reverse Z stepper 2000 steps and stop  
for (int i = 0; i \< 2000; i++) {  
step(true, Z\_DIR\_PIN, Z\_STP\_PIN, 1);  
}  
step(true, Z\_DIR\_PIN, Z\_STP\_PIN, 0);  
}

void loop() {  
// Read the button state  
bool buttonState = !digitalRead(BTN\_PIN); // Invert the button state, as we're using a Normally Open button

if (buttonState && !isMoving) {  
// Button has been pressed, move the Z motor continuously until the button is released  
isMoving = true;

```
while (buttonState) {
  // Move the Z motor one step at a time
  step(false, Z_DIR_PIN, Z_STP_PIN, 1);

  // Read the button state
  buttonState = !digitalRead(BTN_PIN); // Invert the button state, as we're using a Normally Open button
}

// Stop the Z motor
step(false, Z_DIR_PIN, Z_STP_PIN, 0);

// Move the servo to 0 degrees
myservo.write(0);
delay(1000);
// Move the servo to 115 degrees
myservo.write(115);
delay(1000);
// Move the servo back to 5 degrees
myservo.write(0);

isMoving = false;
  // Reverse Z stepper 2000 steps and stop
  delay(1000); // Delay to allow servo to return to 5 degrees

```

for (int i = 0; i \< 2000; i++) {  
step(true, Z\_DIR\_PIN, Z\_STP\_PIN, 1);  
}  
step(true, Z\_DIR\_PIN, Z\_STP\_PIN, 0);  
}  
}

`  
I hope you can help.  
Peter

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 26, 2023, 8:35am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/18 "2023-05-26T08:35:19Z")

</div>

Hi,  
To add code please click this link;

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

Thanks.. Tom.. 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [May 26, 2023, 12:26pm UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/19 "2023-05-26T12:26:33Z")

</div>

Here's what I've noticed with your code and with the Servo library.

In the [Servo library ServoTimers.h](https://github.com/arduino-libraries/Servo/blob/master/src/megaavr/ServoTimers.h), lines 29-31:

```arduino
//#define USE_TIMERB1 // interferes with PWM on pin 3
#define USE_TIMERB2 // interferes with PWM on pin 11
//#define USE_TIMERB0 // interferes with PWM on pin 6

```

In your code:

```arduino
#define BTN_PIN 11
pinMode(BTN_PIN, INPUT_PULLUP); // Set the button pin as input with pull-up resistor

```

I think the Servo Library using TIMERB2 would interfere with your BTN\_PIN.  
Changing the timer would interfere with pin 3 or 6 which you are already using, so I would just change the BTN\_PIN to another pin that's free.

The library's issue #103 [Attach with Min Max bug](https://github.com/arduino-libraries/Servo/issues/103) could possibly create problem with a Servo with limited movement range.

Can't help much further as I don't have the hardware.

---

<div class="post-metadata">

**Author:** ![paymentupfront](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@paymentupfront](https://forum.arduino.cc/u/paymentupfront)\
**Post date:** [May 28, 2023, 7:39am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/20 "2023-05-28T07:39:57Z")

</div>

Thank you, I tried it but still had problems. Finally I added a different delay and called it int servoDelayTime and it worked. Well, given the history... I hope it still works tomorrow. Thank you so much for your help.  
Peter

---

<div class="post-metadata">

**Author:** ![aliyaaii](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@aliyaaii](https://forum.arduino.cc/u/aliyaaii)\
**Post date:** [July 17, 2023, 8:42am UTC](https://forum.arduino.cc/t/servo-library-problem/1129603/22 "2023-07-17T08:42:34Z")

</div>

Hi guys  
I'm glad to hear that you found a solution by adding a different delay and renaming it as "int servoDelayTime.

[Next page](https://forum.arduino.cc/t/servo-library-problem/1129603.md?page=2)
