# continuous rotation of servo problem

I am making a solar tracker using MG995 servo motors. The problem I am facing is that as soon as I run the program the servo starts continuously rotating. Can someone please help me to figure it out?

I am using an Arduino Uno and external power supply for 2 servos.

Below is my code:

#include <Servo.h>
//defining Servos
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;

Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = 2; //top left LDR green
int ldrtopr = 1; //top right LDR yellow
int ldrbotl = 3; // bottom left LDR blue
int ldrbotr = 0; // bottom right LDR orange

void setup ()
{
Serial.begin(9600);
servohori.attach(5);
servohori.write(0);
servoverti.attach(6);
servoverti.write(0);
delay(500);
}

void loop()
{
//capturing analog values of each LDR

// calculating average
int avgtop = (topl + topr) / 2; //average of top LDRs
int avgbot = (botl + botr) / 2; //average of bottom LDRs
int avgleft = (topl + botl) / 2; //average of left LDRs
int avgright = (topr + botr) / 2; //average of right LDRs
Serial.print("avgtop = ");
Serial.print(avgtop);
Serial.print("\n");
Serial.print("avgbot = ");
Serial.print(avgbot);
Serial.print("\n");
Serial.print("avgleft = ");
Serial.print(avgleft);
Serial.print("\n");
Serial.print("avgright = ");
Serial.print(avgright);
Serial.print("\n");
delay(500);

if (avgtop < avgbot)
{
servoverti.write(servov +1);
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
delay(10);
}
else if (avgbot < avgtop)
{
servoverti.write(servov -1);
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
delay(10);
}
else
{
servoverti.write(servov);
}

if (avgleft > avgright)
{
servohori.write(servoh +1);
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
delay(10);
}
else if (avgright > avgleft)
{
servohori.write(servoh -1);
if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
delay(10);
}
else
{
servohori.write(servoh);
}
delay(50);
}

• Are the reading from LDRs coherent ? (what are the Serial output ?) Are you able to control the servo motors independently by running a code that just controls the servo ? Are they new ? I doubt that these servo motors are intended to run continuously. They should have limited angular position. Are you sure about your servo references ? Commented Jun 6, 2020 at 12:38
• ya this servos are new and i am trying to control servo independently but still it's rotating Commented Jun 6, 2020 at 12:42
• When posting code you should make sure that it is indented properly and you should remove all the irrelevant stuff. There is no need for all the serial print sections to be posted in your code as that is not part of the problem. Commented Jun 6, 2020 at 13:38

You simply have the wrong servo for your application. Google MG955 and you will find that some are labelled "continuous rotation" or "360°". It means that you can not set it to a particular angle.

Arduino Servo library tells:

Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).

A standard servo motor uses a potentiometer to acquire the shaft angular position. Then a feedback loop controls the position.

If your servo is new, it must be a simple DC motor with a reduction stage (like here). If it is not new, it also could be a standard servo that has been hacked to break the position control loop (like here).

• i have order this servo : amazon.in/dp/B07QFBJMV5/ref=cm_sw_r_wa_apa_i_8PTmEbCZKRCP0 Commented Jun 6, 2020 at 13:35
• Yeah and that is a continuous rotation servo as I suspected :). Buy another one and your application should work well ! Commented Jun 6, 2020 at 13:38
• can you please suggest me which type of servo should i buy my panel weight is around 1Kg Commented Jun 6, 2020 at 13:42
• It depends on your mechanism. I will not do the math for you. Besides it is out of topic here. You found your solution : buy a standard servo (not a continuous one). Good luck ! Commented Jun 6, 2020 at 14:18