Problem with MG996R

Hi Guys,

I need help with this problem, i want control the rotation of servo mg996r using a simple web page html, but when i change the servo 9g to servo mg996r doesn't work.

I see many videos when the servo was connected at arduino and functions normally.

The video below is my case.

I try use a external font 9v-1a dc, but the same problem ocurred.

Any suggestion?

The lighting is very poor in your video - was there a blackout? - so I can't figure what the problem is.

What do you mean by "mg996r doesn't work"

What is it supposed to?
What does it actually do?
Post the code are you using?
Post a diagram of how everything is connected - a photo of a pencil drawing will be fine.

...R

Hi!!

When i click to Rotate the servo "lose connection with pc(??)" and pin of reset start blink, but when i make the same action button with 9g servo, functions normally

Servo - Arduino
Red - 5V
Yellow - pin 9
Brown - GND

.

#include <Servo.h>
#include <SPI.h>
#include <String.h>
#include <Ethernet.h>

#define REQUEST_BUFFER_SIZE 700
#define MENOR_ANGULO 10
#define MAIOR_ANGULO 175
#define MEDIO_ANGLE 90

#define SERVO_PIN0 9 // Pino 2 = dados

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 125 };

EthernetServer server(80);
Servo servo;
int servo1pos = MENOR_ANGULO;
int servo2pos = MENOR_ANGULO;



char requestBuffer[REQUEST_BUFFER_SIZE];

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  servo.attach(SERVO_PIN0);
  Serial.begin(9600);
}

void loop()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      
      readHTTPRequest(client, requestBuffer, REQUEST_BUFFER_SIZE);
      char action = getHTTPParam('TipoGiro', requestBuffer, REQUEST_BUFFER_SIZE);
      // HTTP padrão
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      
      Serial.println("about to write body");
      client.println("<html><body>");
      client.println("<script>");
      client.println("function summon(c)");
      client.println("{");
      client.println("	document.location = 'TipoGiro=' + c;");
      client.println("}");
      client.println("</script>");
      
      client.println("<h1>Alimentador Automatio para PETS</h1>");
      client.println("<input type='button' value='TipoGiro=1' onClick='summon(1)'/>");
      client.println("<input type='button' value='TipoGiro=2' onClick='summon(2)'/>");
      client.println("<input type='button' value='TipoGiro=3' onClick='summon(3)'/>");
      client.println("
<img src='http://photos-e.ak.fbcdn.net/hphotos-ak-snc6/185209_255805207771296_255804684438015_998863_49064_a.jpg'/>");
      
      client.println("<object type=\"application/x-shockwave-flash\" width=\"320\" height=\"240\" data=\"http://RENANPC:8080/flashMJPEG.swf\">");
      client.println("<param name=\"movie\" value=\"http://RENANPC:8080/flashMJPEG.swf?webcam=http://RENANPC:8080/cam_1.jpg&amp;refresh=50&amp;connect=&amp;offline=&amp;transtype=Fade&amp;bgcolor=#FFFFFF&amp;txtcolor=#FFFFFF\" />");
      client.println("<param name=\"FlashVars\" value=\"webcam=http://RENANPC:8080/cam_1.jpg&amp;refresh=50&amp;connect=&amp;offline=&amp;transtype=Fade&amp;bgcolor=#FFFFFF&amp;txtcolor=#FFFFFF\" />");
      client.println("<param name=\"loop\" value=\"false\" />");
      client.println("<param name=\"menu\" value=\"false\" />");
      client.println("<param name=\"quality\" value=\"best\" />");
      client.println("<param name=\"scale\" value=\"noscale\" />");
      client.println("<param name=\"salign\" value=\"lt\" />");
      client.println("<param name=\"wmode\" value=\"opaque\" />");
      client.println("</object>");
      client.println("</body></html>");
      Serial.print("Param TipoGiro"); Serial.println(action);

      Serial.println("written body");
      client.stop();
      

       
      
      if (action == '1')
      {
        servo1pos=179;
        delay(1000);
        
      }
      if (action == '2')
      {
        servo1pos = 179;
        delay(1000);
      }
       if (action == '3')
      {
        servo1pos = 180;
        delay(1500);
      }

      
    }
    delay(10);
  }
  servo.write(servo1pos);
  servo1pos=90;
  
}



void readHTTPRequest(EthernetClient client, char* buffer, int bufferSize)
{
  // read the request until you get a blank line or the buffer is full 
  int i = 0;
  int linePos = 0;
  boolean tf = false;
  while (! tf)
  {
    if (client.available())
    {
      char ch = client.read();

      if ((ch == '\n' && linePos < 2) || i == bufferSize - 1)
      {
        tf= true;
      }
      else
      {
        buffer[i] = ch;
        linePos++;
        if (ch == '\n')
        {
          linePos = 0;
        }
      }
      i++;
    }
  }
}

char getHTTPParam(char name, char* buffer, int bufferSize)
{

  for (int i = 1; i < bufferSize - 1; i++)
  {
    if (buffer[i] == '=' && buffer[i-1] == name)
    {
      return buffer[i+1];
    }
  }
  return '\0';
}

Servo - Arduino
Red - 5V
Yellow - pin 9
Brown - GND

If the red servo wire is connected to the arduino 5v pin, then when the servo tries to move it basically shorts out the arduino power supply causing the arduino to reset. Servos need external power supplies similar to the below setup.

^yes. Sounds like you are power cycling the arduino, because the servo draws too much current. At startup and stall, the mg996r can draw more than 2 amps.

If you look at the figures for that servo it can produce about 1Nm of torque and rotates at
5.25 rad/s (for 4.8V supply voltage).

That's >5W of mechanical power - even if it were 100% efficient it would pull more than
1A by the laws of physics. In reality mechanical and motor efficiency losses easily mean
its going to need 2A peak.

Thank's all for help :wink:

I will try use a 9v battery and post if i got success or fail

No, use 4 x AA NiMH rechargables, then it will work.