Servos go crazy when two arduino boards are connected.

Im trying to build a robot. I have a master board with an MD25 for just two large motors for movement, and two servos. This is powered by a 12V 2.0Ah battery. The second arduino powers just two small dc motors, with a separate 9v battery, that spin in such a way to project ping pong balls. The two servos act to release the ping pong balls using a chamber system. When the boards are not connected, the servos release the balls perfectly, and the constantly spinning dc motors will fire the balls.

I developed a master/slave code to turn the dc motors on at the right times, rather than being on constantly. The main target is to have the spinning dc motors to stop when the master board is disconnected from its power supply. I assume they have to be connected in some way to do this. This is when the servos misbehave and twitch and start to release balls randomly. I resorted back to the separate codes, and even a common grounding is enough to cause the twitching problems. I have no idea why the common grounding does this..?

I understand that servos draw a lot of current, but there will be a time where both the motors and servos are active.

Thanks in advance.

Master code with steup and some of the initial movements of movement motors and servos.

#include <Wire.h>                                             // Calls for I2C bus library

#define MD25ADDRESS         0x58                              // Address of the MD25
#define SPEED1              0x00                              // Byte to send speed to both motors for forward and backwards motion if operated in MODE 2 or 3 and Motor 1 Speed if in MODE 0 or 1
#define SPEED2              0x01                              // Byte to send speed for turn speed if operated in MODE 2 or 3 and Motor 2 Speed if in MODE 0 or 1
#define ENCODERONE          0x02                              // Byte to read motor encoder 1
#define ENCODERTWO          0x06                              // Byte to read motor encoder 2
#define ACCELERATION        0xE                               // Byte to define motor acceleration
#define CMD                 0x10                              // Byte to reset encoder values
#define MODE_SELECTOR       0xF                               // Byte to change between control MODES

int DualSpeedValue = 0;                                       // Combined motor speed variable
int speed1 = 0;                                               // Set forward speed for Mode 3 to initial value 0
int speed2 = 0;                                               // Set turn factor for Mode 3 to initial value 0 
int Mode = 2;                                                 // MODE in which the MD25 will operate selector value 
float Wheel_1_Distance_CM = 0;                                // Wheel 1 travel distance variable
float Wheel_2_Distance_CM = 0;  

  
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
Servo myservo2;                // a maximum of eight servo objects can be created
int pos = 20;    // variable to store the servo position 
 // Wheel 2 travel distance variable  
 
int motorPin = 11;
int motorPin2 = 12;

byte x = 0;

void setup(){
  Wire.begin();                                               // Begin I2C bus
  Serial.begin(9600);                                         // Begin serial
  delay(100);                                                 // Wait for everything to power up

  Wire.beginTransmission(MD25ADDRESS);                        // Set MD25 operation MODE to mode 2
  Wire.write(MODE_SELECTOR);
  Wire.write(Mode);                                           
  Wire.endTransmission(); 

  myservo.attach(11);  // attaches the servo on pin 11 to the servo object 
  myservo2.attach(12);
  
  pinMode(motorPin, OUTPUT);     //sets servos to hold position
  pinMode(motorPin2, OUTPUT);
  for(pos = 120; pos>=25; pos-=2)     // goes from 180 degrees to 0 degrees 
  {                                
   myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  for(pos = 120; pos>=25; pos-=2)     // goes from 180 degrees to 0 degrees 
  {                                
   myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15); 
  }
  
  encodeReset();       

  // Calls a function that resets the encoder values to 0 
}

void loop(){
  
  delay(6000);

  
  DualSpeedValue = 150;                                      // Sets a combined motor speed value for upcoming forward motion function
  Wheel_1_Distance_CM = 40;                                  // Sets wheel 1 travel distance value - cm, for upcoming forward motion function
  Wheel_2_Distance_CM = 40;                                  // Sets wheel 2 travel distance value - cm, for upcoming forward motion function
  forward();                                                 // Calls a function that moves the platform forward
  stopMotor();                                               // Calls a function that stops the platform 
  encodeReset();                                             // Calls a function that resets the encoder values to 0 
  
  x=1;
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte  
  Wire.endTransmission();

  DualSpeedValue = 150;                                      // Sets a combined motor speed value for upcoming right turn function
  Wheel_1_Distance_CM = +16.25;                               // Sets wheel 1 travel distance value - cm, for upcoming right turn motion function
  Wheel_2_Distance_CM = -16.25;                               // Sets wheel 2 travel distance value - cm, for upcoming right turn motion function
  Right_Turn();                                              // Calls a function that turn the platform right by 90 degrees
  stopMotor();                                               // Calls a function that stops the platform 
  encodeReset();                                             // Calls a function that resets the encoder values to 0


  for(pos = 25; pos < 120; pos += 2)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
   myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  delay(500);
  for(pos = 120; pos>=25; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
   myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  delay(100);
  
  for(pos = 25; pos < 120; pos += 2)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
   myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  delay(100);
  
  for(pos = 120; pos>=25; pos-=2)     // goes from 180 degrees to 0 degrees 
  {                                
   myservo.write(pos);              // tell servo to go to position in variable 'pos' 
   delay(15);    // waits 15ms for the servo to reach the position 

}

This is the slave code that reads x is 1 or 2 and will turn the motors on/off.

#include <Wire.h>

int x = 0;
int motorPin = 9;

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  pinMode(motorPin, OUTPUT);
  x = 0;
}

void loop()
{ 
 if (x==1){
  motorsOn();
  }
 
 if (x==2){
  motorsOff();
 }
 
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany){
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read();
    Serial.print(c);
  }
  x = Wire.read();    // receive byte as an integer
  Serial.println(x);
}

void motorsOn(){
  digitalWrite(motorPin, HIGH);   // turn the LED on (HIGH is the voltage level)
}

void motorsOff(){
  digitalWrite(motorPin, LOW);    // turn the LED off by making the voltage LOW              // wait for a second
}

When a servo loses its ground connection to the arduino/servo power supply common connection, it may behave erratically. Sounds like you are somehow breaking this connection when you connect.

Hi, can you post a picture of your project and a CAD or picture of a hand drawn circuit diagram please.

The situation at the moment is its yr project and you know everything about it, we have come in cold and need to get up to speed, so as much visual as well as text help is much appreciated.

Tom....... :slight_smile:
Is the 9V battery supplying the arduino as well as the motors, do you have bypass caps on the motors.

Sorry for the delay.

Attached is an upside down tacky circuit diagram. I forgot to add the 9v battery that is attached to the slave arduino.

I'm still unsure about how to rectify the servo grounding problem with the two arduino having a common ground.

One of us is going to have to turn that image the right way up. Do you think it should be you or me?

The diagram doesn't show the pin connections clearly.

Your servos seem to be powered from the Arduino. DON'T do that, the Arduino can't provide enough current. They need a separate power supply with a common ground with the Arduino.

...R