Problem with servo code (sometimes it works fine and others does not)

Hello and thank you in advance,

I have an issue programming my servo.

I want to be able to set my servo at pos=180 when i send ‘b’ at serial, and pos=0 when i send ‘a’.

My code so far is :

#include <Servo.h>

Servo myservo;  

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

void setup()
 myservo.attach(11);  // attaches the servo on pin 11 
 myservo.write(90); //set servo to midpoint as default

void loop()
  if (Serial.available() > 0)
   int value =; //reads the incoming byte

     case 'a':
     pos = 0;
     case 'b':
     pos = 180;

The problem is that sometimes everything works fine and other times servo is not moving at all and overheating making a noise.

PS I have to command it using chars ‘a’ and ‘b’ (or whatever chars and not just send a position at serial)

Is the servo capable of moving between 0 and 180 degrees ? Not all of them are.

Try the Sweep example in the IDE with a longer delay between the writes to the servo and print the value of the position variable each time you write to the servo. Does the servo stop moving before the position variable reaches its limit in one or other of the directions ?

How is the servo powered ?
What type of servo is it ?

In your program I would use a char variable when reading the serial input as it makes more sense. You should also make pos a byte variable to save memory.

Tried again with an other servo thats is more suitable for what i am about to do.

Changed the range to 0-120 cause its a Feetech FS90MG.

Operating Speed : 0.12sec/60degree (4.8V) / 0.10sec/60degree (6V)
Stall Torque : /
Operating Voltage :4.8V~6V
Control System :Analog
Direction :CCW
Operating Angle :120degree
Required Pulse :900us-2100us
Bearing Type :None
Gear Type :Metal
Motor Type :Metal
Connector Wire Length :20 cm
) if its any help

Still have the same issue. Some times it works, some not.

Even the sweep example is behaving the same.

Servo is connected to the arduino without shield. (Brown -> GND, Red-> 5v, Orange-> digital pin 11)

I ll try changing the code and making pos a byte in a bit .

Servo is connected to the arduino without shield. (Brown -> GND, Red-> 5v, Orange-> digital pin 11)

You can NOT expect the Arduino to provide enough current to power the servo. Power the servo correctly.

When you add a separate 4.8-6V supply for the servo, be sure to connect the Brown wire on the servo to both the negative side of the power supply and the Ground of the Arduino.