Servo(s) are going mad when USB cable is disconnected

I have an Arduino Mega 2560 and two metal gear servos connected to the board. And push button to control the servos. If I use the push button while USB plugged in to the my PC(windows 7/Fujitsu a series).

If I disconnect the USB from PC and press the push button, Servos will starting to rotate without sto (as it should be when push button is released. )

Where's the board getting its power from when you unplug the usb? And have to ask, where are the servos getting their power?

Show us a circuit and code....

Servos are powered by a regulator based circuit.
Also the Arduino. But separate regulators are used. (Common ground :slight_smile: )

Here is the code…

#include <Servo.h>

#define ENR  2   // Arduino pin tied to Enable Motor 1
#define ENL  3   // Arduino pin tied to Enable Motor 2

#define LN1  4    // Arduino pin tied to control Motor 1
#define LN2  5    // Arduino pin tied to control Motor 1
#define LN3  6    // Arduino pin tied to control Motor 2
#define LN4  7    // Arduino pin tied to control Motor 2

#define forward   30
#define backward  31
#define left      32
#define right     33
#define leftPin 34
#define rightPin 35
#define wide_open 36
#define narrow_close 37


Servo myservo;
Servo left_arm;
Servo right_arm;
byte arm_pos = 0;
byte pos = 0;
byte delayPeriod = 10;

byte forward_flg,backward_flg,left_flg,right_flg;

void setup()
{
  pinMode(ENR,OUTPUT);
  pinMode(ENL,OUTPUT);

  pinMode(LN1,OUTPUT);
  pinMode(LN2,OUTPUT);
  pinMode(LN3,OUTPUT);
  pinMode(LN4,OUTPUT);

  digitalWrite(ENR,LOW);
  digitalWrite(ENL,LOW);

  digitalWrite(LN1,LOW);
  digitalWrite(LN2,LOW);
  digitalWrite(LN3,LOW);
  digitalWrite(LN4,LOW);

  forward_flg=0; 
  backward_flg=0; 
  left_flg=0; 
  right_flg=0;
  delay(400);

  pinMode(28,INPUT);
  digitalWrite(28,LOW);

  myservo.attach(48);  
  left_arm.attach(44);
  right_arm.attach(46);

  left_arm.write(100);
  right_arm.write(0);

  pinMode(leftPin, LOW);   
  pinMode(rightPin, LOW);
  pinMode(wide_open, LOW);
  pinMode(narrow_close, LOW);
}

void JoisticUpdate()
{
  if(digitalRead(forward)==1)forward_flg=1;   
  else if(digitalRead(forward)==0)forward_flg=0;   
  if(digitalRead(backward)==1)backward_flg=1; 
  else if(digitalRead(backward)==0)backward_flg=0;   
  if(digitalRead(left)==1)left_flg=1;         
  else if(digitalRead(left)==0)left_flg=0;   
  if(digitalRead(right)==1)right_flg=1;       
  else if(digitalRead(right)==0)right_flg=0;    
}


void RobotMoveForwardAdvance(int SPEEDL,int SPEEDR)
{

  analogWrite(ENR,SPEEDL);
  analogWrite(ENL,SPEEDR);  

  digitalWrite(LN1,HIGH);    // Right motor
  digitalWrite(LN2,LOW);
  digitalWrite(LN3,HIGH);    // Left motor
  digitalWrite(LN4,LOW);  
}

void RobotMoveBackwardAdvance(int SPEEDL,int SPEEDR)
{

  analogWrite(ENR,SPEEDL);
  analogWrite(ENL,SPEEDR);  

  digitalWrite(LN1,LOW);    // Right motor
  digitalWrite(LN2,HIGH);
  digitalWrite(LN3,LOW);    // Left motor
  digitalWrite(LN4,HIGH);   

}

void RobotMoveStop(int SPEEDL,int SPEEDR)
{

  digitalWrite(ENR,LOW);
  digitalWrite(ENL,LOW);  

  digitalWrite(LN1,LOW);    // Right motor
  digitalWrite(LN2,LOW);
  digitalWrite(LN3,LOW);    // Left motor
  digitalWrite(LN4,LOW); 

}

void RobotTurnRight(int SPEEDL,int SPEEDR)
{ 
  analogWrite(ENR,SPEEDL);
  analogWrite(ENL,SPEEDR);

  digitalWrite(LN1,LOW);    // Right motor
  digitalWrite(LN2,HIGH);
  digitalWrite(LN3,LOW);   // Left motor
  digitalWrite(LN4,LOW);

}

void RobotTurnLeft(int SPEEDL,int SPEEDR)
{
  analogWrite(ENR,SPEEDL);
  analogWrite(ENL,SPEEDR);

  digitalWrite(LN1,LOW);    // Right motor
  digitalWrite(LN2,LOW);
  digitalWrite(LN3,LOW);     // Left motor
  digitalWrite(LN4,HIGH);

}

void ArmHome()
{
  //Arm controles goes here
  if(digitalRead(leftPin) == LOW)  
  {                         

    // in steps of 1 degree 
    if( pos > 0){
      //Serial.write("Arm is heading up");
      --pos;
    myservo.write(pos);              
    delay(delayPeriod);    }                  
  } 
  if(digitalRead(rightPin) == LOW)  
  {                              
    if( pos < 180){
      //Serial.write("Arm is heading down");
      ++pos;
    myservo.write(pos);             
    delay(delayPeriod); }       
  }
  if(digitalRead(wide_open) == HIGH)
  {
    left_arm.write(100);
    right_arm.write(0);
    /*for(pos = 50; pos < 100; pos += 1)  // goes from 0 degrees to 180 degrees 
     {                                  // in steps of 1 degree 
     left_arm.write(pos);    // tell servo to go to position in variable 'pos' 
     right_arm.write(pos);
     delay(15);                       // waits 15ms for the servo to reach the position 
     }*/
  }

  if(digitalRead(narrow_close)==HIGH)
  {
    left_arm.write(60);
    right_arm.write(20);
  }
}

void loop()
{
  if(forward_flg==1)
  {
    RobotMoveForwardAdvance(150,150);
  }
  else if(backward_flg==1)
  {
    RobotMoveBackwardAdvance(150,150);
  }
  else if(right_flg==1)
  {
    RobotTurnRight(150,150);
  }
  else if(left_flg==1)
  {
    RobotTurnLeft(150,150);
  }
  else if(forward_flg==1 && left_flg==1)
  {
    RobotMoveForwardAdvance(100,150);
  }
  else if(forward_flg==1 && right_flg==1)
  {
    RobotMoveForwardAdvance(150,100);
  }
  else
  {
    RobotMoveStop(0,0);
  }


  delay(100);
  JoisticUpdate();
  //  JoisticTest();
  ArmHome();
  delay(150);
}

FYI-

L LED keeps blinking while system is doing crazy stuffs. :roll_eyes:

Just thought, There may be a serial communication error with this board?

What do you mean regulators? What voltage are you supply externally and where? This you were asked for a circuit. Even a rough schematic to show what is powered and how.

It sounds like the on-board regulator is sagging under load, causing the chip to reset.

If Vin is more than 9V, you may get less than 500mA out of the on-board regulator. USB can provide that current since it isn’t regulated on the board.

Hi, I think you had better post a circuit diagram, just a picture of a hand drawn one would be enough.
A picture of your project and how its connected would also give us some help.

Tom..... :slight_smile: