0
\$\begingroup\$

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()
{
servoh = servohori.read();
servov = servoverti.read();
//capturing analog values of each LDR
int topl = analogRead(ldrtopl);
int topr = analogRead(ldrtopr);
int botl = analogRead(ldrbotl);
int botr = analogRead(ldrbotr);

// 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);
}
\$\endgroup\$
3
  • \$\begingroup\$ 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 ? \$\endgroup\$
    – Mathieu G.
    Commented Jun 6, 2020 at 12:38
  • \$\begingroup\$ ya this servos are new and i am trying to control servo independently but still it's rotating \$\endgroup\$
    – Muhammed
    Commented Jun 6, 2020 at 12:42
  • \$\begingroup\$ 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. \$\endgroup\$
    – Transistor
    Commented Jun 6, 2020 at 13:38

1 Answer 1

1
\$\begingroup\$

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).

\$\endgroup\$
4
  • \$\begingroup\$ i have order this servo : amazon.in/dp/B07QFBJMV5/ref=cm_sw_r_wa_apa_i_8PTmEbCZKRCP0 \$\endgroup\$
    – Muhammed
    Commented Jun 6, 2020 at 13:35
  • \$\begingroup\$ Yeah and that is a continuous rotation servo as I suspected :). Buy another one and your application should work well ! \$\endgroup\$
    – Mathieu G.
    Commented Jun 6, 2020 at 13:38
  • \$\begingroup\$ can you please suggest me which type of servo should i buy my panel weight is around 1Kg \$\endgroup\$
    – Muhammed
    Commented Jun 6, 2020 at 13:42
  • 1
    \$\begingroup\$ 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 ! \$\endgroup\$
    – Mathieu G.
    Commented Jun 6, 2020 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.