One servo works. other 3 dont.. (but they do with servo tester).

hello forum. i am working on a project, that requires to move 4 servos simultaneously.
i have tested all 4 of my servos, with a servo tester, and they all work just fine.

now, lets say i am using the following code :

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
  myservo.attach(7);  // attaches the servo on pin 9 to the servo object
}
 
 
void loop()
{
  for(pos = 0; pos < 180; pos += 1)  // 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
  }
  for(pos = 180; pos>=1; pos-=1)     // 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
  }
}

witch is exactly the same as the tutorial. i have servos on pin 7,8,A0,A1. when i am testing the pin 7, it works just fine. But if i use pin 8 , or ther others, nothing happens. the servos dont bother moving everywhere. they only move like 5-9 deggres counter-clockwise, when power is first applied… what i might be doing wrong?? thanks!

by the way, All the servos work if i connect them on the pin 7.... it seems like the pins 8,ao,a1 just fail to do their job...

Did you remember to change this:

  myservo.attach(7);

to, say, this:

  myservo.attach(8);

And have one for each servo, with different names myservo, yourservo etc

I’ve used pins 7,8,9 and 10 in the example below, it should do the same as your example, but for all 4 servos.

#include <Servo.h>
 
Servo myservo1;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
Servo myservo2;
Servo myservo3;
Servo myservo4;
 
int pos = 0;    // variable to store the servo position
 
void setup()
{
  myservo1.attach(7);  // attaches the servo on pin 7 to the servo object
  myservo2.attach(8);  // attaches the servo on pin 8 to the servo object
  myservo3.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(10);  // attaches the servo on pin 10 to the servo object
}
 
 
void loop()
{
  // move servo 1 on pin 7
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  // servo 2
  for(pos = 0; pos < 180; pos += 1)  // 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
  }
  for(pos = 180; pos>=1; 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
  }
  // servo 3
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  // servo 4
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                                
    myservo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

What is the power supply for the four servos?

Did you remember to change this:

yiap :slight_smile:

What is the power supply for the four servos?

the power supply is 2A. i "hope" it will be enough(will it ? :stuck_out_tongue: ). but i dont think power is the problem, because even if its one servo it still do sent move... i am starting to blame the atmega... :smiley: i think that something is wrong with it ( my project is still undergoing tests, so it might got "a little" fried or something..) i will try a new atmega tomorrow and i guess i'll see..

Forum wisdom is to budget an Amp each so 2A might not be enough. But I guess they’re unloaded for testing so it probably is.

You have them connected like in the attached pic?.. ie, remembering the grounds, and p/s separate from Arduino?

Many servos- small V2.jpg

yiap. well they will not be working under heavy load....

Can you post a picture/drawing of the circuit between the Arduino and the servos, including the servo power supply?

If you suspect there is a problem with your Arduino board you need to test it systematically.

Use the servo sweep sketch from the Arduino IDE and modify it to use each I/O pin on the Arduino one at a time.

...R

the power supply is 2A. i "hope" it will be enough(will it ?

What voltage ? What exactly IS the power supply ? (battery , bench lab supply ?)

I ran the code of Reply#3 on a 3A 5V regulator and all four servos worked perfectly despite the fact that the voltage on the 5V servo power dropped to 4.23 to 4.80 V and fluctuated between those two levels as the servos exercised one by one. The voltage dropped the most (to the 4.23 V low when a servo was homing to 0 servo at normal speed before beginning the slow move. The arduino UNO did not reset at any time during the test. I discovered the 5V supply I was using was also connected to a bunch of other circuits so I found a 5V 1A module
(ac/dc) that was brand new and connected that to the servo circuit and it only dropped to 4.76 occasionally but stayed mostly in the 4.95 to 5.00 V range.
Conclusions:
1-The code works.
2- Four unloaded servos is not too much of a load for a 1 A 5V supply.

Servo test code for four servos. It is important to properly wire your external power supply to the servos and the arduino. Check your wiring again and make sure it is making a good connection for the servos that are not working.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

What's the SYNTAX for the serial commands for the code you just posted ?

raschemmel:
What's the SYNTAX for the serial commands for the code you just posted ?

CSV

OK, SO . the problem, was Indeed the atmega board. i swapped it with another one, and everything works fine. but now i have a different problem... the schematic, is besically this :

So what it is, is this:
the main supply will be a 12v source. atm is a 2A, but on the final , it will be car battery.
it has a 5v 2A Linear regulator.

the arduino board, is connected with a pc , via usb. on the pc, i run a program made in java, (with eclipse). via taht program, i can control the servos.
when i issue a command from the pc, the arduino, first turns the relay on, so that the servos will get power, and then (no delay between "digitalWrite(12,HIGH), and Servo1.writeMicroseconds(1000)" ) and then turns the relay off, After waiting 2 seconds. (digitalWrite(12,LOW))

the arduino is supposed to also be sending a string back to the pc. So this usually works, BUT some times, just as when the servos are supposed to move, the arduino "Freezes". the servos dont move, the relay opens, but does not close after 2 seconds, and back on the Pc java application, i get a message like " no input stream available" (cant remember the actuall sentence)

i suspect that is the Power source? etc, when all the servos get energized at once, it fails, so the arduino sort of resets... what do you guys think??

i can post the full code of the arduino sketc, if you guys think its something that might have to do with the sketch

Have you got a snubber diode on your relay? The schematic doesn't show one.

i forgot to show, but yes, i do have one . keep in mind, that the relay has 12v power supply, and the transistor is being operated by 5v... which i didint think would work, but turns out it does...?

Your circuit doesn't show any ground connections. It would be best to post the complete circuit.

unless you are suggesting there is a ground loop problem that causes this, then i think posting a circuit with all the wires, is completly usless...